在ggplot中绘制2个变量以显示比例/百分比,而不是总和

时间:2018-06-06 18:39:07

标签: r ggplot2

我想根据州绘制对调查问题的回答。我有数据框

DF <- data.frame(V1 = factor(c("Option 1", "Option 1", "Option 1", "Option 2", "Option 1", "Option 2", "Option 1", "Option 1", "Option 2", NA, "Option 2", "Option 1")),
                  Location = factor(c("California", "Georgia", "Texas", "Texas", "Georgia", "Georgia", "California", "Georgia", "Texas", "Texas", "California", "Georgia")))

由于格鲁吉亚在样本中的比例过高,因此情节难以理解:

library(tidyr)
library(dplyr)
DF %>%
  filter(!is.na(V1)) %>% 
  ggplot(aes(V1, ..count..)) + 
  geom_bar(aes(fill = Location), position = "dodge") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

有没有办法显示回复的比例/百分比,每个州的回复标准化为1还是100?

1 个答案:

答案 0 :(得分:1)

我不是100%肯定我正在关注,但是这里有一个选项显示不同选项的相对比例而不是计数:

DF <- data.frame(
  V1 = factor(c("Option 1", "Option 1", "Option 1", "Option 2", "Option 1", "Option 2", "Option 1", "Option 1", "Option 2", NA, "Option 2", "Option 1")),
  Location = factor(c("California", "Georgia", "Texas", "Texas", "Georgia", "Georgia", "California", "Georgia", "Texas", "Texas", "California", "Georgia"))
)

library("tidyverse")
DF <- filter(DF, !is.na(V1))
ggplot(DF, aes(Location, fill = V1)) +
  geom_bar(position = "fill")

(您可能希望将y轴重命名为“比例”或类似的)