使用ggplot2的序数因子的密度直方图

时间:2021-12-25 14:58:49

I have a simple dataset that looks like this:

我有一个简单的数据集,如下所示:

school       score     grade subject
Your school  83.27     1     English
All schools 113.60     6     English

grade is an ordinal factor with 9 levels, school is a factor with 2 levels.

等级是9个等级的序数因子,学校是2个等级的因子。

'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" ...

I'd like to plot a facetted histogram with ggplot2 using the density function. If I try it without density I get this:

我想使用密度函数绘制带有ggplot2的刻面直方图。如果我尝试没有密度,我得到这个:

p <- ggplot(ss, aes(x=grade))
p <- p + geom_histogram() 
p <- p + facet_wrap(~school)

使用ggplot2的序数因子的密度直方图

When I add the density to normalise the y-axis I get this:

当我添加密度以规范化y轴时,我得到:

p <- ggplot(ss, aes(x=grade))
p <- p + geom_histogram(aes(y=..density..)) 
p <- p + facet_wrap(~school)

使用ggplot2的序数因子的密度直方图

What am I missing?

我错过了什么?

1 个解决方案

#1


3  

R cannot calculate density on a factor, even ordered one. Your best bet would be to convert grade to numeric (using for example as.numeric(as.character(x))).

R无法计算因子上的密度,即使是有序因子也是如此。您最好的选择是将成绩转换为数字(例如使用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)

#1


3  

R cannot calculate density on a factor, even ordered one. Your best bet would be to convert grade to numeric (using for example as.numeric(as.character(x))).

R无法计算因子上的密度,即使是有序因子也是如此。您最好的选择是将成绩转换为数字(例如使用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)