R:使用coord_flip,ggplot2时如何使geom_vline图例键水平?

时间:2017-01-04 09:33:40

标签: r ggplot2 legend

这是我的第一个问题:

使用ggplot2

  • 我有一个用翻转轴坐标(coord_flip)制作的水平直方图
  • 我使用geom_vline添加了垂直底线(也被翻转为水平)
  • 结果的标题图例显示了键中的垂直线

问:图例键中是否可以有水平线?

enter image description here

library(ggplot2)

dat <- data.frame(vs=c(0,0,1,1),
                  stat = c("25%", "75%", "25%", "75%"),
                  value = c(quantile(mtcars$disp[mtcars$vs==0],0.25),
                            quantile(mtcars$disp[mtcars$vs==0],0.75),
                            quantile(mtcars$disp[mtcars$vs==1],0.25),
                            quantile(mtcars$disp[mtcars$vs==1], 0.75)))

p <- ggplot(mtcars, aes(disp)) +
coord_flip() +
facet_grid(vs~.) +
geom_histogram(aes(y = ..density..), stat="bin") +
geom_vline(mapping=aes(xintercept=value, colour=stat), linetype="solid", size=0.6, alpha=0.7, data=dat) +
guides(col = guide_legend(nrow = 2, title="Quantile")) +
theme(legend.position = "bottom")

p

0 个答案:

没有答案