无法获得用于显示条形图的%s和颜色的r代码

我在网上查找了许多示例代码片段,但是对于我一生来说,我无法弄清楚自己在做什么错。我在下面包含了提供该图表的r代码:

无法获得用于显示条形图的%s和颜色的r代码

通常可以正常工作,除了:

  1. 我无法显示%s而不是计数(对于因子变量“ doctorate”),并且

  2. “组”的颜色显示在图例中,而不显示在条形图中。

pg_doc <- ztemp.pg %>%
  ggplot(aes(doctorate,group = group)) +
  geom_bar(aes(y = ..prop..,fill = group),stat="count") + 
  geom_bar(position = position_dodge(preserve = "single")) +
  scale_fill_manual(values=c("grey45","goldenrod2")) +
  theme(legend.position = "bottom",legend.title=element_blank(),text = element_text(color="black"),#for x & y axis text labels
        axis.text.x = element_text(color="black",size=10,hjust=1),axis.text.y = element_text(color="black"),axis.title.x=element_blank(),axis.title.y=element_blank(),plot.title=element_text(hjust=.5))+
  ggtitle("Highest degrees attained by leadership")+
  scale_x_discrete(labels= c("Other","Doctorate"))

pg_doc
caiczy000 回答:无法获得用于显示条形图的%s和颜色的r代码

只需编写一个geom_bar。您还可以在fill函数中将aes参数移至ggplot命令。要显示百分比,您必须先计算它们并绘图。一个简单的可复制示例将提供更多的清晰度。

pg_doc <- ztemp.pg %>%
      ggplot(aes(doctorate,group = group)) +
      geom_bar(aes(y = ..prop..,fill = group),stat="count",position = position_dodge(preserve = "single")) +
      scale_fill_manual(values=c("grey45","goldenrod2")) +
      theme(legend.position = "bottom",legend.title=element_blank(),text = element_text(color="black"),#for x & y axis text labels
            axis.text.x = element_text(color="black",size=10,hjust=1),axis.text.y = element_text(color="black"),axis.title.x=element_blank(),axis.title.y=element_blank(),plot.title=element_text(hjust=.5))+
      ggtitle("Highest degrees attained by leadership")+
      scale_x_discrete(labels= c("Other","Doctorate"))

    pg_doc
本文链接:https://www.f2er.com/3169328.html

大家都在问