我有一些数据用于绘制直方图.我也有两组具有一定意义的阈值.
我可以用适当的样式绘制直方图和vlines.但是,我无法让我的vlines显示在图例中.我相信这样的东西应该有用,但是传奇项目不会显示.
df <- data.frame(val=rnorm(300, 75, 10))
cuts1 <- c(43, 70, 90)
cuts2 <- c(46, 79, 86)
ggplot(data=df, aes(x=val)) +
geom_histogram() +
geom_vline(xintercept=cuts1,
linetype=1,
color="red",
labels="Thresholds A",
show_guide=TRUE) +
geom_vline(xintercept=cuts2,
linetype=2,
color="green",
labels="Thresholds B",
show_guide=TRUE)
Run Code Online (Sandbox Code Playgroud)
或者,如果我为剪切构建一个data.frame并进行美学映射,我可以让我的vlines显示在图例中.不幸的是,这个图例给了我两个不同线型叠加在一起的实例:
cuts1 <- data.frame(Thresholds="Thresholds A", vals=c(43, 70, 90))
cuts2 <- data.frame(Thresholds="Thresholds B", vals=cuts2 <- c(46, 79, 86))
ggplot(data=df, aes(x=val)) +
geom_histogram() +
geom_vline(data=cuts1, aes(xintercept=vals, shape=Thresholds),
linetype=1,
color="red",
labels="Thresholds A",
show_guide=TRUE) +
geom_vline(data=cuts2, aes(xintercept=vals, shape=Thresholds),
linetype=2,
color="green",
labels="Thresholds B",
show_guide=TRUE)
Run Code Online (Sandbox Code Playgroud)
所以,最后,我正在寻找的,是最简单的方法,手动将两组线添加到绘图中,然后让它们在图例中正确显示.
jor*_*ran 23
诀窍是将阈值数据全部放在同一个数据框中,然后映射美学,而不是设置它们:
cuts <- rbind(cuts1,cuts2)
ggplot(data=df, aes(x=val)) +
geom_histogram() +
geom_vline(data=cuts,
aes(xintercept=vals,
linetype=Thresholds,
colour = Thresholds),
show_guide = TRUE)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12461 次 |
最近记录: |