R(ggplot)-如何基于data.frame生成多个图(面),其中只有一个数值变量和不同的类别。变量
从这类数据开始:R(ggplot)-如何基于data.frame生成多个图(面),其中只有一个数值变量和不同的类别。变量,r,R,从这类数据开始: colnames(mydata)您试图强制数据框列,而ggplot2只需要一个列名。正确的语法更经济: ggplot(mydata, aes(y = freq, x = 1:nrow(mydata), colour = bool)) + geom_point() + facet_wrap(~ digit) + labs(x = "1, 2, ..., n") 另外,我无法检查您堆叠/可视化数据的方式,我不知道这是从何而来,也不知道它代表了什么。您能否分享您的
colnames(mydata)您试图强制数据框列,而
ggplot2
只需要一个列名。正确的语法更经济:
ggplot(mydata, aes(y = freq, x = 1:nrow(mydata), colour = bool)) +
geom_point() +
facet_wrap(~ digit) +
labs(x = "1, 2, ..., n")
另外,我无法检查您堆叠/可视化数据的方式,我不知道这是从何而来,也不知道它代表了什么。您能否分享您的数据作为
dput(mydata)
的结果?检查变量类型后,可以更容易地更正代码。首先,不要在aes()中使用$
。在ggplot(mydata,…)
中指定mydata
的全部要点是,您随后不会将列引用为mydata$freq
,而只引用freq
。
ggplot(mydata, aes(y = mydata$freq, x = seq(1, length(mydata$freq)))) +
geom_point() +
facet_wrap(~ mydata$digit)
dput(mydata)
structure(list(digit = c(1.1, 1.1, 1.1, 1.1, 1.1, 1.2, 1.2, 1.2,
1.2, 1.2, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3, 1.4, 1.4, 1.4, 1.4, 1.4,
2.1, 2.1, 2.1, 2.1, 2.1, 2.1), freq = c(1L, 2L, 20L, 3L, 7L,
8L, 25L, 10L, 60L, 28L, 7L, 0L, 56L, 12L, 7L, 4L, 3L, 87L, 25L,
56L, 167L, 46L, 25L, 75L, 20L, 12L, 15L), bool = structure(c(1L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L), .Label = c("false",
"true"), class = "factor")), .Names = c("digit", "freq", "bool"
), class = "data.frame", row.names = c(NA, -27L))
ggplot(mydata, aes(y = freq, x = 1:nrow(mydata), colour = bool)) +
geom_point() +
facet_wrap(~ digit) +
labs(x = "1, 2, ..., n")