Home > Software engineering >  Question about ggplot2 bar graph transformation method
Question about ggplot2 bar graph transformation method

Time:06-24

I wanted the barplot to appear in two forms, so I created repeated data and used it as an input. So I used the data in the form below.

I put the data in the form above and wrote the following code to use it.

Select <- "Mbp"

if(Select == "Mbp"){
  Select <- "Amount of sequence (Mbp)"
} else if (Select == "Gbp"){
  Select <- "Amount of sequence (Gbp)"
}


ggplot(G4, aes(x = INDV, y = Bp, fill = Group))   theme_light()  
  geom_bar(stat = 'identity', position = 'dodge', width = 0.6)   coord_flip()   
  scale_x_discrete(limits = rev(unname(unlist(RAW_TRIM[1]))))  
  scale_fill_discrete(breaks = c("Raw data","Trimmed data")) 
  scale_y_continuous(labels = scales::comma, position = "right")  
  theme(axis.text = element_text(colour = "black", face = "bold", size = 15))   
  theme(legend.position = "bottom", legend.text = element_text(face = "bold", size = 15),
        legend.title = element_blank())   ggtitle(Select)   xlab("")   ylab("")   
  theme(plot.title = element_text(size = 25, face = "bold", hjust = 0.5))

enter image description here

enter image description here

enter image description here

Then I can get a plot like the one below, where I want the red graph to be on top of the green graph. I also tried changing the order of the data, and several sites such as the Internet and Stack Overflow provided solutions and used them, but not a single solution was able to solve them.

If you know a solution, please let me know how to modify the code or change the data. thank you.

CodePudding user response:

You seem to be asking more than one question at once here, but the main one is: why do the bars for Raw appear under those for Trimmed? The short answer is: factor levels and the behaviour of coord_flip().

Let's make a toy dataset:

library(tidyverse)
G4 <- data.frame(INDV = c("C_01", "C_01", "C_41", "C_41"), 
                 Group = c("Raw data", "Trimmed data", "Raw data", "Trimmed data"), 
                 Bp = c(200, 100, 500, 400))

A simple dodged bar chart. Note that Raw comes before Trimmed, because R is before T in the alphabet:

G4 %>% 
  ggplot(aes(INDV, Bp))   
  geom_col(aes(fill = Group),
           position = "dodge")

enter image description here

Now we coord_flip:

G4 %>% 
  ggplot(aes(INDV, Bp))   
  geom_col(aes(fill = Group), 
           position = "dodge")   
  coord_flip()

enter image description here

This has the effect of reversing the variables, so Raw is now below Trimmed.

We can fix that by altering factor levels. As there are only two groups we can just reverse them using fct_rev() from the forcats package:

G4 %>% 
  ggplot(aes(INDV, Bp))   
  geom_col(aes(fill = fct_rev(Group)), 
  position = "dodge")   
  coord_flip() 

enter image description here

The bar for Raw is now on top but unfortunately, the colours are now reversed so that Raw bars are green. We can fix that using scale_fill_manual():

G4 %>% 
  ggplot(aes(INDV, Bp))   
  geom_col(aes(fill = fct_rev(Group)), 
           position = "dodge")   
  coord_flip()   
  scale_fill_manual(values = c("#00BFC4", "#F8766D"))

enter image description here

Now the Raw bars are on top, and they are red.

  • Related