R 在x轴上绘制四个分类变量的条形图,每个变量有两个级别作为填充,并带有各自的百分比

R 在x轴上绘制四个分类变量的条形图,每个变量有两个级别作为填充,并带有各自的百分比,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我花了很多时间试图用ggplot2制作这张图,最终放弃了,用excel b/c制作。我不得不做演示,但我很想用ggplot2制作。下面有一个我如何在excel中制作的示例 有4个分类变量,我有它们对两个队列(NPO,ALL)贡献的百分比 我创建了一个数据框,并能够在某种程度上绘制它们,但无法用它们各自的百分比标记列,也无法更改每个x变量的顺序(顺序应与我制作的excel图表中的顺序相同) 非常感谢任何见解,我一直在扯我的头发,我求助于excel 在我的努力之下 df_all_npo_insur

我花了很多时间试图用ggplot2制作这张图,最终放弃了,用excel b/c制作。我不得不做演示,但我很想用ggplot2制作。下面有一个我如何在excel中制作的示例

有4个分类变量,我有它们对两个队列(NPO,ALL)贡献的百分比

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

非常感谢任何见解,我一直在扯我的头发,我求助于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)

解释

reorder()
:请参阅浏览量很大的帖子。“—”号告诉R按降序排序,因为默认值为升序

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

paste0()
:非常简单的函数来连接向量。在这里,我们只需要将
%
添加到
百分比
向量的每个值中

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



真是太神奇了。非常感谢。我编辑了我的第一篇文章,只是提出了一些澄清性的问题,比如你的代码中的一些行,这样下次我就可以更好地理解自己了。谢谢!:)非常有用。非常感谢。
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")
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")