【问题标题】:Draw a vertical line on a histogram plotted by qplot在 qplot 绘制的直方图上画一条垂直线
【发布时间】:2015-08-02 01:29:54
【问题描述】:

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

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

hist distribution using ggplot2

【问题讨论】:

  • 可能是geom_segment

标签: r ggplot2


【解决方案1】:

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

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")

希望这会有所帮助。

【讨论】:

  • 如何将线段绘制到直方图的顶部,在这里,浅蓝色条的最大值可能是 9.75,但事先不知道这个值,即 yend 的最大值?
  • 不知何故,应该计算直方图计数的最大值以便用于yend。相当于my.hist= hist(my.vec, plot=F)max.count= max(my.hist$count)
猜你喜欢
  • 2015-11-01
  • 1970-01-01
  • 2018-03-12
  • 2015-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多