R:ddply-通过将字符串分配为变量名称来聚合数据

时间:2019-02-23 11:38:24

标签: r ggplot2 dplyr aggregate

为我提供了一个包含几列的大数据集。例如

set.seed(1)
x <- 1:15
y <- letters[1:3][sample(1:3, 15, replace = T)]
z <- letters[10:13][sample(1:3, 15, replace = T)]
r <- letters[20:24][sample(1:3, 15, replace = T)]
df <- data.frame("Number"=x, "Section"=y,"Chapter"=z,"Rating"=r)
dput(df)

structure(list(Number = 1:15, Area = structure(c(1L, 2L, 2L, 3L, 1L, 3L, 3L, 2L, 2L, 1L, 1L, 1L, 3L, 2L, 3L), .Label = c("a", "b", "c"), class = "factor"), Section = structure(c(2L, 3L, 3L, 2L, 3L, 3L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 3L, 2L), .Label = c("j", "k", "l"), class = "factor"), Rating = structure(c(2L, 2L, 2L, 1L, 3L, 3L, 3L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 2L), .Label = c("A", "B", "C"), class = "factor")), class = "data.frame", row.names = c(NA,-15L))

我现在想创建按等级和所选类别(例如,通过字符串:

Category<-"Section"
data_count <- ddply(df, .(get(Category),Rating), 'count')
data_rel_freq <- ddply(data_count, .(Rating), transform, rel_freq = freq/sum(freq))
dput(data_rel_freq)

structure(list(get.Category. = structure(c(2L, 2L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("j", "k","l"), class = "factor"), Number = c(4L, 8L, 10L, 12L, 1L, 15L, 2L, 3L, 14L, 7L, 9L, 11L, 13L, 5L, 6L), Area = structure(c(3L, 2L, 1L, 1L, 1L, 3L, 2L, 2L, 2L, 3L, 2L, 1L, 3L, 1L, 3L), .Label = c("a", b", "c"), class = "factor"), Section = structure(c(2L, 2L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("j", "k", "l"), class = "factor"), Rating = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), freq = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), rel_freq = c(0.5, 0.5, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667)), class = "data.frame", row.names = c(NA, -15L))

使用ggplot

ggplot(data_rel_freq,aes(x = Rating, y = rel_freq,fill = get(Category)))+ 
geom_bar(position = "fill",stat = "identity",color="black") +
scale_y_continuous(labels = percent_format())+ 
labs(x = "Rating", y="Relative Frequency")

enter image description here

现在的问题是“ get(Category)”现在被视为新列

    get.Category. Number Area Section Rating freq  rel_freq
1              k      4    c       k      A    1 0.5000000
2              k      8    b       k      A    1 0.5000000
3              j     10    a       j      B    1 0.1428571
4              j     12    a       j      B    1 0.1428571
5              k      1    a       k      B    1 0.1428571
6              k     15    c       k      B    1 0.1428571
7              l      2    b       l      B    1 0.1428571

此外,“数字”列应加起来,例如其他类别(此处:Area)应被删除,对于“ k”部分,我们应仅与一行保持一致,等级为“ A”。

1 个答案:

答案 0 :(得分:1)

我们可以使用count通过在转换为符号(sym之后评估对象标识符'Category'并评估(!!)来获取'Section'列的频率。在ggplot语法内,aes也可以使用符号,并且可以较早地进行评估

library(tidyverse)
library(scales)
library(ggplot2)
df %>% 
    count(!! rlang::sym(Category), Rating) %>%
    group_by(Rating) %>% 
    mutate(rel_freq = n/sum(n)) %>%
    ggplot(., aes(x =Rating, y = rel_freq, fill = !! rlang::sym(Category))) + 
    geom_bar(position = "fill",stat = "identity",color="black") + 
    scale_y_continuous(labels = percent_format())+ 
    labs(x = "Rating", y="Relative Frequency")

-输出

enter image description here