Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R:Inconsistent line thickness in geom_segment ggplot2

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

like image 850
Steve Avatar asked Jun 15 '16 20:06

Steve


1 Answers

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.

like image 57
Nate Avatar answered Oct 07 '22 17:10

Nate