I'm new to ggplot2 so there are things I still don't understand, sorry in advance for this.
I'm plotting with ggplot2 but my y
axis is not in the right order (i.e., the first column of the file is the last one to be plotted). Here is my code and the graph:
tab[[5]] %>%
gather(col_name, value, -An) %>%
ggplot(aes(factor(An), sort(col_name), fill = value == 1)) +
geom_tile(colour = 'black') +
scale_x_discrete(breaks=tab[[5]]$An, labels=substr(tab[[5]]$An, 3,4)) +
scale_y_discrete(labels=liste_BV$Nom_riviere) +
scale_fill_manual(values = c('white', "thistle"),
name="Disponiblité",
breaks=c("TRUE", "FALSE"),
labels=c("Oui", "Non")) +
xlab("Années") + ylab("Bassins versants") +
labs(fill = "Disponibilité") +
ggtitle(paste0("Données de ", var[[5]], " manquantes")) + theme(plot.title = element_text(hjust= 0.5))
I found other posts explaining how to transform the x
axis into factor, which is already done here and I don't think the y
axis needs to be.
I tried order
as said in other posts but the plot doesn't work at all.
The sort
in front of the col_name allows me to plot the right data with the right names of y
, otherwise it was mixed and the data and names didn't match (no idea why as well).
It should just be the other way around as the columns are in the object tab.
Thanks a lot for your help!