Agus camacho
Agus camacho

Reputation: 898

control color in horizontal lines in ggplot2

anybody could tell me what is going wrong with this script? I need 2 horizontal, black, dashed lines, but i got two red continuous instead. I am also unable to change the color of the plot margins to black, despite being using theme_bw, and also the fill of the boxplot is not grey,as required.

  dat1 <- data.frame (xvar = rep(c("A", "B"), each=10),

                yvar = 1:20 + rnorm(20,sd=3))

  ggplot(dat1, aes(x=xvar, y=yvar)) +
  theme_bw()+
  geom_boxplot(fill=grey)+
  geom_hline(aes(yintercept=40, color="black", linetype="dashed"))+
  geom_hline(aes(yintercept=33.84, color="black", linetype="dashed"))+  
  scale_x_discrete(name="") +
  scale_y_continuous(name="temperature (°C)")+
  opts(
    panel.grid.major = theme_line(size = 0.5, colour = NA),
    panel.background = theme_rect(colour = NA),   
    axis.title.y = theme_text(angle=90,face="bold", colour="black", size=14),
    axis.text.y  = theme_text(face="bold",angle=0, size=14,colour="black"),
    axis.title.x = theme_text(face="bold", colour="black", size=14),
    axis.text.x  = theme_text( size=14,vjust=1.2, colour=NA))

thanks a lot!

Upvotes: 12

Views: 29216

Answers (1)

Sam
Sam

Reputation: 4507

Regarding the black dashed line, you should define it outside of the aes(). Try the code below:

geom_hline(aes(yintercept=40), color="black", linetype="dashed")

Regarding the box plot, you should correct your code to the one below:

geom_boxplot(fill="gray")

And finally, to get the black margin, note that you are setting the margin to have NA color in your opts(..., panel.background = theme_rect(colour = NA),...). To solve the issue try this:

panel.background = theme_rect(colour = "black")

Hope my comment helps.

Upvotes: 25

Related Questions