在x轴上绘制四个类别变量,每个变量具有两个填充级别,并分别带有百分比

时间:2020-06-13 02:38:10

标签: r ggplot2 bar-chart

我花了很多时间试图用ggplot2制作这张图,最终放弃了,并用excel b / c制作了它,我不得不给出演示文稿,但是我喜欢用ggplot2来做。下面有一个示例,说明如何在excel中制作。

Excel example of what I am trying to accomplish with ggplot!

有4个类别变量,我有两个类别变量(NPO,ALL)占其中的百分比。

我创建了一个数据框,能够在某种程度上绘制它们,但无法用各自的百分比标记列,也无法更改每个x变量的顺序(顺序应与在我制作的Excel图表中)。

非常感谢任何见识,一直在梳理我的头发,以求取胜。

在我的尝试下

df_all_npo_insurance <- data.frame(insurance=c("Medicaid", "Medicaid",
                                               "Blue Cross", "Blue Cross",
                                               "Managed Care", "Managed Care",
                                               "Other", "Other"),
                                   cohort=c("NPO", "All",
                                            "NPO", "All",
                                            "NPO", "All",
                                            "NPO", "All"),
                                   percentage=c(70.1, 43.6, 17.4, 34.8, 8.5, 14.3, 4.0, 7.3))


ggplot(df_all_npo_insurance, aes(x=insurance, y=percentage, fill=cohort)) +                        
geom_col(position="dodge", width =0.8, colour="black", size=.8)

编辑6/13 / 20-仅仅为了我的学习,就您的代码提出一些澄清的问题,以便我下次可以自己做!

library(dplyr)
library(ggplot2)

df_all_npo_insurance %>%
  mutate(cohort=factor(cohort, levels=c("NPO","All"))) %>%
  ggplot(aes(x=reorder(insurance, -percentage), y=percentage,   # What does the "reorder" do here, particularly the "-percentage"?
             fill=cohort, label = paste0(percentage,"%"))) +    # What does the "paste0" do here? 
  geom_col(position="dodge", width = 0.8, size = 0.8) +
  scale_y_continuous(lim=c(0,71)) +
  geom_text(position = position_dodge(width = 0.8), vjust = -0.5, size = 2) + 
  scale_fill_manual(values=c("blue","darkorange"), labels=c("NPO Violators","All Cases")) +
  theme_classic() +
  theme(axis.line.y=element_blank(),   # I am familiar with the "theme" call, but would you mind telling me a little about what you did here with the "axis.text.y=element_blank()" and the other ones here?
        axis.text.y=element_blank(),
        axis.title=element_blank(),
        axis.ticks=element_blank(),
        legend.position = "bottom", 
        legend.title = element_blank(), 
        plot.title=element_text(size=10, hjust=0.5)) +
  ggtitle("NPO Violators vs All Cases by Insurance")

1 个答案:

答案 0 :(得分:1)

library(dplyr)
library(ggplot2)

df_all_npo_insurance %>%
  mutate(cohort=factor(cohort, levels=c("NPO","All"))) %>%
  ggplot(aes(x=reorder(insurance, -percentage), y=percentage, 
             fill=cohort, label = paste0(percentage,"%"))) +
  geom_col(position="dodge", width = 0.8, size = 0.8) +
  scale_y_continuous(lim=c(0,71)) +
  geom_text(position = position_dodge(width = 0.8), vjust = -0.5, size = 2) + 
  scale_fill_manual(values=c("blue","darkorange"), labels=c("NPO Violators","All Cases")) +
  theme_classic() +
  theme(axis.line.y=element_blank(),
        axis.text.y=element_blank(),
        axis.title=element_blank(),
        axis.ticks=element_blank(),
        legend.position = "bottom", 
        legend.title = element_blank(), 
        plot.title=element_text(size=10, hjust=0.5)) +
  ggtitle("NPO Violators vs All Cases by Insurance")

说明

reorder():请参阅this浏览量高的帖子。因为默认值是升序,所以“-”号告诉R降序排列。

label=:这告诉ggplot我们将通常使用geom_text()将文本添加到绘图中。

paste0():非常简单地用于连接向量。在这里,我们只需要向%向量的每个值添加percentage

theme():默认情况下提供大多数ggplot组件(轴,标签,图例标题)。如果要抑制它们或更改默认值,则需要这样说。 element_blank()是“”的ggplot语法,即不显示任何内容。


enter image description here