使用堆积条形图的 y 轴标签计算(*4 或百分比)
Calculating with y-axis labels of stacked bar plot (either *4 or into percent)
我知道这是一个已经经常讨论的话题,但是我没有找到一个令人满意的解决方案来解决这个问题:
我想绘制一个 100% 的堆积条形图。堆栈应包括绝对计数,y 轴应说明百分比。此外,它包含 1 个分组和 3 个方面:
如您所见,每个组包含 25 名参与者(y 轴)。我正在寻找一种简单的解决方案来将 y 轴标签更改为百分比;例如y 轴中断四次 (25*4 = 100)。或者,我尝试为比率创建第二个数据框,但在将它们包含在此图中时遇到了麻烦。
event_labels <- c("T0\nBaseline", "T1\nDay 7", "T2\nWeek 6", "T3\nWeek 12", "T4\n6 Months\nFollow-Up")
names(event_labels) <- c("t0", "t1", "t2", "t3", "t4")
rem_eular_response <- rem_eular_data %>%
ggplot(aes(x=gruppe,
fill=factor(number_ordered))) +
geom_bar() +
stat_count(geom = "text",
aes(label = (..count..)),
position = position_stack(vjust = 0.5),
size = 4) +
facet_wrap(~event,
scales="free",
ncol=3,
labeller = labeller(event = event_labels)) +
labs(title ="",
fill="") +
ylab("Patients achieving response [%]") +
xlab("") +
#labs(color="", linetype="")+
theme_bw()+
theme(strip.background=element_rect(fill="lightgrey",
color="transparent"),
panel.border = element_blank(),
axis.line = element_line(color="black",
size=0.2),
strip.text = element_text(face="bold"),
legend.position = "bottom",
legend.title=element_text())+
scale_x_discrete(labels=c('DGE', 'Fasting'))+
scale_fill_brewer(palette = 'Accent',
na.value = "grey70",
labels=c('Good Responder', 'Moderate Responder', 'Non-Responder',
'Missing'))
这是我的数据的最小可重现示例:
structure(list(sid = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
5, 5, 6, 6, 6, 8, 8), gruppe = c("DGE", "DGE", "DGE", "DGE",
"DGE", "DGE", "DGE", "DGE", "DGE", "Fasten", "Fasten", "Fasten",
"Fasten", "Fasten", "Fasten", "Fasten", "Fasten", "Fasten", "DGE",
"DGE"), responsetype = c("das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response"
), event = c("t1", "t2", "t3", "t1", "t2", "t3", "t1", "t2",
"t3", "t1", "t2", "t3", "t1", "t2", "t3", "t1", "t2", "t3", "t1",
"t2"), number = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 2, 2, 1, NA,
NA, 1, 1, 1, NA, NA), responsetype_ordered = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("remission", "das28esr_response"), class = "factor"),
number_ordered = structure(c(3L, 3L, 3L, 2L, 2L, 2L, 3L,
2L, 3L, 3L, 1L, 1L, 2L, NA, NA, 2L, 2L, 2L, NA, NA), .Label = c("2",
"1", "0"), class = "factor")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
您有快速简单的解决方案吗?
提前致谢!
您可以将此添加到您的代码中:
scale_y_continuous(labels = function(x) paste0((x/max(x))*100, "%"))
对于没有(event_labels
)的给定示例数据集:
我知道这是一个已经经常讨论的话题,但是我没有找到一个令人满意的解决方案来解决这个问题:
我想绘制一个 100% 的堆积条形图。堆栈应包括绝对计数,y 轴应说明百分比。此外,它包含 1 个分组和 3 个方面:
如您所见,每个组包含 25 名参与者(y 轴)。我正在寻找一种简单的解决方案来将 y 轴标签更改为百分比;例如y 轴中断四次 (25*4 = 100)。或者,我尝试为比率创建第二个数据框,但在将它们包含在此图中时遇到了麻烦。
event_labels <- c("T0\nBaseline", "T1\nDay 7", "T2\nWeek 6", "T3\nWeek 12", "T4\n6 Months\nFollow-Up")
names(event_labels) <- c("t0", "t1", "t2", "t3", "t4")
rem_eular_response <- rem_eular_data %>%
ggplot(aes(x=gruppe,
fill=factor(number_ordered))) +
geom_bar() +
stat_count(geom = "text",
aes(label = (..count..)),
position = position_stack(vjust = 0.5),
size = 4) +
facet_wrap(~event,
scales="free",
ncol=3,
labeller = labeller(event = event_labels)) +
labs(title ="",
fill="") +
ylab("Patients achieving response [%]") +
xlab("") +
#labs(color="", linetype="")+
theme_bw()+
theme(strip.background=element_rect(fill="lightgrey",
color="transparent"),
panel.border = element_blank(),
axis.line = element_line(color="black",
size=0.2),
strip.text = element_text(face="bold"),
legend.position = "bottom",
legend.title=element_text())+
scale_x_discrete(labels=c('DGE', 'Fasting'))+
scale_fill_brewer(palette = 'Accent',
na.value = "grey70",
labels=c('Good Responder', 'Moderate Responder', 'Non-Responder',
'Missing'))
这是我的数据的最小可重现示例:
structure(list(sid = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
5, 5, 6, 6, 6, 8, 8), gruppe = c("DGE", "DGE", "DGE", "DGE",
"DGE", "DGE", "DGE", "DGE", "DGE", "Fasten", "Fasten", "Fasten",
"Fasten", "Fasten", "Fasten", "Fasten", "Fasten", "Fasten", "DGE",
"DGE"), responsetype = c("das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response",
"das28esr_response", "das28esr_response", "das28esr_response"
), event = c("t1", "t2", "t3", "t1", "t2", "t3", "t1", "t2",
"t3", "t1", "t2", "t3", "t1", "t2", "t3", "t1", "t2", "t3", "t1",
"t2"), number = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 2, 2, 1, NA,
NA, 1, 1, 1, NA, NA), responsetype_ordered = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("remission", "das28esr_response"), class = "factor"),
number_ordered = structure(c(3L, 3L, 3L, 2L, 2L, 2L, 3L,
2L, 3L, 3L, 1L, 1L, 2L, NA, NA, 2L, 2L, 2L, NA, NA), .Label = c("2",
"1", "0"), class = "factor")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
您有快速简单的解决方案吗? 提前致谢!
您可以将此添加到您的代码中:
scale_y_continuous(labels = function(x) paste0((x/max(x))*100, "%"))
对于没有(event_labels
)的给定示例数据集: