How to Customize the Order of Alphabetically Sorted Variables in R Studio?

How to Customize the Order of Alphabetically Sorted Variables in R Studio?


One of the problems that can occur when drawing graphs using R is that the x-axis of the graph is sorted alphabetically. There is data as shown above. I used this data to create a graph as shown below.

Fertilizer=rep(c("Urea","Calcium Nitrate","Sodium Nitrate","Amonium Sulfate"),each=2)
Location=rep(c("A","B"),time=4)
Yield=c(100,120,90,100,80,95,130,145)
std_error=c(8.3,8.2,7.8,9.8,6.4,4.2,5.9,4.1)
dataA=data.frame(Fertilizer,Yield,Location, std_error)
dataA

       Fertilizer Yield Location std_error
1            Urea   100        A       8.3
2            Urea   120        B       8.2
3 Calcium Nitrate    90        A       7.8
4 Calcium Nitrate   100        B       9.8
5  Sodium Nitrate    80        A       6.4
6  Sodium Nitrate    95        B       4.2
7 Amonium Sulfate   130        A       5.9
8 Amonium Sulfate   145        B       4.1

library(ggplot2)
ggplot(data=dataA, aes(x=Fertilizer, y=Yield, fill=Location)) +
  geom_bar(stat="identity",position="dodge", width = 0.5) +
  geom_errorbar(aes(ymin=Yield-std_error, ymax=Yield+std_error), 
                position=position_dodge(0.5) ,width=0.2) +
  scale_y_continuous(breaks=seq(0,150,50), limits = c(0,150)) +
  scale_fill_manual(values=c("grey15","grey75"), name="Location") +
  theme_grey(base_size=18, base_family="serif")+
  theme(legend.position="bottom",
        axis.line= element_line(linewidth=0.5, colour="black"))+
  windows(width=7, height=5)

The above graph displays Amonium Sulfate first according to alphabetical order. However, I want to show the treatments in the following order: Calcium Nitrate first, followed by Urea, then Sodium Nitrate, and lastly Ammonium Sulfate.

Here are two ways to change the order of variables.



1. Using scale_x_discrete()

The first method is to use the scale_x_discrete() function. To do so, simply insert the following code into the existing code:

scale_x_discrete(limits=c("Calcium Nitrate","Urea","Sodium Nitrate","Amonium Sulfate"))


2. Using factor() code

Before running ggplot, you can change the variable order using the following simple code:

dataA$Fertilizer=factor(dataA$Fertilizer, levels=c("Calcium Nitrate","Urea","Sodium Nitrate","Amonium Sulfate")) 

For more detailed information on this topic, please refer to the following post:

How to reorder variables in R for data analysis?



Extra tip!! How to change legend order?

If you want to change the order of the legend, you can do so by specifying the order of the factor levels. For example, to make Location B appear first in the legend, you can use the factor() function to change the order of the levels.

dataA$Location=factor(dataA$Location, levels=c("B","A"))
# Full code
# to generate dat
Fertilizer=rep(c("Urea","Calcium Nitrate","Sodium Nitrate","Amonium Sulfate"),each=2)
Location=rep(c("A","B"),time=4)
Yield=c(100,120,90,100,80,95,130,145)
std_error=c(8.3,8.2,7.8,9.8,6.4,4.2,5.9,4.1)
dataA=data.frame(Fertilizer,Yield,Location, std_error)

# to change variable order
dataA$Fertilizer=factor(dataA$Fertilizer, levels=c("Calcium Nitrate","Urea","Sodium Nitrate","Amonium Sulfate")) 
dataA$Location=factor(dataA$Location, levels=c("B","A"))

# to make a graph
library(ggplot2)
ggplot(data=dataA, aes(x=Fertilizer, y=Yield, fill=Location)) +
  geom_bar(stat="identity",position="dodge", width = 0.5) +
  geom_errorbar(aes(ymin=Yield-std_error, ymax=Yield+std_error), 
                position=position_dodge(0.5) ,width=0.2) +
  scale_x_discrete(limits=c("Calcium Nitrate","Urea","Sodium Nitrate","Amonium Sulfate"))+
  scale_y_continuous(breaks=seq(0,150,50), limits = c(0,150)) +
  scale_fill_manual(values=c("grey15","grey75"), name="Location") +
  theme_grey(base_size=18, base_family="serif")+
  theme(legend.position="bottom",
        axis.line= element_line(linewidth=0.5, colour="black"))+
  windows(width=7, height=5)


Comments are closed.