0

I am trying to create a graph of value ranges so that they can be compared to a unique range (represented by a gray rectangle in the graph). I got the graph to work just like I wanted to it but I cannot effectively change the line weight of the error bars that I am using to represent the value ranges. The code it below with example figures.

### Graph with default line weights   
ggplot(data=droplevels(wght2[which(wght2$Zone!="Oahu"),]), aes(x=wght2$num[wght2$Zone!="Oahu"], y=(wght2[which(wght2$Zone!="Oahu"),4]+wght2[which(wght2$Zone!="Oahu"),4+3])/2, 
        group= wght2[which(wght2$Zone!="Oahu"),2], color= wght2[which(wght2$Zone!="Oahu"),2])) + 
    geom_errorbar(aes(ymin=wght2[which(wght2$Zone!="Oahu"),4], ymax=wght2[which(wght2$Zone!="Oahu"),4+3], width=0.3, color=wght2[which(wght2$Zone!="Oahu"),2])) +
    geom_rect(ymin=wght2[8,4], ymax=wght2[8,4+3], xmin=0.5, xmax=6+0.5, fill="gray25", linetype=0, alpha=0.07)+
    scale_color_manual(values=c("forestgreen","deepskyblue3","gold"))+
    scale_x_continuous(labels=levels(wght2$Zone), breaks=c(1,2,3.5,5.5))+
    labs(x="Zone",y=measure3[4]) +
    theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        legend.text = element_text(size = 16),
        legend.title = element_text(size=16),
        axis.text = element_text(size = 16,colour="black"),
        axis.title = element_text(size = 18)) +
    theme(legend.position="topright")

### My attempt to change line weights
ggplot(data=droplevels(wght2[which(wght2$Zone!="Oahu"),]), aes(x=wght2$num[wght2$Zone!="Oahu"], y=(wght2[which(wght2$Zone!="Oahu"),4]+wght2[which(wght2$Zone!="Oahu"),4+3])/2, 
        group= wght2[which(wght2$Zone!="Oahu"),2], color= wght2[which(wght2$Zone!="Oahu"),2])) + 
    geom_errorbar(aes(ymin=wght2[which(wght2$Zone!="Oahu"),4], ymax=wght2[which(wght2$Zone!="Oahu"),4+3], width=0.3, size=0.5, color=wght2[which(wght2$Zone!="Oahu"),2])) +
    geom_rect(ymin=wght2[8,4], ymax=wght2[8,4+3], xmin=0.5, xmax=6+0.5, fill="gray25", linetype=0, alpha=0.07)+
    scale_color_manual(values=c("forestgreen","deepskyblue3","gold"))+
    scale_x_continuous(labels=levels(wght2$Zone), breaks=c(1,2,3.5,5.5))+
    labs(x="Zone",y=measure3[4]) +
    theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        legend.text = element_text(size = 16),
        legend.title = element_text(size=16),
        axis.text = element_text(size = 16,colour="black"),
        axis.title = element_text(size = 18)) +
    theme(legend.position="topright")

enter image description hereenter image description here

As you can see from my code when I include the 'size' argument in the 'aes' function of geom_errorbar, the line weights become huge even though I set it at the default which is 0.5.

Any advice on how to change the line weights in geom_errorbar would be much appreciated. I know I could just draw each line in with geom_segment but feel like using geom_errorbar should work and want to know why it is not. Thanks!

camille
  • 16,432
  • 18
  • 38
  • 60
Jason
  • 381
  • 3
  • 2
  • 4
    If you want to define the line weight to a constant you need to put `size` outside `aes()`. – aosmith Jul 11 '18 at 15:45
  • In addition to taking `size` outside of `aes`, since you aren't mapping a variable to the line size, you should also not be referring to the name of your data frame inside any `ggplot` functions once that data frame has been declared. It's a quick way to introduce bugs to your code. – camille Jul 11 '18 at 16:07
  • To get much advice beyond that, you'll need to make this a [reproducible question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), such as posting a representative sample of data and paring down the code that we don't need in order to recreate it (like all the theme adjustments) – camille Jul 11 '18 at 16:08
  • @aosmith Thanks! That was exactly it. I can't believe I did not think about that. – Jason Jul 11 '18 at 16:23

0 Answers0