Home > Software engineering >  Rearranging order of X axis causes errorbars to no longer match up on y axis
Rearranging order of X axis causes errorbars to no longer match up on y axis

Time:09-27

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

CodePudding user response:

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)) 

CodePudding user response:

Miff answered this as follows and it works.

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))

CodePudding user response:

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)
  • Related