R ggplot(): geom_point() with color-palette "Greens" ,如何得到黑点边框?

R ggplot(): geom_point() with color-palette "Greens" , how to get black point border?

我用 ggplot() 绘制散点图并使用特定的调色板,即 'Greens'。基本上我对情节非常满意,但我希望每个点周围都有黑色边框。我的剧情代码是:

p <- ggplot(data = df.dataCorrelation, aes(x = prod1, y = prod2)) +
     geom_point(aes(color = year)) +
     geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
     theme_classic() +
     theme(legend.position = "none") +
     theme(panel.background = element_blank()) +
     scale_color_brewer(palette = 'Greens') +   # customized color palette
     xlab(product1) +
     ylab(product2) +
     ggtitle("Correlation Scatter Plot (Pearson)") +
     theme(plot.title = element_text(hjust = 0.5, face = "bold"))

并提供以下图形:

我知道我可以用命令画黑边:

geom_point(aes(color = year, fill = ?), color = "black", pch = 21),

但这不适用于我选择的调色板,因为我不知道在 fill = ?

中使用什么

试试这个。

此处为可重现示例的数据

library(dplyr)

product1 <- "product1"
product2 <- "product2"
df.dataCorrelation <- iris %>% rename(prod1 = Petal.Length,
                                      prod2 = Petal.Width,
                                      year  = Species)

这是你的代码

library(ggplot2)

ggplot(data = df.dataCorrelation, aes(x = prod1, y = prod2)) +
 geom_point(aes(fill = year), colour = "black", shape = 21, size = 3) +
 geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
 theme_classic() +
 theme(legend.position = "none") +
 theme(panel.background = element_blank()) +
 scale_fill_brewer(palette = 'Greens') +   # customized color palette
 xlab(product1) +
 ylab(product2) +
 ggtitle("Correlation Scatter Plot (Pearson)") +
 theme(plot.title = element_text(hjust = 0.5, face = "bold"))

请注意:

  • 我写了colour = "black"
  • 我把scale_colour_brewer改成了scale_fill_brewer
  • 我写了 fill = year 而不是 colour = year

(因为看不到最后的结果所以加大了点数)