r - ggplot2:在绘图和图例中用白色填充的线+点?

标签 r ggplot2 plot fill points

我想使用 ggplot2 包创建一个图,它结合了线和点。这些点应具有根据组指示器的颜色和形状。应创建一个图例,根据绘图显示颜色和形状。

这部分工作得很好。但是,所有点都应该有白色填充,但我找不到正确的代码。

谷歌搜索建议使用fill = "white",但这不起作用。

考虑以下示例数据和绘图:

library("ggplot2")

# Example data
df <- data.frame(y = 1:100,
                 x = 1:100,
                 group = as.factor(c(rep(1, 33), rep(2, 33), rep(3, 34))))

# Create plot --> fill = "white" doesnt work
ggplot(df, aes(x = x, y = y)) + 
  geom_line(aes(colour = factor(group, labels = c("a", "b", "c")))) +
  geom_point(aes(colour = factor(group, labels = c("a", "b", "c")),
                 shape = factor(group, labels = c("a", "b", "c"))),
             fill = "white") +              ##### This line is not working #####
  theme(legend.title = element_blank())

enter image description here

问题:如何用白色填充该图的点(在图和图例中)?

最佳答案

您可以使用scale_shape_discrete设置solid = FALSE:

ggplot(df, aes(x = x, y = y)) + 
  geom_line(aes(colour = factor(group, labels = c("a", "b", "c")))) +
  scale_shape_discrete(solid = F) +
  geom_point(aes(colour = factor(group, labels = c("a", "b", "c")),
                 shape = factor(group, labels = c("a", "b", "c")))) +              
theme(legend.title = element_blank())

enter image description here

关于r - ggplot2:在绘图和图例中用白色填充的线+点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54074070/

相关文章:

r - 从 UTC 时间获取本地时间

r-ggplot2 : connecting points in polar coordinates with a straight line

r - 更改 ggplot 中的配色方案并能够编辑图例

r - ggplot : aes vs aes_string,或如何以编程方式指定列名称?

r - ggplot aes_string 不适用于空格

r - 分发使用外部软件包的R脚本

r - 使用 Sweave 将源 R 文件打印到附录

r - 什么是连续 x 美学——你忘了 aes(group=...) 是什么意思吗?

R ggplot geom_jitter 重复异常值

python - matplotlib 颜色图 : set_bad doesn't have an impact with collections