geom_col分配了错误的自变量 [英] geom_col is assigning the wrong independent variable

查看:202
本文介绍了geom_col分配了错误的自变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple two variable data frame with a third variable acting as a factor

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绘制为深度的函数,因此深度应该是离散变量.但是,当我使用

I want to create a bar graph of the absolute values so I am using geom_col(). I want to plot Rb as a function of Depth therefore Depth should be the discrete variable. However, when I plot using

ggplot (DF, aes(x=Depth, y=Rb))+
geom_col()

该图具有水平条,该水平条显示每个离散Rb读数处的深度.我想查看每个离散深度处的Rb值. 反转x和y会遇到相同的问题,仅使用竖线即可

the graph has horizontal bars that show how much Depth there is at each discrete Rb reading. I want to see the value of Rb at each discrete Depth. Reversing the x and y gives the same problem, just with vertical bars

ggplot (DF, aes(x=Rb, y=Depth))+
geom_col()

我也尝试过使用geom_bar(stat = 'identity')进行相同操作,但是仍然是相同的问题.

I have also tried the same with geom_bar(stat = 'identity'), but it's still the same problem.

编辑-如果任何人都可以解释为什么会这样做

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),则会像上图中那样对数据进行分组,并且条形图是垂直的,而不是水平的? 谢谢, 杰里米

For some reason, dividing the Rb values by 10 sorts the problem out?? Dividing by any number greater than 2 works but if you divide by 1 or 2 (Rb, Rb/1, or Rb/2) it groups the data like in the above graphs and the bars are vertical, not horizontal?? Thanks, Jeremy

推荐答案

您可以使用"orientation"参数在geom_col()中强制设置方向.

You may force the orientation in geom_col() with the "orientation" argument.

来自?geom_col:

orientation层的方向.默认值(NA)自动从美学映射确定方向.在极少数情况下,如果此操作失败,则可以通过将orientation设置为"x""y"来明确给出.

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]不同地对待每个轴,因此可以具有两个方向.通常,方向很容易从给定的映射和使用的位置比例尺的组合中得出.因此,默认情况下,ggplot2将尝试猜测该层应具有的方向.在极少数情况下,方向是模棱两可的,并且猜测可能会失败.在那种情况下,可以直接使用orientation参数指定方向,该参数可以是"x""y".该值给出了几何图形应沿其运行的轴,"x"是您期望几何图形的默认方向.

[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.

另请参见问题 geom_col意外的(?)定向.

ggplot (DF, aes(x = Depth, y = Rb)) +
  geom_col(orientation = "x")

这篇关于geom_col分配了错误的自变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆