0

Probably a simple question, but I just cannot seem to find the answer: I would like to change the order of the stacked bars in my diagram. In the "semi-natural" group, the stacked bars shall be displayed "Forest", then "Shrubland", then "Grassland", from bottom to top. In the "cropland" group, the stacked bars shall be displayed "Mixed Cropland", "Rice-Wheat", "Sugarcane", from bottom to top. Thanks for your help!

data <- read_excel("C:/lulc_change_analysis.xlsx")

data1 <- data
data1$group <- factor(data1$group, levels = c("Semi-natural", "Water", "Cropland", "Urban"))


ggplot(data=data1, aes(x=year, y=value,fill=lulc)) + geom_bar(color = "black", stat="identity") +
  facet_grid(~group) +
  scale_fill_manual("legend", values = c("Forest" = "green4", "Shrubland" = "green3", "Grassland" = "chartreuse",
                                         "Water" = "blue", "Sugarcane" = "magenta", "Rice-Wheat" = "grey",
                                         "Mixed Cropland" = "yellow", "Urban" = "red")) +
  labs(y= "Percentage of catchment area (%)") +
  theme(axis.title = element_text(face="bold")) +
  theme(legend.title = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), panel.background = element_blank(),
        axis.line = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank(),
        axis.text.x = element_text(size=7))
Anna
  • 1
  • 1
  • 1
    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). – stefan Apr 14 '22 at 13:47
  • 3
    This said: Convert your `lulc` column to a factor with the levels set in your desired order as you did for your `group` column. – stefan Apr 14 '22 at 13:48

0 Answers0