如果bar的名称类别是字符,如何使用geom_bar连接堆叠的bar比例 [英] How to use geom_bar to connect stacked-bar proportions if name categorial for bar is character
问题描述
This is an extension to a previous answer of a question found here
简短地 @Jon Spring 使用以下示例代码来生成堆叠的条形图,并用线条连接每个条形比例两组之间:
Briefly @Jon Spring uses the following example code to produce a stacked bar plot with lines connecting each bar proportion between the two groups:
library(ggplot2)
set.seed(0)
data_bar <- data.frame(
stringsAsFactors = F,
Sample = rep(c("A", "B"), each = 10),
Percentage = runif(20),
Taxon = rep(1:10, by = 2)
)
library(tidyr)
ggplot() +
geom_bar(data = data_bar,
aes(x = Sample, y =Percentage, fill = Taxon),
colour = 'white', width = 0.3, stat="identity") +
geom_segment(data = tidyr::spread(data_bar, Sample, Percentage),
colour = "white",
aes(x = 1 + 0.3/2,
xend = 2 - 0.3/2,
y = cumsum(A),
yend = cumsum(B))) +
theme(panel.background = element_rect(fill = "black"), # to make connecting points
panel.grid = element_blank())
尽管这是一段精美的代码,可以解决连接小节比例的问题,但是一旦小节比例名称是字符串,而不是上面的整数,我就无法以某种方式重现它.这是我的代码:
While this is an elegant piece of code to address the issue of connecting the bar proportions, I am somehow not able to reproduce it once the bar proportion names are character strings instead on integer as above. Here is my code:
test.matrix<-matrix(c(70,120,65,140,13,68,46,294,52,410),ncol=2,byrow=TRUE)
rownames(test.matrix)<-c("BC.1","BC.2","GC","MO","EB")
colnames(test.matrix)<-c("12m","3m")
test.matrix <- data.frame(test.matrix)
ggplot() +
geom_bar(data = test.matrix,
aes(x = Var2, y =Freq, fill = Var1),
colour = 'black', width = 0.3, stat="identity") +
geom_segment(data = tidyr::spread(test.matrix, Var2, Freq),
colour = "black",
aes(x = 1 + 0.3/2,
xend = 2 - 0.3/2,
y = cumsum(`12m`),
yend = cumsum(`3m`))) +
scale_fill_manual(values=c('BC.1'="gold",'BC.2'="yellowgreen",'GC'="navy",'MO'="royalblue",'EB'="orangered")) +
theme(panel.background = element_rect(fill = "white"), panel.grid = element_blank())
结果与geom_segment线和条形比例不匹配.也许它与 cumsum()
使用字符串的字母顺序有关,但是我不知道如何解决这个问题-或它完全不同...
The result does not match the geom_segment lines to the bar proportions. Maybe it has sth to do with cumsum()
using an alphabetic order of the strings, but I cannot figure out how to solve this - or its sth completely different...
所以我有两个问题:
-
如果必须确定比例顺序,如何连接钢筋比例?(每个值组或每个行的字符串矢量或因子作为名称")
How can the bar proportions be connected if the proportions order has to be fixed? (a string vector or factor as 'names' for each value group or row)
如何在每个条的最底部生成一个附加的geom_segment,以将每个条的两个下端彼此连接?
How can an additional geom_segment at the very bottom of each bar be generated connecting both lower ends of each bar with another?
推荐答案
-
问题是您
cumsum
用错误的方向"输入,或订单,即,您在BC.1
处开始cumsum
定位,而在条形图中,它位于顶部.这可以简单地通过在累积之前重新排列数据集来解决.因此,我认为最好在绘图代码之外执行此操作,以便您可以轻松地检查数据.
The issue is that you
cumsum
med in the wrong "direction" or order, i.e. you startcumsum
ming atBC.1
while in the bar chart it's on the top. This could simply be fixed by rearranging the dataset before cumulating. Therefore in my opinion it's best to do this outside of the plotting code so that you can easily check the data.
要在底部获得另一个 geom_segment
,您只需在数据中添加一行即可.
To get another geom_segment
at the bottom you can simply add a row to your data.
library(tidyverse)
test.matrix<-matrix(c(70,120,65,140,13,68,46,294,52,410),ncol=2,byrow=TRUE)
rownames(test.matrix)<-c("BC.1","BC.2","GC","MO","EB")
colnames(test.matrix)<-c("12m","3m")
test.matrix <- data.frame(test.matrix)
test.matrix <- test.matrix %>%
setNames(c("12m", "3m")) %>%
rownames_to_column(var = "Var1") %>%
pivot_longer(-Var1, names_to = "Var2", values_to = "Freq")
test.matrix.wide <- tidyr::spread(test.matrix, Var2, Freq) %>%
arrange(desc(Var1)) %>%
mutate(y = cumsum(`12m`),
yend = cumsum(`3m`)) %>%
add_row(y = 0, yend = 0)
ggplot() +
geom_bar(data = test.matrix,
aes(x = Var2, y =Freq, fill = Var1),
colour = 'black', width = 0.3, stat="identity") +
geom_segment(data = test.matrix.wide,
colour = "black",
aes(x = 1 + 0.3/2,
xend = 2 - 0.3/2,
y = y,
yend = yend)) +
scale_fill_manual(values=c('BC.1'="gold",'BC.2'="yellowgreen",'GC'="navy",'MO'="royalblue",'EB'="orangered")) +
theme(panel.background = element_rect(fill = "white"), panel.grid = element_blank())
这篇关于如果bar的名称类别是字符,如何使用geom_bar连接堆叠的bar比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!