在qplot绘制的直方图上绘制一条垂直线

时间:2015-08-01 16:17:53

标签: r ggplot2

在直方图上,我想绘制从y = 0开始到直方图条形的最大计数的虚线垂直线,而不是直方图的顶部,也从0开始,而不是低于0.我应该使用geom_ablinegeom_vline?假设我想在

中绘制的直方图上从x = .75,y = 0到x = .75到y = 9进行绘制

hist distribution using ggplot2

1 个答案:

答案 0 :(得分:2)

这是一种可能性,使用您提到的示例中的数据:

my.vec <-c(0.41, 0.42, 0.47, 0.47, 0.49, 0.50, 0.51, 0.55, 0.56, 0.57, 0.59, 0.61, 0.62, 0.65, 0.68, 0.69, 0.70, 0.75, 0.78, 0.79)
p <- qplot(my.vec,binwidth = .2) +  
    geom_histogram(binwidth = .2, aes(fill =..count..), colour='black') + 
    geom_segment(aes(x =.75, y = 0, xend = .75, yend = 9), linetype="dashed",  color="red")

enter image description here

希望这有帮助。