7

I am trying to some line segments on the bottom of a histogram using ggplot2, overlaying a couple of line segments to indicate relevant parts of a distribution. I would like the green segment to be twice as thick as the gray segment, but the green segment always seems to be five times the thickness of the gray segment. Here's some toy data and my ggplot2 code:

library(ggplot2)
x<- as.data.frame(rnorm(1000, mean=50))
colnames(x) <- c("values")

ggplot(x, aes(x=values)) +
geom_histogram (binwidth=1,position="identity", col="black")+
 theme_classic(base_size=18)+
theme(axis.line.x = element_line(colour = "black"),
axis.line.y = element_line(colour = "black"))+
geom_segment(aes(y=-10, yend=-10, col=I("darkgray"), size=.1,x=1, xend=100),show.legend=F)+
geom_segment(aes(y=-10, yend=-10, col=I("palegreen4"), size=.2,x=25,xend=75), show.legend=F)

And my result: enter image description here

1 Answer 1

13
ggplot(x, aes(x=values)) +
  geom_histogram(binwidth=1, position="identity", col="black") +
  theme_classic(base_size=18) +
  geom_segment(aes(y=-10, yend=-10,x=1, xend=100), colour = "grey", size=1, show.legend=F) +
  geom_segment(aes(y=-10, yend=-10, x=25,xend=75), colour = "green", size = 2, show.legend=F)

Try moving size outside of you aes() call. Only variables that will change with the data need to be inside of aes(), so colour can come out too. I cleaned out your theme_axis adjustments because I didn't notice an appreciable effect on the final plot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.