gpt4 book ai didi

r - geom_col 分配了错误的自变量

转载 作者:行者123 更新时间:2023-12-04 09:51:19 29 4
gpt4 key购买 nike

我有一个简单的两个变量数据框,第三个变量作为一个因素

DF <- data.frame(Depth = c(8.6, 19.6, 42.6, 60.6, 79.4, 101.4, 121.4, 137.6, 163, 180),
Rb = c(103, 59, 99, 53, 107, 87, 52, 33, 105, 49),
Litho = as.factor(c(1,2,1,2,1,1,2,2,1,2)))

我想创建一个绝对值的条形图,所以我使用 geom_col() .我想将 Rb 绘制为深度的函数,因此深度应该是离散变量。但是,当我绘制使用
ggplot (DF, aes(x=Depth, y=Rb))+
geom_col()

该图有水平条,显示每个离散 Rb 读数的深度。我想查看每个离散深度处的 Rb 值。
enter image description here
反转 x 和 y 会产生同样的问题,只是使用竖线
ggplot (DF, aes(x=Rb, y=Depth))+
geom_col()

enter image description here
我也尝试过 geom_bar(stat = 'identity') ,但它仍然是同样的问题。

编辑 - 如果有人可以解释原因,此方法有效
ggplot (DF, aes(x=Depth, y=Rb/10, fill=Litho)) +
geom_bar(stat='identity') +
labs(x="Depth", y="Rb") +
scale_x_continuous (trans = "reverse") +
scale_y_continuous (position = "right") +
coord_flip()

出于某种原因,将 Rb 值除以 10 可以解决问题?除以任何大于 2 的数字都有效,但如果除以 1 或 2(Rb、Rb/1 或 Rb/2),它会将数据分组,如上图所示,条形是垂直的,而不是水平的?
enter image description here
谢谢,
杰里米

最佳答案

您可以在 geom_col() 中强制定向与 "orientation"争论。
来自 ?geom_col :

orientation The orientation of the layer. The default (NA) automatically determines the orientation from the aesthetic mapping. In the rare event that this fails it can be given explicitly by setting orientation to either "x" or "y".

[geom_col] treats each axis differently and, thus, can thus have two orientations. Often the orientation is easy to deduce from a combination of the given mappings and the types of positional scales in use. Thus, ggplot2 will by default try to guess which orientation the layer should have. Under rare circumstances, the orientation is ambiguous and guessing may fail. In that case the orientation can be specified directly using the orientation parameter, which can be either "x" or "y". The value gives the axis that the geom should run along, "x" being the default orientation you would expect for the geom.


另见问题 Unexpected (?) orientation with geom_col中的讨论.
ggplot (DF, aes(x = Depth, y = Rb)) +
geom_col(orientation = "x")
enter image description here

关于r - geom_col 分配了错误的自变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62007106/

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