编辑条形图以创建显示百分比的条形

问题描述 投票:0回答:1

我想创建一个类似于我在医学期刊上展示的图表,它显示了两种治疗方法,仅显示了反应的百分比。 我的图表显示了有反应的患者和没有反应的患者。 另外,我想在列上添加百分比数字,将 Y 轴标签更改为数字(不是小数),并将比例显示为最多 60% 而不是 100%。

我正在分享我的图表,一张与所需图表类似的图表和我的代码。

Here's the desired chart

这是我的代码:

ggplot(early_treatment_effects, 
       aes(x = Treatment, 
          fill = early_serological_response)) +
  geom_bar(position = "fill") +
  labs(title = "Early Serological Response by Treatment Group", subtitle = "Difference 2%, (95% CI, 35 to57), p 0.12",
 y = "Response rate", fill = "Response")
theme_minimal()

这是我的图表。

Here's my chart

r ggplot2 charts bar-chart
1个回答
0
投票
library(ggplot2)
library(magrittr)
library(dplyr)
library(scales)
early_treatment_effects %>% 
  group_by(Treatment) %>% 
  summarise(early_serological_response = mean(early_serological_response)) %T>%  print() %>% 
  ggplot(aes(x = Treatment, y = early_serological_response)) +
  geom_col() +
  geom_text(aes(label = percent(early_serological_response)), vjust = 2, color = "white") +
  labs(title = "Early Serological Response by Treatment Group", subtitle = "Difference 2%, (95% CI, 35 to57), p 0.12",
       y = "Response rate", fill = "Response") +
  theme_minimal()
© www.soinside.com 2019 - 2024. All rights reserved.