0

I'm trying to plot mean values for species although the mean values are all negative. I want the more smaller values (more negative) to be towards the bottom of the y axis with the larger values (less negative) to be higher up on the y axis.

I've tried changing coord_cartesian and ylim and neither work.

ggplot(meanWUE, aes(x = Species, y = mean, fill = Species)) + 
 coord_cartesian(ylim = c(-0.8, -0.7)) +
 scale_fill_manual( values c("EUCCHR" = "darkolivegreen2","ESCCAL" = "darkgoldenrod2", "ARTCAL" = "darkcyan", "DEIFAS" = "darkred", "ENCCAL" = "darkorchid2", "SALMEL" = "deepskyblue1", "ERIFAS" = "blue3", "BRANIG" = "azure3", "PHAPAR"= "palevioletred" )) + 
 scale_y_reverse() + 
 geom_bar(position = position_dodge(), stat="identity") +
 geom_errorbar(aes(ymin=mean-se, ymax=mean+se),width=.3) +
 labs(x="Species", y="WUE")+ 
 theme_bw() + 
 theme(panel.grid.major = element_blank(), legend.position = "none")

I want ESCCAL and EUCCHR to be the shortest bars essentially, but currently they're being shown as the tallest.

Species vs water use efficiency

enter image description here

If I don't do scale_y_reverse, I get a plot that looks like this second image

Collin
  • 11
  • 2
  • Please add reproducible minimal sample data (e.g. using `dput`). At the moment we have nothing to work with, which makes answering questions like "I did XYZ but it didn't work" very difficult. – Maurits Evers May 09 '19 at 22:31
  • https://stackoverflow.com/questions/35324892/ggplot2-setting-geom-bar-baseline-to-1-instead-of-zero – Jon Spring May 09 '19 at 22:33
  • Not sure how this can work unless you scale or transform the values. If values are all negative then the most negative is the longest bar, by definition. Perhaps a bar isn't the best geom? Points will place less-negative values higher. – neilfws May 09 '19 at 22:47
  • I am sorry I am very new to R and am using it for a research project. My advisor told me this, "I think the problem seems to be with your new Figure 1, because you need to flip the y-axis so that the delta values go from lowest (most negative) to highest (least negative). The axis should start at -0.8 or -0.85 and go to -0.75. Then ESCCAL and EUCCHR will have the shortest bars, indicating the lowest WUE values. Does that make sense?" But it doesn't make sense and I haven't found a way to do it.... – Collin May 09 '19 at 23:21

1 Answers1

0

One approach is to shift all the numbers to show their value over a baseline, and then adjust the labeling the same way:

df <- data.frame(Species = LETTERS[1:10],
                 mean = -80:-71/100)

ggplot(df, aes(x = Species, y = mean, fill = Species)) + 
  geom_bar(position = position_dodge(), stat="identity")

enter image description here

Here we shift the values to show them against a new baseline. Then we can show larger numbers as larger bars the way we'd normally expect for positive numbers. At the same time, we change the labels on the y axis so they correspond to the original values. So -0.8 becomes +0.1 vs. a baseline of -0.9. But we adjust the labels too, so that adjusted 0 has a label of -0.9, and adjusted +0.1 has a label of -0.8, its original value.

baseline <- -0.9
ggplot(df, aes(x = Species, y = mean - baseline, fill = Species)) + 
  geom_bar(position = position_dodge(), stat="identity") +
  scale_y_continuous(breaks = 0:100*0.02,
                     labels = 0:100*0.02 + baseline, minor_breaks = NULL)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53