在我的ggplot2图表中添加a geom_smooth()
和a时geom_vline()
,我在传奇中遇到了一些奇怪的行为.这是一个可重复的例子:
library(ggplot2)
n <- 60
set.seed(123)
df <- data.frame(Area = sample(c("A", "B", "C", "D"), size = n, replace = TRUE),
x = runif(n),
y = runif(n),
Type = sample(c("I", "II"), size = n, replace = TRUE),
Result = sample(c("K", "L", "M"), size = n, replace = TRUE))
df.breaks <- data.frame(Area = c("B", "C"), x = c(0.8, 0.3))
ggplot(df, aes(x = x, y = y)) +
geom_point(aes(colour = Result, shape = Type), size = 3) +
geom_smooth(aes(linetype = "Smooth"), colour = "green", se = FALSE) +
geom_hline(yintercept = 0.3) +
facet_wrap(~Area) +
geom_vline(data = df.breaks, aes(xintercept = x, linetype = "Break"), colour = "purple") +
scale_colour_manual(values = c("K" = "red", "L" = "orange", "M" = "blue")) +
scale_linetype_manual(name = "Lines", values = c("Break" = "dashed", "Smooth" = "solid"))
Run Code Online (Sandbox Code Playgroud)
正如您将注意到的"线条"图例在每个项目中都有垂直线和水平线,在第一种情况下有几条虚线,而在第二种情况下有几条实线.我正在尝试调整我的代码以生成一个图例,其中(1)水平绿线和旁边的一个键称为"Smooth",(2)一条垂直的紫色dahsed线,旁边有一个键叫做"Break".我会感激一些帮助,无论我尝试过什么(包括linetype
内/外aes()
等,或使用scale_linetype_identity()
,甚至override.aes
选项guides
),我都找不到合适的组合!
我搜索了类似的例子,即使我发现其他职位具有叠加竖线上colour
,fill
或者shape
等我coulnd't找到一个与一垂直线linetype
的传说,如矿山.任何帮助将深表感谢!谢谢!
对这个问题的答复迟来了,但我认为最好有一个答案,以防其他人(或未来的我!)对此感兴趣。
根据 @aosmith 在评论部分的建议以及他提供答案的鼓励,我最终将情节的代码替换为:
ggplot(df, aes(x = x, y = y)) +
geom_point(aes(colour = Result, shape = Type), size = 3) +
geom_hline(yintercept = 0.3) +
facet_wrap(~Area) +
geom_vline(data = df.breaks,
aes(xintercept = x, linetype = "Break"), colour = "purple") +
scale_colour_manual(values = c("K" = "red", "L" = "orange", "M" = "blue")) +
scale_linetype_manual(name = "Lines",
values = c("Break" = "dashed", "Smooth" = "solid")) +
geom_smooth(aes(fill = "Smooth"), method = "loess", colour = "green", se = FALSE) +
scale_fill_discrete(name = "")
Run Code Online (Sandbox Code Playgroud)
上面的最后两行替换了原始代码中的以下行:
geom_smooth(aes(linetype = "Smooth"), colour = "green", se = FALSE) +
。
生成的图表仍然需要一些工作,特别是图例间距,但它解决了最初的问题。
归档时间: |
|
查看次数: |
1107 次 |
最近记录: |