How do you specifically order the ggplot2 x axis instead of alphabetical order?

I am trying to make heatmap using ggplot2 using the geom_tiles function geom_tiles here is my code below:

 p<-ggplot(data,aes(Treatment,organisms))+geom_tile(aes(fill=S))+ scale_fill_gradient(low = "black",high = "red") + scale_x_discrete(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) + theme(legend.position = "right", axis.ticks = element_blank(), axis.text.x = element_text(size = base_size, angle = 90, hjust = 0, colour = "black"), axis.text.y = element_text(size = base_size, hjust = 1, colour = "black")). 

- my data.csv file
my x axis - types of treatment
my Y axis - species of organisms

I'm not too familiar with commands and programming, and I'm relatively new to this. I just want to indicate the order of the labels on the x axis. In this case, I am trying to indicate the order of "treatment". By default, it is ordered alphabetically. How do I override this / save the data in the same order as in my csv source file?

I tried this command

 scale_x_discrete(limits=c("Y","X","Z")) 

where x, y and z are my order of processing conditions. However, it does not work very well, and I do not have enough heat boxes.

+55
r r-faq ggplot2
Oct 08 '12 at 1:10
source share
2 answers

It is difficult to answer your specific question without a complete, reproducible example. However, something like this should work:

 #Turn your 'treatment' column into a character vector data$Treatment <- as.character(data$Treatment) #Then turn it back into a factor with the levels in the correct order data$Treatment <- factor(data$Treatment, levels=unique(data$Treatment)) 

In this example, the order of the coefficient will be the same as in the data.csv file.

If you prefer a different order, you can order them manually:

 data$Treatment <- factor(data$Treatment, levels=c("Y", "X", "Z")) 

However, this is dangerous if you have many levels: if you made a mistake in any of them, it will cause problems.

+90
Oct 08
source share

Stumbled upon this answer due to a current duplicate question. The most acceptable answer offers a solution that requires changing the underlying data frame. It's not obligatory. You can also simply expand it inside the aes () call directly or create a vector instead.

This, of course, is not much different from @Drew Steen's answer, but with a significant difference in that you do not change the original data frame.

 level_order <- c('virginica', 'versicolor', 'setosa') #this vector might be useful for other plots/analyses ggplot(iris, aes(x = factor(Species, level = level_order), y = Petal.Width)) + geom_col() 

or

 level_order <- factor(iris$Species, level = c('virginica', 'versicolor', 'setosa')) ggplot(iris, aes(x = level_order, y = Petal.Width)) + geom_col() 



edit : or directly in aes () call without a previously created vector:

 ggplot(iris, aes(x = factor(Species, level = c('virginica', 'versicolor', 'setosa')), y = Petal.Width)) + geom_col() 

that's for the first version

+3
Feb 26 '18 at 22:53
source share



All Articles