0

I wanted to order my y axis values and in doing so my errorbars no longer fit on the y axis. The code is below if i run just ggplot down I get error bars in the right place, if I run it all removing the Kale_Nutrients from the ggplot the error bars are displaced on the Y axis.

Kale_Nutrients %>%
  arrange(X) %>%
  mutate(X = factor(X, levels=c( "Control", "B1 <2mm 5%", "B1 <2mm 10%", 
            "B1 <2mm 20%", "B1 >2mm 5%", "B1 >2mm 10%", 
            "B1 >2mm 20%", "B2 <2mm 5%", "B2 <2mm 10%","B2 <2mm 20%", "B2 >2mm 5%", "B2 >2mm 10%", "B2 >2mm 20%", "B3 <2mm 5%", "B3 <2mm 10%", "B3 <2mm 20%", "B3 >2mm 5%", "B3 >2mm 10%", "B3 >2mm 20%"))) %>%
ggplot(Kale_Nutrients,aes(X, P))+
geom_point()+
theme_classic()+
theme(axis.text.x=element_text(angle=90, size=12, color="black"),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(),axis.line = element_line(colour = "black"),axis.text.y=element_text( size=14, color="black"),axis.title=element_text(size=14, face="bold"))+
geom_errorbar(ymin=Kale_Nutrients$P-Kale_Nutrients$P.s.e , ymax=Kale_Nutrients$P+Kale_Nutrients$P.s.e)+
ylim(0,4000)

Running without reordering the x axis

Running after reordering the x axis with the errorbars displaced on the y axis

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
  • I doont understand why you use `ggplot(Kale_Nutrients, aes...)` after the pipe instead of just `ggplot(aes...)` which is the whole point of the pipe system, or why you then again specify `Kale_Nutrients$...` even though it is already your active data for the plot. Just let ggplot take the data from the pipe, and remove the `Kale_Nutrients$` bits, that way everything would be consistently based on the processed data.frame. – cymon Sep 27 '21 at 08:49
  • I specified in the problem that I removed Kale_Nutrients when running the second time with the modifications to reorder the x axis. Thanks though – Charlotte Tutton Sep 28 '21 at 09:11
  • I saw it but found it ambiguous what exactly you removed since there are multiple places where the name comes up in the code. Removing the name only from the base ggplot call and not the errorbars call, would obviously cause a mismatched mix of reordered and non-reordered data. In any case, you didn't provide (a piece of) `Kale_Nutrients` to try code with so guesstimation based on principles was the only thing I could do. You might find this guide useful for future posts, particularly the portion about easily reproducible examples: https://stackoverflow.com/help/how-to-ask Happy coding! – cymon Sep 29 '21 at 09:22
  • Sorry that you found it ambiguous. It was not obvious to me hence I asked the question. I am only just starting out learning all this so what might be obvious to you won’t be obvious to those asking the question or they wouldn’t be asking it. I will look into the easily reproducible examples, perhaps creating dummy data would be the solution as I’m not comfortable sharing any of my research data – Charlotte Tutton Sep 30 '21 at 09:13
  • Absolutely, dummy data is fine, often even better than real data actually. You are right, it is impossible to know what the person knows or doesn't know, we make a guess and hope we don't grossly over- or under-estimate. Your confidence in comments made me think maybe I initially underestimated your knowledge. By the way, for my peace of mind, does my solution even work correctly? You've left me with the impression that it doesn't, which would then mean I have a significant gap in my understanding that I'd need to look into. – cymon Oct 01 '21 at 12:16

2 Answers2

0

You need to pull the values of the error bars from the dataset with the re-ordered factor levels, rather than the original Kale_Nutrients data frame. This would change your code for the error bars to something like:

geom_errorbar(aes(ymin=P-P.s.e , ymax=P+P.s.e))+
Miff
  • 7,486
  • 20
  • 20
0

I believe the problem is based on a misunderstanding/misuse of the pipe mechanism:

By specifying ggplot(Kale_Nutrients, ...) you are telling ggplot to use the original dataframe instead of the reordered one that you are piping to it (pipes craete a new object, they don't modify the original object unless explicitly assigned). In this case it makes no difference because you don't apply filters or modifications to the point values, but in another scenario it could cause you to plot the wrong/old point values.

Also, since the columns exist in the same dataframe as your point data, there is no need to specify the dataframe namee in the aesthetics of the error bar.

I believe the following will do what you need, by fixing both the problem you see, and the problem you don't see but would have if you did more modifications than reorder the factor:

Kale_Nutrients %>%
  arrange(X) %>%
  mutate(X = factor(X, levels=c( "Control", "B1 <2mm 5%", "B1 <2mm 10%", 
            "B1 <2mm 20%", "B1 >2mm 5%", "B1 >2mm 10%", 
            "B1 >2mm 20%", "B2 <2mm 5%", "B2 <2mm 10%","B2 <2mm 20%", "B2 >2mm 5%", "B2 >2mm 10%", "B2 >2mm 20%", "B3 <2mm 5%", "B3 <2mm 10%", "B3 <2mm 20%", "B3 >2mm 5%", "B3 >2mm 10%", "B3 >2mm 20%"))) %>%
ggplot(aes(X, P))+
geom_point()+
theme_classic()+
theme(axis.text.x=element_text(angle=90, size=12, color="black"),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(),axis.line = element_line(colour = "black"),axis.text.y=element_text( size=14, color="black"),axis.title=element_text(size=14, face="bold"))+
geom_errorbar(ymin=P-P.s.e , ymax=P+P.s.e)+
ylim(0,4000)
cymon
  • 413
  • 2
  • 9
  • I appreciate this , however I specified in the text that I removed the Kale_Nutrients from the ggplot. And the Miff already answered the question. – Charlotte Tutton Sep 28 '21 at 09:08
  • When I started typing, there were no responses. When I refreshed after posting, there were multiple. It's just how things work. – cymon Sep 29 '21 at 08:53