[英]Sorting a boxplot in R by the mean of the factor, in a “long” data structure
[英]Boxplot in R for Factor Data
我想為我的項目創建一個箱線圖。 所以我正在研究一個世界幸福數據集,其中包含 Region 作為因素和 happy.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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.