0

I'm struggling with my ggplot2 stacked barplot. I want to define the order of the bars manually. So I do that normally by transforming the variable into a factor and defining the levels in my desired order.

data <- transform(data, variable = factor(variable, levels =  c("A4 Da/De/Du", "A2 London", "A3 Berlin", "A1 Paris", "A5 Rome")))

When I check my variable levels I can see that the levels are in my desired order to plot

head(data$variable)

When I plot my data everything looks as desired, but somehow, and I have no clue why, one variable (for example "A4 Da/De/Du") is not in my defined variable order...

Has someone an idea what the problem could be? -It's the only variable with special characters (e.g "/") in it -It's the only variable which has zero levels in it (e.g. c(20,40,0,0,40)) -My ggplot code is quite complex, and I use the "reorder()" function, and I use the "forcats" package in my ggplot2 code. Could that be a problem?

Thanks very much for any help or ideas!

EDIT (some example data)

library(reshape2)
library(ggplot2)
library(dplyr)

df <- data.frame(cbind(a=c(20,40,20,10,10),b=c(10,30,50,5,5), c=c(60,10,10,15,5), d=c(80,20,0,0,0), e=c(50,10,10,15,15)))
colnames(df) <- c("D1 Paris", "D2 London", "D3 Berlin", "D4 Da/De/Du", "D5 Rome")
df$category <- c("C1", "C2", "C3", "C4", "C5")

data <- data %>% group_by(variable) %>% arrange(variable)
data <- melt(data)
data$percent <-  data$value/100
data <- transform(data, variable = factor(variable, 
                                            levels = c("D4 Da/De/Du", "D2 London", "D3 Berlin", "D1 Paris", "D5 Rome")))

And the short version of the ggplot2 code:

ggplot(data, aes(x=reorder((variable), percent), y=percent, fill=category)) +
coord_flip()+
geom_bar(stat="identity", width = .4, colour="black", lwd=0.1) 

SOLUTION

I finally solved my problem :) Gregor was right, after transforming the levels of the specific variable in the desired order, the reorder() function in ggplot2 is no longer necessary respectively overwrites the earlier defined levels, what at the end produced my error...

Thanks Gregor!

JonesRu
  • 1
  • 1
  • 2
  • 2
    This is a good start, but can you [make it reproducible](https://stackoverflow.com/q/5963269/903061)? Sharing some data with `dput` will help us all understand what's going on as well as a simplified version of your plotting code. To understand and illustrate the problem, edit your code taking out as much as you possibly can and still see the problem. – Gregor Thomas Jun 24 '17 at 14:29
  • 1
    Looking at `head(data$variable)` will only check the row order, not the order of factor levels. Look at `levels(data$variable)` for that. Depending on what you mean by "not in my defined order" it could be a discrepancy between the level name you specified in `levels = ` and the actual data. Also, `reorder` changes the order of the levels, and the `forcats` package is made for changing factors conveniently, so both of those could affect the level order and hence the bar order. – Gregor Thomas Jun 24 '17 at 14:32
  • Hi Gregor, thanks! Some example code: `library(reshape2) library(ggplot2) df <- data.frame(cbind(a=c(20,40,20,10,10),b=c(10,30,50,5,5), c=c(60,10,10,15,5), d=c(80,20,0,0,0), e=c(50,10,10,15,15))) colnames(df) <- c("D1 Paris", "D2 London", "D3 Berlin", "D4 Da/De/Du", "D5 Rome") df$category <- c("C1", "C2", "C3", "C4", "C5") df_melt <- melt(df) df_melt <- transform(df_melt, variable = factor(variable, levels = c("D4 Da/De/Du", "D2 London", "D3 Berlin", "D1 Paris", "D5 Rome")))` – JonesRu Jun 24 '17 at 14:44
  • 1
    Could you edit your original post and update your code? It is difficult to read the code in your comment. – www Jun 24 '17 at 15:09

0 Answers0