问题描述
我用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
(我增加点数的原因仅在于看不到最终结果)