使用 ggplot2 创建条形图并按多个值拆分条形?

Posted

技术标签:

【中文标题】使用 ggplot2 创建条形图并按多个值拆分条形?【英文标题】:Creating a bar chart using ggplot2 and splitting bars by multiple values? 【发布时间】:2022-01-13 07:17:18 【问题描述】:

我有一个看起来像这样的数据集

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

我想在 x 轴上绘制毛细血管扩张,因此有 4 个条形对应于毛细血管扩张_肿瘤_0、毛细血管扩张_肿瘤_1、毛细血管扩张_肿瘤_12、毛细血管扩张_肿瘤_24。我希望条形图通过 y 轴上的数字拆分。例如。对于 telangiectasia_tumour_0,0 级块是 2039,1 级是 14 等等......

我已经尝试了以下代码:

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

但是,我只得到 4 个大条,每个条的总数 (2059)。但我希望根据成绩来拆分这些条。

【问题讨论】:

【参考方案1】:

样本数据:

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" ))

绘制数据:

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())

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"))

【讨论】:

【参考方案2】:

您需要将成绩收集到名称:值对中,然后按名称分组,如下所示:

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

【讨论】:

【参考方案3】:

问题是您没有以可识别的方式对 ggplot 数据进行分组 当我想做这种事情时,我在 ggplot 中使用填充或分组,它可以识别不同的类别(在这种情况下是成绩)。为了让它工作,我也重新组织了你的数据

您可能必须将值转换为类数字才能正常工作

 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")

【讨论】:

以上是关于使用 ggplot2 创建条形图并按多个值拆分条形?的主要内容,如果未能解决你的问题,请参考以下文章

R语言ggplot2可视化彩色水平条形图并基于条形长度和数值标签长度自定义最优化配置标签在条形内部或者条形外部

R语言ggplot2可视化绘制分组水平条形图并在条形图的各种位置添加数值标签实战

R语言ggplot2可视化发散型条形图发散条形图(Diverging Bars)是一种可以同时处理负值和正值的条形图并按照大小排序区分数据(Diverging Bars)

在 r ggplot2 中为百分比值创建条形图

用均值条形图及其标准差 ggplot2 总结数据框

为多个变量制作堆积条形图 - R 中的 ggplot2