我已使用此代码:
data %>%
gather(activity, ct, ActSwim:acteat) %>%
filter(ct == 1) %>%
count(activity, Location) %>%
ggplot(., aes(activity, n)) +
geom_barplot(stat = "identity") +
facet_wrap(~Location, ncol = 1)
如何将这个从计数更改为比例,以解释每个地点调查不同人数的事实?我希望比例能够反映每个方面的比例(在这种情况下是位置)。
答案 0 :(得分:0)
尽管这可能是重复的,但您有两种选择。
ggplot
为您完成转化。ggplot
我赞成#2,因为从长远来看它提供了更大的灵活性。
library(dplyr)
library(ggplot2)
library(scales)
data %>%
gather(activity, ct, ActSwim:acteat) %>%
filter(ct == 1) %>%
count(activity, Location) %>%
group_by(activity) %>%
mutate(percentage = n/sum(n)) %>%
ggplot(., aes(activity, percent)) +
geom_col() +
facet_wrap(~Location, ncol = 1) +
scale_y_continuous(labels = percent)