我有一个简单的数据集,如下所示:
ul
each account in accountlist
li(class='no-bullets'): form(id='get-account-info'): input(type='hidden', name= account)
button(class='btn', action='submit')= account
等级是9个等级的序数因子,学校是2个等级的因素。
school score grade subject
Your school 83.27 1 English
All schools 113.60 6 English
我想使用密度函数绘制带有ggplot2的刻面直方图。如果我尝试没有密度,我得到这个:
'data.frame': 12762 obs. of 4 variables:
$ school : Factor w/ 2 levels "Your school",..: 1 2 2 2 2 2 2 2 2 2 ...
$ score : num 83.3 113.6 109.2 117.4 100.3 ...
$ grade : Ord.factor w/ 9 levels "1"<"2"<"3"<"4"<..: 1 6 6 7 4 3 6 1 6 6 ...
$ subject: chr "English" "English" "English" "English" ...
当我添加密度以标准化y轴时,我得到:
p <- ggplot(ss, aes(x=grade))
p <- p + geom_histogram()
p <- p + facet_wrap(~school)
我错过了什么?
答案 0 :(得分:3)
R无法计算因子上的密度,即使是有序因子也是如此。您最好的选择是将grade
转换为数字(例如使用as.numeric(as.character(x))
)。
xy <- data.frame(school = sample(c("your", "all"), size = 100, replace = TRUE),
grade = sample(1:10, size = 100, replace = TRUE))
xy$grade.factor <- factor(xy$grade, ordered = TRUE)
library(ggplot2)
# doesn't work for factors
ggplot(xy, aes(x = grade.factor)) +
theme_bw() +
geom_histogram(aes(y = ..density..)) +
facet_wrap(~ school)
# works for integers/numeric
ggplot(xy, aes(x = grade)) +
theme_bw() +
geom_histogram(aes(y = ..density..)) +
facet_wrap(~ school)