0

I have the following input data.

sample_data <- data.frame(city = c("a", "b", "c","d", "a", "b", "c", "d"), value = c(10, 11, 17, 12, 13, 14, 11, 8), type = c(1,1,1,1,2,2,2,2), country = c("c1", "c2", "c1", "c1", "c2", "c2", "c1", "c1")) 

And want to create plots within ggplot that splits the data by type (so two sets of bar charts). I want the order of the bar charts to be grouped by the colours together. So below the right hand chart would group the blue and red bars to be next to each other. I have a large number of variables so manually moving them around would not be an option. The code I used for the charts was:

sample_data <- sample_data %>% mutate(city2 = factor(city, levels=city[order(country)])) ggplot(sample_data) + geom_col(aes(x=city2, y=value, colour=country, fill=country)) + facet_wrap(~type) 

enter image description here

6
  • Redefining the levels of your factor variable you can change the order of the elements. I don't understand well your second question. Commented Sep 27, 2017 at 8:18
  • I don't want to manually move the bar charts (as I have a large number of variables) but rather have a way to automatically group the country colours together. What part don't you understand? Commented Sep 27, 2017 at 8:31
  • Changing type by country in the facet_wrap() function, do you get what you want? ggplot(sample_data) + geom_col(aes(x=city2, y=value, colour=country, fill=country)) + facet_wrap(~country) Commented Sep 27, 2017 at 10:21
  • Unfortunately not. I still want the facet_wrap to by type. Just the order of City to be as such that the same bar colours are next to each other. For example in the above the top left chart is correct the right (should be blue blue red red or red red blue blue and not blue red red blue) Commented Sep 27, 2017 at 10:46
  • 1
    Sounds like you're asking for a potentially different order on the x-axis in each facet. Is that right? Commented Sep 27, 2017 at 10:54

1 Answer 1

1

I note that in different facets, the same city (x-axis variable) may be associated with different fill colours, which suggests that the order of variables could be different in each facet, in which case specifying its factor order won't work.

Here's a workaround that specifies the x-axis order based on the fill variable, & adds a fake x-axis to the plot:

library(dplyr) sample_data <- sample_data %>% group_by(type) %>% arrange(country) %>% mutate(x = row_number()) %>% ungroup() ggplot(sample_data, aes(x = x, y = value, fill = country, label = city)) + geom_col() + geom_text(aes(y = 0), nudge_y = -1) + # fake x-axis facet_wrap(~type) + theme(axis.text.x = element_blank(), # hide x-axis labels axis.ticks.x = element_blank(), # hide x-axis tick marks panel.background = element_blank()) # white background for easier reading 

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.