r的分组杆图

发布于 2025-01-25 21:37:05 字数 3499 浏览 2 评论 0原文

我正在尝试制作一个堆叠的条形图,但是栏是重叠的。

这是我使用的代码:

library(ggplot2)   
 
ggplot(data = opstaR, aes(x = Indicator, y = Percentage, fill = factor(tip))) +
    geom_bar(position = "dodge", stat = "identity") + 
    coord_flip() +
    scale_fill_grey(labels = c("Case load change", "Compliance change")) +
    theme_bw() +
    scale_y_continuous(labels = scales::percent, limits = c(-0.15, 0.05),breaks = seq(-0.15, 0.05, 0.05)) +
    theme(axis.title.y = element_blank()) +
    theme(legend.title = element_blank()) +
    theme(legend.position = "bottom")

这是输出:

以及如何颠倒黑色/灰色图案?

数据

opstaR <- structure(list(Indicator = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L), .Label = c("HbA1c measurment among diabetic patients", 
"LDL-cholesterol measurement among diabetic patients", "Diet advice among diabetic patients", 
"Blood preasure measurment among diabetic patients", "Foot examination among diabetic patients", 
"Blood preasure among hypertensive patients", "Total cholesterol among hypertensive patients", 
"Blood glucose among hypertensive patients", "Lifestyle advice among hypertensive patients", 
"ECG among hypertensive patients", "Lifestyle advice among patients with MI", 
"Blood preasure measurment among patients with MI", "Total cholesterol among patients with MI", 
"LDL-cholesterol measurement among patients with MI", "HDL-cholesterol measurement among patients with MI", 
"Triglycerides measurement among patients with MI", "Blood glucose among patients with MI", 
"ECG among patients with MI"), class = c("ordered", "factor")), 
    Percentage = c(-0.119461152, -0.119461152, -0.119461152, 
    -0.119461152, -0.119461152, -0.125256348, -0.125256348, -0.125256348, 
    -0.125256348, -0.125256348, -0.139557636, -0.139557636, -0.139557636, 
    -0.139557636, -0.139557636, -0.139557636, -0.139557636, -0.139557636, 
    -0.016017788, -0.01085703, 0.030175594, -0.02377841, -0.011164678, 
    -0.020884472, -0.014592098, -0.013684149, 0.033509796, -0.009671902, 
    0.037509542, -0.018565706, -0.01177768, -0.007261598, -0.005188794, 
    -0.008428487, -0.010241197, 0.002690945), tip = c("Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_compliance_rate", 
    "Change_in_case_load", "Change_in_compliance_rate", "Change_in_case_load", 
    "Change_in_compliance_rate", "Change_in_case_load", "Change_in_compliance_rate", 
    "Change_in_case_load", "Change_in_compliance_rate", "Change_in_case_load", 
    "Change_in_compliance_rate", "Change_in_case_load", "Change_in_compliance_rate", 
    "Change_in_case_load", "Change_in_compliance_rate", "Change_in_case_load", 
    "Change_in_compliance_rate", "Change_in_case_load")), row.names = c(NA, 
-36L), class = "data.frame")

I'm trying to make a stacked bar chart, but the bars overlap.

This is the code that I am using:

library(ggplot2)   
 
ggplot(data = opstaR, aes(x = Indicator, y = Percentage, fill = factor(tip))) +
    geom_bar(position = "dodge", stat = "identity") + 
    coord_flip() +
    scale_fill_grey(labels = c("Case load change", "Compliance change")) +
    theme_bw() +
    scale_y_continuous(labels = scales::percent, limits = c(-0.15, 0.05),breaks = seq(-0.15, 0.05, 0.05)) +
    theme(axis.title.y = element_blank()) +
    theme(legend.title = element_blank()) +
    theme(legend.position = "bottom")

This is the output:

bar plot

How can I make the bars not stack to each other?

And how to invert the black/gray pattern?

Data

opstaR <- structure(list(Indicator = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L), .Label = c("HbA1c measurment among diabetic patients", 
"LDL-cholesterol measurement among diabetic patients", "Diet advice among diabetic patients", 
"Blood preasure measurment among diabetic patients", "Foot examination among diabetic patients", 
"Blood preasure among hypertensive patients", "Total cholesterol among hypertensive patients", 
"Blood glucose among hypertensive patients", "Lifestyle advice among hypertensive patients", 
"ECG among hypertensive patients", "Lifestyle advice among patients with MI", 
"Blood preasure measurment among patients with MI", "Total cholesterol among patients with MI", 
"LDL-cholesterol measurement among patients with MI", "HDL-cholesterol measurement among patients with MI", 
"Triglycerides measurement among patients with MI", "Blood glucose among patients with MI", 
"ECG among patients with MI"), class = c("ordered", "factor")), 
    Percentage = c(-0.119461152, -0.119461152, -0.119461152, 
    -0.119461152, -0.119461152, -0.125256348, -0.125256348, -0.125256348, 
    -0.125256348, -0.125256348, -0.139557636, -0.139557636, -0.139557636, 
    -0.139557636, -0.139557636, -0.139557636, -0.139557636, -0.139557636, 
    -0.016017788, -0.01085703, 0.030175594, -0.02377841, -0.011164678, 
    -0.020884472, -0.014592098, -0.013684149, 0.033509796, -0.009671902, 
    0.037509542, -0.018565706, -0.01177768, -0.007261598, -0.005188794, 
    -0.008428487, -0.010241197, 0.002690945), tip = c("Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_compliance_rate", 
    "Change_in_case_load", "Change_in_compliance_rate", "Change_in_case_load", 
    "Change_in_compliance_rate", "Change_in_case_load", "Change_in_compliance_rate", 
    "Change_in_case_load", "Change_in_compliance_rate", "Change_in_case_load", 
    "Change_in_compliance_rate", "Change_in_case_load", "Change_in_compliance_rate", 
    "Change_in_case_load", "Change_in_compliance_rate", "Change_in_case_load", 
    "Change_in_compliance_rate", "Change_in_case_load")), row.names = c(NA, 
-36L), class = "data.frame")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-01 21:37:05

只需进行小调整,删除position =“ dodge”。对于颜色,一种选择是自己分配。

opstaR %>% 
  ggplot(aes(x = Indicator, y = Percentage, fill = factor(tip))) +
  geom_bar(stat = "identity") +
  coord_flip() +
  scale_fill_manual(labels = c("Case load change", "Compliance change"),values = c("grey","black")) +  theme_bw() +
  scale_y_continuous(labels = scales::percent, limits = c(-0.15, 0.05),breaks = seq(-0.15, 0.05, 0.05)) +
  theme(axis.title.y = element_blank(),
        legend.title = element_blank(),
        legend.position = "bottom")

Just a small tweak, remove the position = "dodge". For the colors, one option is to assign it yourself.

opstaR %>% 
  ggplot(aes(x = Indicator, y = Percentage, fill = factor(tip))) +
  geom_bar(stat = "identity") +
  coord_flip() +
  scale_fill_manual(labels = c("Case load change", "Compliance change"),values = c("grey","black")) +  theme_bw() +
  scale_y_continuous(labels = scales::percent, limits = c(-0.15, 0.05),breaks = seq(-0.15, 0.05, 0.05)) +
  theme(axis.title.y = element_blank(),
        legend.title = element_blank(),
        legend.position = "bottom")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文