gpt4 book ai didi

r - GGlot2 Boxplot 只显示平线

转载 作者:行者123 更新时间:2023-12-02 07:32:02 25 4
gpt4 key购买 nike

我已经为此工作了几个小时,似乎无法做到这一点。箱线图只给我平坦的垂直线,它让我发疯。无论有没有因子函数,我都得到相同的输入

ggplot(df2,aes(x = factor(Location),y=Final.Result)) + geom_boxplot()

解决了!有一些数据值,例如“< 0.005”,R 将其作为字符串提取并将所有内容转换为因子。

最佳答案

你得到这些行是因为你的数据框中的变量 Final.Result 是因子而不是数字(你可以用函数 str() 来检查它)。

> str(df2)
'data.frame': 66 obs. of 3 variables:
$ Location : Factor w/ 17 levels "BOON KENG RD BLK 6 (DS)",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Parameter : Factor w/ 54 levels "Aluminium","Ammonia (as N)",..: 37 37 37 37 37 37 37 37 37 37 ...
$ Final.Result: Factor w/ 677 levels "< 0.0005","< 0.001",..: 645 644 654 653 647 643 647 647 646 646 ...

尝试将这些值转换为数字(如在 df2 中没有非数字值)。这仅适用于 df2 但如果您的整个数据框具有那些 "< 0.0005","< 0.001" 值,您应该决定如何处理它们(用 NA 或一些小常量替换)。
df2$Final.Result2<-as.numeric(as.character(df2$Final.Result))
ggplot(df2,aes(x = factor(Location),y=Final.Result2)) + geom_boxplot()

关于r - GGlot2 Boxplot 只显示平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20841733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com