0

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))

enter image description here

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 orderas 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!

Jude
  • 153
  • 2
  • 8
  • Changing the y-axis should work just the same as changing the x. The side of the plot an axis is on doesn't really matter to its construction in `ggplot` – camille Aug 13 '18 at 13:59
  • Respect the order of *levels* for factors (rather than the order of values) - a reproducible example would help you out – CPak Aug 13 '18 at 14:07

1 Answers1

0

As others have mentioned please provide a reproducible question so that someone can give you the best possible answer. Here you can find more on that.


Coming to your question as far as I understand you need to change your Y-axis values to factor with proper levels to see the desired effect. Here is a little demo on that.

library(ggplot2)
library(dplyr)

test_data <- data_frame(col_a = c("a1","a2","a3"), col_b = c(1,3,5))

# this is what you seem to have for now
# x values are continuous and have a "sense" of "order"
# y values are charactors without order
# you have already converted one of the variables into factor need to do the same here as well
# let us say we need  'a1' top and not in the bottom
# we need to convert 'col_a' to a factor with levels = a3 < a2 < a1
test_data %>%
  ggplot(data =., aes(col_a,col_b)) +
  geom_bar(stat = "identity") +
  coord_flip()

enter image description here

# there are multiple ways to convert to a factor type, but this is what came to my mind
test_data$col_a <- factor(c("a1","a2","a3"),levels = rev(c("a1","a2","a3")), ordered = T)

test_data$col_a

[1] a1 a2 a3
Levels: a3 < a2 < a1

# now the plot starts with 'a1' instead of 'a3'
test_data %>%
  ggplot(data =., aes(col_a,col_b)) +
  geom_bar(stat = "identity") +
  coord_flip()

enter image description here

Suhas Hegde
  • 366
  • 1
  • 6
  • 13
  • structure(list(An = c(1950, 1951, 1952, 1953, 1954, 1955), Allie_Qobs = c(0, 0, 0, 0, 0, 0), Black_Qobs = c(0, 0, 0, 0, 0, 0), Ferno_Qobs = c(0, 0, 0, 0, 0, 0), Obyån_Qobs = c(0, 0, 0, 0, 0, 0), Rimba_Qobs = c(0, 0, 0, 0, 0, 0), Ferso_Qobs = c(0, 0, 0, 0, 0, 0), Ourth_Qobs = c(0, 0, 0, 0, 0, 0)), .Names = c("An", "Allie_Qobs", "Black_Qobs", "Ferno_Qobs", "Obyån_Qobs", "Rimba_Qobs", "Ferso_Qobs", "Ourth_Qobs" ), row.names = c(NA, 6L), class = "data.frame") – Jude Aug 22 '18 at 15:43
  • Hi, sorry for the delay and thanks for your replies, here is a dput of my data, if I understand well, it's enough for you to see how it works. By the way it's data frames in a list, and as I understood it's not the best format to work with ggplot but I didn't find another way to do that yet. – Jude Aug 22 '18 at 15:49
  • After looking, it might be because my `scale_y_discrete` is from another object (_liste_BV_) and not from the same data frame that I plot (_tab[[2]]_), I didn't manage to put the names in the same order as it is plotted... – Jude Aug 22 '18 at 15:59