简体   繁体   English

ggplot2如何将y比例相等的图形对齐为百分比

[英]ggplot2 How to align graphs with equal y scale as percentage

I'm trying to obtain 2 aligned bar plots with percentage scales of 2 different factors. 我正在尝试获取2个对齐的条形图,其中包含2个不同因素的百分比比例。 The y scales calculated as percent are different. 以百分比计算的y比例不同。 I would like to have the same y scales for both plots, for example from 0 to 40% in both. 我希望两个图的y比例相同,例如,两个图的0到40%。 I've tried ylim() which doesn't work on the percentage scale. 我尝试了ylim(),它在百分比刻度上不起作用。 Example below 下面的例子

library(ggplot2)
library(scales)
data("diamonds")

First bar-plot for cut 切割的第一个条形图

  p<- ggplot(diamonds, aes(x = cut)) +  
    geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) + 
    scale_y_continuous(labels = percent) +
    geom_text(aes(y = ((..count..)/sum(..count..)), label = 
    scales::percent((..count..)/sum(..count..))), 
            stat = "count", vjust = -0.25) +
    ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, 
    face="bold")) + 
    xlab("Cut") +
    ylab("Percent") +
    theme(legend.position="bottom")

Second bar-plot for clarity 第二个条形图,清晰明了

p1<- ggplot(diamonds, aes(x = clarity)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=clarity)) + 
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = 
scales::percent((..count..)/sum(..count..))), 
        stat = "count", vjust = -0.25) +
ggtitle("Clarity") + theme(plot.title = element_text(hjust = 0.5, size=14, 
face="bold")) + 
xlab("Clarity") +
ylab("Percent") +
theme(legend.position="bottom")

Arranging bar-plot with different scales 排列不同比例的条形图

  grid.arrange(p,p1, ncol = 2)  

different scales but I would like for example both at 40% top 不同的比例,但是我想两者都达到40%

在此处输入图片说明

If scales weren't percentages I would do this: 如果比例不是百分比,我会这样做:

p<- ggplot(diamonds, aes(x = cut)) +  
  geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) + 
  scale_y_continuous(labels = percent) +
  geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), 
            stat = "count", vjust = -0.25) +
  ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) + 
  xlab("Cut") +
  ylab("Percent") +
  ylim(0, 40)
  theme(legend.position="bottom")

But here, of course, it doesn't work and returns this: 但是,这里当然不起作用,并返回以下内容:

在此处输入图片说明

Ok I found a way, here the code for Cut for a % scale limited to 60% 好的,我找到了一种方法,在这里,将比例缩放比例限制为60%的代码

p<- ggplot(diamonds, aes(x = cut)) +  
  geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) + 
  geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), 
            stat = "count", vjust = -0.25) +
  ggtitle("Diamonds Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) + 
  scale_y_continuous(labels = scales::percent, limits=c(0,0.6)) + labs(y="Percent")
xlab("Cut") +
  theme(legend.position="bottom")
p

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM