我想使用 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())
问题:如何用白色填充该图的点(在图和图例中)?
最佳答案
您可以使用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())
关于r - ggplot2:在绘图和图例中用白色填充的线+点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54074070/