本文介绍了如何控制ggplot2中堆栈栏之间的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在ggplot2中绘制一个堆栈barplot。我的数据集就像是, var1 var2 var3 value treatment1 group_1 C8.0 0.010056478 treatment2 group_1 C8 .0 0.009382918 treatment3 group_2 C8.0 0.003014983 treatment4 group_2 C8.0 0.005349631 treatment5 group_2 C8.0 0.005349631 var1 包含5个处理,这5个处理属于 var2 ,每个治疗在 var3 中有14个测量值,它们的值存储在值中。 我想绘制一张图来比较这五种治疗方法及其测量结果。 ,所以我绘制了如下图所示的堆栈条: 我的代码: library(ggplot2) colourCount = length(唯一的(mydata $ var3)) getPalette = colorRampPalette(brewer.pal 14,YlGnBu))#从调色板中获取更多颜色 ggplot(data = mydata,aes(x = var1,y = value,fill = var3))+ geom_bar(stat =identity,position =stack,color =black,width = 0.2)+ *#geom_errorbar(aes(ymax = var3 + se,ymin = var3-se,width = .1) )+ * scale_fill_manual(values = getPalette(colourCount))+ scale_y_continuous(expand = c(0,0))+ mytheme 我怎样才能将前两个堆积的柱子和其他三个柱子组合在一起?因为它们属于两组 var2 。解决方 (dplyr) library(ggplot2) dummydf var3 = paste0(C_,11: 15)%%>% mutate(value = runif(length(var1)), var2 = ifelse(%c(trt1,trt2),grp1,var1% grp2)) ggplot(dummydf,aes(var1,value,fill = var3))+ geom_col(position =stack)+ facet_grid(〜var2,scales =free_x,space =free_x) 这个解决方案有时很棒!优点是: $ b $ ol 实现起来非常简单 包含分层分组标签顶部 通常看起来不错 很容易定制。 例如: ggplot(dummydf,aes(var1,value,fill = var3))+ geom_col(position =stack)+ facet_grid(〜var2,scales =free_x,space =free_x) + 主题(panel.spacing = unit(3,cm), strip.text = element_text(size = 12,family =mono)) 主要缺点到这个方法: 如果这已经是一个面板拼图的一部分,它会让整件事变得混乱。 li> 如果您的分层组从治疗中显而易见,则您可能不需要明确标注,只需快速区分视觉。例如,假设这些组是控制/干预,而您的治疗是无药物,安慰剂和药物1,2和3。 ol> 所以这里有一个替代方法: dummydf%>% bind_rows(data_frame(var1 =trt99))%>% ggplot(aes(var1,value,fill = var3))+ geom_col(position =stack)+ scale_x_discrete(limits = c(trt1,trt2,trt99,trt3,trt4,trt5), breaks = c(trt1,trt2,NA ,trt3,trt4,trt5), labels = c(trt1,trt2,,trt3,trt4,trt5)) 您可以在假栏空间中提供更多信息: 添加 替换 NA 和在中断和标签中使用 trt99 和< -group1 | group2->或类似的东西。 I'm plotting a stack barplot in ggplot2. My dataset is like,var1 var2 var3 valuetreatment1 group_1 C8.0 0.010056478treatment2 group_1 C8.0 0.009382918treatment3 group_2 C8.0 0.003014983treatment4 group_2 C8.0 0.005349631treatment5 group_2 C8.0 0.005349631var1 contains 5 treatments, these five treatments belong to two groups in var2, and each treatment has 14 measurements in var3, their value stored in value.I want to make a plot to compare these five treatments, and their measurements.so I plot with stack bar plot like this figure:My code:library(ggplot2)colourCount = length(unique(mydata$var3))getPalette = colorRampPalette(brewer.pal(14, "YlGnBu")) #get more color from paletteggplot(data=mydata, aes(x=var1, y=value, fill=var3))+ geom_bar(stat="identity", position="stack", colour="black", width=.2)+ *#geom_errorbar(aes(ymax=var3+se, ymin=var3-se, width=.1))+* scale_fill_manual(values = getPalette(colourCount))+ scale_y_continuous(expand = c(0, 0))+ mythemeHow could I group the first two stacked columns together, and the other three columns together? Because they belong to two groups in var2. 解决方案 The "duplicate question" comments above will lead you to an answer like this one:library(dplyr)library(ggplot2)dummydf <- expand.grid(var1 = paste0("trt", 1:5), var3 = paste0("C_", 11:15)) %>% mutate(value = runif(length(var1)), var2 = ifelse(var1 %in% c("trt1", "trt2"), "grp1", "grp2")) ggplot(dummydf, aes(var1, value, fill = var3)) + geom_col(position = "stack") + facet_grid(~var2, scales = "free_x", space = "free_x")And this solution is sometimes great! The advantages are:it's simple to implementcontains the labels for the hierarchical grouping at the topgenerally looks niceis easily customizable.For example: ggplot(dummydf, aes(var1, value, fill = var3)) + geom_col(position = "stack") + facet_grid(~var2, scales = "free_x", space = "free_x") + theme(panel.spacing = unit(3, "cm"), strip.text = element_text(size = 12, family = "mono"))The main disadvantages to this method:If this is already part of a facet panelled plot, it makes thewhole thing cluttered.If your hierarchical groups are obvious from the treatments, you may not need explicit labelling, just a quick visual distinction.For example, say the groups are control/intervention, and yourtreatments are "no drug, placebo" and "drugs 1, 2, and 3".So here's an alternate method:dummydf %>% bind_rows(data_frame(var1 = "trt99")) %>% ggplot(aes(var1, value, fill = var3)) + geom_col(position = "stack") + scale_x_discrete(limits = c("trt1", "trt2", "trt99", "trt3", "trt4", "trt5"), breaks = c("trt1", "trt2", NA, "trt3", "trt4", "trt5"), labels = c("trt1", "trt2", "", "trt3", "trt4", "trt5"))This solution has its own drawbacks, primarily that you can only customize the space in a limited way. You can create a "false" bar equal to an integer multiple of the widths of the bars you've already got by adding additional false levels to your limits, breaks, and labels. But you can't create a space that's only half a bar wide.You could provide additional information in the false bar space though:Adding a text annotation in the plot areaReplacing the NA and "" in breaks and labels with trt99 and "<-group1 | group2->" or something similar. 这篇关于如何控制ggplot2中堆栈栏之间的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 21:04