Home > Mobile >  Creating a bar chart using ggplot2 and splitting bars by multiple values?
Creating a bar chart using ggplot2 and splitting bars by multiple values?

Time:12-08

I have a dataset that looks like this

Telangiectasia_time      grade0 grade1 grade2 gradeNA
  <chr>                     <int>  <int>  <int>   <int>
1 telangiectasia_tumour_0    2039     14      2       4
2 telangiectasia_tumour_1    2007      9      1      42
3 telangiectasia_tumour_12   1807     32      3     217
4 telangiectasia_tumour_24   1666     77      5     311

I want to plot Telangiectasia on the x asis, so there are 4 bars corresponding to telangiectasia_tumour_0, telangiectasia_tumour_1, telangiectasia_tumour_12, telangiectasia_tumour_24. I want the bars to split via number on the y axis. E.g. For telangiectasia_tumour_0, Grade 0 chunck to be 2039, grade 1 to be 14 etc...

I have tried the following code:

ggplot(telangiectasia_tumour_data, aes(x=Telangiectasia_time))   geom_bar(position = "stack")  theme_minimal() 

However, I just get 4 big bars with the total number (2059) for each bar. But I want these bars to be split based on the grades.

Bar chart

CodePudding user response:

Sample data:

data=data.frame("Telangiectasia_time" = c("telangiectasia_tumour_0", "telangiectasia_tumour_1", "telangiectasia_tumour_12", "telangiectasia_tumour_24"),
    "value"= c(2039, 2007, 1807, 1666, 14, 9, 32, 77, 2,1,3,5, 2, 42, 217, 311),
    "grade" = c("grade0", "grade0", "grade0", "grade0", "grade1", "grade1", "grade1", "grade1",
               "grade2", "grade2", "grade2", "grade2", "gradeNA", "gradeNA", "gradeNA" ,"gradeNA" ))

Ploting the data:

ggplot(data, aes(Telangiectasia_time, y=value, x= Telangiectasia_time, fill=grade))  
 # labs(x="", y="", )
  geom_bar(stat="identity") 
  labs(x="Telangiectasia_time", y="value", title="", fill="Grades")           
  theme_bw() 
  theme(plot.title = element_text(hjust = 0.5,  face="bold", size=20, color="black"))   
  theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black")) 
  theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black")) 
  theme(axis.text.x = element_text( hjust = 1,  face="bold", size=14, color="black", angle=30) ) 
  theme(axis.text.y = element_text( hjust = 1,  face="bold", size=14, color="black") ) 
  theme(plot.title = element_text(hjust = 0.5)) 
  theme(legend.title = element_text(family="Times", color = "black", size = 16,face="bold"),
        legend.text = element_text(family="Times", color = "black", size = 14,face="bold"),
        legend.position="right",
        plot.title = element_text())

enter image description here

or with facet_wrap()

data$Telangiectasia_time<-as.factor(data$Telangiectasia_time)

ggplot(data, aes(Telangiectasia_time, y=value, x= "", fill=grade))  
  geom_bar(stat="identity") 
  facet_wrap(~Telangiectasia_time) 
  labs(x="", y="value", title="", fill="Grades")           
  theme_bw() 
  theme(plot.title = element_text(hjust = 0.5,  face="bold", size=20, color="black"))   
  theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black")) 
  theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black")) 
  theme(axis.text.x = element_text( hjust = 1,  face="bold", size=14, color="black", angle=30) ) 
  theme(axis.text.y = element_text( hjust = 1,  face="bold", size=14, color="black") ) 
  theme(plot.title = element_text(hjust = 0.5)) 
  theme(legend.title = element_text(family="Times", color = "black", size = 16,face="bold"),
        legend.text = element_text(family="Times", color = "black", size = 14,face="bold"),
        legend.position="right",
        plot.title = element_text()) 
theme(strip.text.x = element_text(size = 16, colour = "black",family="Times"))

enter image description here

CodePudding user response:

You need to be gather the grades into name:value pairs and then group by the name like so:

data %>%
  pivot_longer(cols = starts_with('grade')) %>%
  ggplot(aes(x = Telangiectasia_time, y = value, fill = name))  
    geom_bar(stat = 'identity')  
    theme_minimal()

CodePudding user response:

The problem is that you are not grouping your data in a recognisable way for ggplot When I want to do this kind of things I use fill or group in ggplot, which recognises different categories (in this case the grades). For it to work I reorganised your data a bit as well

You may have to convert the values to class numeric for it to work properly

 Telangiectasia_time <- c("telangiectasia_tumour_0", "telangiectasia_tumour_1", "telangiectasia_tumour_12", "telangiectasia_tumour_24")

value <- c(2039, 2007, 1807, 1666, 14, 9, 32, 77, 2,1,3,5, 2, 42, 217, 311)
grade <- c("grade0", "grade0", "grade0", "grade0", "grade1", "grade1", "grade1", "grade1",
           "grade2", "grade2", "grade2", "grade2", "gradeNA", "gradeNA", "gradeNA" ,"gradeNA" )

data <- cbind(Telangiectasia_time, value, grade) %>% as.data.frame()

barplot <- data %>% ggplot(aes(Telangiectasia_time, value, fill = grade))  
  geom_bar(stat="identity", position="stack")

see plot here

  • Related