简体   繁体   English

如何在r中使用ggplot中的比例绘制两个分类变量

[英]How to plot two categorical variables using proportion in ggplot in r

I have two categorical variables v1 is a dichotomous(Yes/No) variable and v2 has multiple outcomes. 我有两个分类变量v1是二分(Yes / No)变量,v2有多个结果。 I wanted to plot the distribution of v2 only in Yes group using ggplot 我想使用ggplot仅在Yes组中绘制v2的分布

This is my code, but I am not satisfied with the outcome yet. 这是我的代码,但我对结果还不满意。

v1 <- c("Yes", "Yes", "No", "No", "Yes", "Yes", "No", "No", "Yes", "Yes", "No", "No")
v2 <- c("Blue", "Red", "Blue", "Green", "Blue", "Red", "Blue", "Green", "Blue", "Red", "Blue", "Green")
df <- as.data.frame(cbind(v1, v2))
df
table(df)
ggplot(data = df, aes(x = v1, group = v2)) + 
  geom_bar(aes(y = ..prop..), stat = "count") + 
  scale_y_continuous(labels = scales ::percent_format()) + 
  facet_grid(~v2)

I expect the output to only displace proportion Yes among v2. 我希望输出只能在v2中取代比例为Yes。 Please help me with this. 请帮我解决一下这个。

Thanks in advance. 提前致谢。

You may use dplyr for creating a dataframe as you want in the following manner: 您可以使用dplyr以下列方式创建数据帧:

toPlot<-df%>%
    group_by(v1, v2)%>%
    summarise(n = n())%>%
    group_by(v2)%>%
    mutate(prop = n/sum(n))

Then, the graph would be the following: 然后,图表将如下:

ggplot(data = toPlot, aes(v1, prop, fill = v2)) + 
    geom_col() + 
    facet_grid(~v2)+
    scale_fill_manual(values = c("blue","green", "red"))

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

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