geom_col 分配了错误的自变量
geom_col is assigning the wrong independent variable
我有一个简单的双变量数据框,其中第三个变量作为因子
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 的值。
反转 x 和 y 会产生同样的问题,只是竖线
ggplot (DF, aes(x=Rb, y=Depth))+
geom_col()
我也用 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),它会像上图中那样对数据进行分组,并且条形图是垂直的,而不是水平的? ?
谢谢,
杰里米
根据您的图像,我尝试构建您正在寻找的输出:
ggplot(DF, aes(x=rank(Depth), y=Rb, fill=Litho)) +
geom_bar(stat="identity") +
labs(x="Depth", y="Rb") +
scale_x_continuous(breaks=seq(0,15,1), trans = "reverse") +
scale_y_continuous(position="right") +
coord_flip(xlim=c(0,15)) +
theme(panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(colour = "grey50"))
结果
使用 scale_fill_brewer(palette="Paired") +
是 RColorBrewer::display.brewer.all()
给出的颜色主题。
最后一次尝试
我只是在猜测你真正想要什么。上面显示的对我的代码的修改
ggplot(DF, aes(x=Depth, y=Rb, fill=Litho)) +
geom_bar(stat="identity") +
labs(x="Depth", y="Rb") +
scale_x_continuous(breaks=seq(0,200,50), trans = "reverse") +
scale_y_continuous(position="right") +
coord_flip(xlim=c(0,200)) +
theme(panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(colour = "grey50"))
您可以使用 "orientation"
参数强制 geom_col()
中的方向。
来自 ?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")
我有一个简单的双变量数据框,其中第三个变量作为因子
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 的值。
ggplot (DF, aes(x=Rb, y=Depth))+
geom_col()
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),它会像上图中那样对数据进行分组,并且条形图是垂直的,而不是水平的? ?
根据您的图像,我尝试构建您正在寻找的输出:
ggplot(DF, aes(x=rank(Depth), y=Rb, fill=Litho)) +
geom_bar(stat="identity") +
labs(x="Depth", y="Rb") +
scale_x_continuous(breaks=seq(0,15,1), trans = "reverse") +
scale_y_continuous(position="right") +
coord_flip(xlim=c(0,15)) +
theme(panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(colour = "grey50"))
结果
使用 scale_fill_brewer(palette="Paired") +
是 RColorBrewer::display.brewer.all()
给出的颜色主题。
最后一次尝试
我只是在猜测你真正想要什么。上面显示的对我的代码的修改
ggplot(DF, aes(x=Depth, y=Rb, fill=Litho)) +
geom_bar(stat="identity") +
labs(x="Depth", y="Rb") +
scale_x_continuous(breaks=seq(0,200,50), trans = "reverse") +
scale_y_continuous(position="right") +
coord_flip(xlim=c(0,200)) +
theme(panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(colour = "grey50"))
您可以使用 "orientation"
参数强制 geom_col()
中的方向。
来自 ?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 settingorientation
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 theorientation
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")