Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fix the middle category in ggplot

I have a bar graph with 3 groups, each with 3 fill categories. I want one category to always be the middle category on the bar graph.

dat <- data.frame(fill=c("Category A","Category B","Middle",
                      "Category A","Category C","Middle",
                      "Category B","Category C","Middle"),
                  x=c("A","A","A",
                      "B","B","B",
                      "C","C","C"),
                  y=c(.333,.333,.333,
                      .4,.4,.2,
                      .2,.4,.4))

ggplot(dat,aes(x=x,y=y,fill=fill))+
  geom_col()

I don’t think this is possible with traditional factor ordering because I can’t set the Middle category to be the middle of each combination:

dat$fill <- factor(dat$fill,levels=c("Category A","Middle","Category B","Category C"))

If I do this, Middle will fall between Category A and Category B, but not between Category B and Category C.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Is it possible to set one category to always be the middle category?

>Solution :

Here is a way to achieve this using dplyr:

library(dplyr)

dat %>% group_by(x) %>% 
  mutate(n = ifelse(fill=="Middle", 2, c(1,3)))%>%
  ggplot(.,aes(x=x,y=y,fill=fill, group=n))+
  geom_col()

enter image description here

The mutate will add the column you can use for your group ranked by order. The ifesle will give the row with "Middle" 2, and when false 1 and 3. In this case it is convenient as each group have 3 categories. This part will need change if you have more categories per group.

dat %>% group_by(x) %>% 
  mutate(n = ifelse(fill=="Middle", 2, c(1,3)))

# A tibble: 9 x 4
# Groups:   x [3]
  fill       x         y     n
  <chr>      <chr> <dbl> <dbl>
1 Category A A     0.333     1
2 Category B A     0.333     3
3 Middle     A     0.333     2
4 Category A B     0.4       1
5 Category C B     0.4       3
6 Middle     B     0.2       2
7 Category B C     0.2       1
8 Category C C     0.4       3
9 Middle     C     0.4       2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading