R 中用于因子数据的箱线图
Boxplot in R for Factor Data
我想为我的项目创建一个箱线图。所以我正在研究一个世界幸福数据集,其中包含区域作为因素和 happiness.score 作为数值等。我想比较区域之间的平均幸福分数,这是我写的:
ggplot(data=happiness, mapping = aes(x = Region, y = Happiness.Score)) + geom_boxplot()
但是输出很奇怪,如下所示:
谁能给点建议吗?
如果 'y' 变量是 factor
,就会发生这种情况。将它从 factor
转换为 numeric
,它应该可以工作
happiness$Happiness.Score <- as.numeric(as.character(happiness$Happiness.Score))
ggplot(data=happiness, mapping = aes(x = Region, y = Happiness.Score)) +
geom_boxplot()
具有
的可重现示例
data(iris)
ggplot(data = iris, mapping = aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
现在,检查 'Sepal.Length' 作为 factor
ggplot(data = iris, mapping = aes(x = Species, y = factor(Sepal.Length))) +
geom_boxplot()
我想为我的项目创建一个箱线图。所以我正在研究一个世界幸福数据集,其中包含区域作为因素和 happiness.score 作为数值等。我想比较区域之间的平均幸福分数,这是我写的:
ggplot(data=happiness, mapping = aes(x = Region, y = Happiness.Score)) + geom_boxplot()
但是输出很奇怪,如下所示:
谁能给点建议吗?
如果 'y' 变量是 factor
,就会发生这种情况。将它从 factor
转换为 numeric
,它应该可以工作
happiness$Happiness.Score <- as.numeric(as.character(happiness$Happiness.Score))
ggplot(data=happiness, mapping = aes(x = Region, y = Happiness.Score)) +
geom_boxplot()
具有
的可重现示例data(iris)
ggplot(data = iris, mapping = aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
现在,检查 'Sepal.Length' 作为 factor
ggplot(data = iris, mapping = aes(x = Species, y = factor(Sepal.Length))) +
geom_boxplot()