首页 文章

ggplot2用黑色轮廓绘图

提问于
浏览
2

我正在从另一个论坛(https://www.biostars.org/p/268514/#268559)发帖,我得到了有用的评论,但仍然对ggplot2的输出感到困惑 .

我试图在圆圈上得到一个简单的黑色轮廓,但我的代码现在给出了我的随机颜色,似乎忽略了我的scale_colour_manual(values = cols)参数 . 当我运行以下代码时:

# creating color palette

> cols <- c("red" = "red", "orange" = "orange", "nonsignificant" = "darkgrey", "Increased" = "#00B2FF", "Decreased" = "#00B2FF")

# Make a basic ggplot2 object

> vol <- ggplot(data, aes(x = lfc, y = pval, color = color, labels=gene))

# inserting mnaual colors as per color pallette  with term "scale_colour_manual(values = cols)" below

> vol +   
  ggtitle(label = "Volcano Plot", subtitle = "Colored by fold-change direction") +
  geom_point(size = 2.5, alpha = 1, na.rm = T) +
  scale_colour_manual(values = cols) +
  theme_bw(base_size = 14) + 
  theme(legend.position = "right") + 
  xlab(expression(log[2]("VitD" / "Carrier"))) + 
  ylab(expression(-log[10]("FDR"))) + # Change Y-Axis label
  geom_hline(yintercept = 1, colour="#990000", linetype="dashed") + geom_vline(xintercept = 0.586, colour="#990000", linetype="dashed") + geom_vline(xintercept = -0.586, colour="#990000", linetype="dashed") + 
  scale_y_continuous(trans = "log1p")

我用我定义的调色板得到一个漂亮的图形:

Color volcano plot

接下来,我想为点添加边框,因此我只更改了2个代码 . 一开始我用"fill = color"而不是"color = color"制作基本的 ggplot 对象

> vol <- ggplot(data, aes(x = lfc, y = pval, fill = color, labels=gene))

我将“shape = 21,color =”black“”添加到“geom_point”参数中:

> vol +   
  ggtitle(label = "Volcano Plot", subtitle = "Colored by fold-change direction") +
  geom_point(size = 2.5, alpha = 1, na.rm = T, shape = 21, colour = "black") +
  scale_colour_manual(values = cols) +
  theme_bw(base_size = 14) + 
  theme(legend.position = "right") + 
  xlab(expression(log[2]("VitD" / "Carrier"))) + 
  ylab(expression(-log[10]("FDR"))) + l
  geom_hline(yintercept = 1, colour="#990000", linetype="dashed") + geom_vline(xintercept = 0.586, colour="#990000", linetype="dashed") + geom_vline(xintercept = -0.586, colour="#990000", linetype="dashed") + 
  scale_y_continuous(trans = "log1p")

突然间,我有一个似乎随机选择5种颜色的情节;如果我打印颜色:

print(cols)

red      orange         nonsignificant      Increased      Decreased 
"red"    "orange"      "darkgrey"           "#00B2FF"      "#00B2FF"

调色板仍在那里 .

2 回答

  • 2

    当您将 color = cols 更改为 fill = cols 时,您还应将 scale_color_manual(...) 更改为 scale_fill_manual(...)

  • 4

    我通常只是在彩色背后添加更大的黑点来解决这个问题 .

    ggplot(mtcars, aes(wt, qsec)) +
        geom_point(color = "black", size = 2.5) +
        geom_point(size = 2, aes(color = factor(am)))
    

    enter image description here

相关问题