I need to create a bar plot that has the Y axis in a particular order. I need the Y axis to be ordered small, medium, unlimited. When I run the code it keeps ordering it medium, small, unlimited.
All of the resources online are for ordering the Y axis by decsending/ascending order or weekday/month.
SummarypH <- summarySE(df, measurevar ="pH_H",
groupvars=c("pit_size"), na.rm = TRUE)
arrange(pit_size) %>%
+ mutate(pit_size = factor(pit_size, levels= c("small","medium", "unlimited"))
p<-ggplot(SummarypH, aes(x=pit_size, y=pH_H)) +
geom_bar(position=position_dodge(), stat="identity", fill="darkgreen") +
geom_errorbar(aes(ymin=pH_H-se, ymax=pH_H+se),
width=.2, # Width of the error bars
position=position_dodge(.9)) + ylab("pH") +
xlab("Soil Area") + theme_bw()
p
I have tried the re-order function and createing a factor but nothing is working.
To sort that categoricals for a bar chart, make the variable (pit_size) a factor with the levels in the order in which you want them to appear.
SummarypH$pit_size <- factor(SummarypH$pit_size, levels = c("small", "medium", "unlimited"))
for example:
library(ggplot2)
library(ggiraphExtra)
df <- data.frame(pH_H = runif(30, 4, 8),
pit_size = sample(c("small", "medium", "unlimited"), 30, replace = TRUE))
SummarypH <- summarySE(
df,
measurevar = "pH_H",
groupvars = c("pit_size"),
na.rm = TRUE
)
SummarypH$pit_size <- factor(SummarypH$pit_size, levels = c("small", "medium", "unlimited"))
ggplot(SummarypH, aes(x = pit_size, y = pH_H)) +
geom_bar(position = position_dodge(),
stat = "identity",
fill = "darkgreen") +
geom_errorbar(aes(ymin = pH_H - se, ymax = pH_H + se),
width = .2,
position = position_dodge(.9)) +
ylab("pH") +
xlab("Soil Area") +
theme_bw()