0

I want to arrange the categories in the x axis of my heatmap in the order as in the excel file's Term order. But it automatically gets arranged in alphabatic order.

library(ggplot2)
library(reshape)
getwd()
x<-read.csv("R.csv",check.names = F)

y<-melt(x,id="Term")

ggplot(y,aes(variable, Term,fill=value))+geom_tile()+coord_flip()+
  scale_fill_manual(values = c("#FFE5CC","#FF9934"),guide = guide_legend(reverse = TRUE))+                   
  theme(legend.title = element_text(color = "black", size = 10),axis.title=element_blank(),
        axis.text.x = element_text(angle = 90,vjust = .5,hjust = 1,color = "black",size="7"),
        axis.text.y = element_text(color = "black",size="12"))+
  scale_x_discrete(expand=expansion(0),limits=rev) +
  scale_y_discrete(expand=expansion(0))

Heatmap

csv file

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. Please do not post an image of code/data/errors [for these reasons](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). Just include the code, console output, or data (e.g., dput(head(x)) or data.frame(...)) directly. – stefan Sep 07 '22 at 19:07
  • This said: If you want a specific order you have to convert to a factor with the levels set according to your desired order. In your case you could try with `x$Term <- forcats::fct_inorder(x$Term)` before reshaping your data via `melt`. – stefan Sep 07 '22 at 19:09

0 Answers0