carol
carol

Reputation: 171

Draw a vertical line on a histogram plotted by qplot

On A histogram, I want to draw a dashed vertical line starting from y=0 to the max count of bars of a histogram and not to the top of the histogram and also starting from 0 and not below 0. Should I use geom_abline or geom_vline? Suppose I want to draw from x=.75, y= 0 to x=.75 to y = 9 on a histogram plotted in

hist distribution using ggplot2

Upvotes: 1

Views: 1268

Answers (1)

RHertel
RHertel

Reputation: 23798

Here is one possibility, using the data from the example that you mentioned:

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

Hope this helps.

Upvotes: 3

Related Questions