在ggplot2中使用双y轴(第二轴) [英] Dual y axis (second axis) use in ggplot2

查看:1551
本文介绍了在ggplot2中使用双y轴(第二轴)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,就是在前面的文章中介绍的第二轴函数的帮助下使用两个不同的数据

解决方案

您在这里遇到问题,因为第二个轴的转换仅用于创建第二轴 - 它对数据没有影响。您的 bar_data 仍然在原始坐标轴上绘制,由于您的限制,它只会上升到0.5。这可以防止条形图出现。



为了使数据显示在同一范围内,必须对条形数据进行归一化处理,使其落入相同的范围作为点数据。然后,轴转换必须撤销这个标准化,以便获得适当的刻度标签。像这样:

 #标准化程序将条形数据带入点数据范围。这使
#最高的栏位等于最高点。如果你愿意的话,你可以使用不同的
#标准化(例如,这可以是你的例子中的常量15
#,但如果数据
#变化则很脆弱)。
标准化程序< - max(bar_data $ bar_y)/ max(point_data $ point_y)


sec_axis_plot< - ggplot(point_data,
aes(y = point_y ,x = gr))+

#首先绘制条形图,以便它们位于底部。使用geom_col,
#来创建指定高度为y的条。
geom_col(data = bar_data,
aes(x = gr,
y = bar_y / normalizer))+#NORMALIZE Y !!!

#stat =identity和alpha = 1是geom_point的默认值
geom_point(size = 5.5)+

#创建第二个轴。请注意,转换撤消了
#这是我们为geom_col中的bar_y所做的规范化操作。
scale_y_continuous(sec.axis = sec_axis(trans =〜。* normalizer,
name ='bar_y'))+
theme_bw()

这会给你以下图表:



我删除了一些钟声和哨声以使轴特定的东西更加清晰,但是您应该可以将它添加回原来的问题中。尽管有一些注释:


  • 记住第二个轴是由主轴的1-1变换创建的,所以确保它们在转换过程中覆盖相同的限制。如果你有条应该归零,主轴应该包括零的未转换模拟。

  • 确保数据标准化和轴转换撤消以便您的坐标轴与您正在绘制的值保持一致。

I come to encounter a problem that using two different data with the help of second axis function as described in this previous post how-to-use-facets-with-a-dual-y-axis-ggplot.

I am trying to use geom_point and geom_bar but the since the geom_bar data range is different it is not seen on the graph.

Here is what I have tried;

point_data=data.frame(gr=seq(1,10),point_y=rnorm(10,0.25,0.1))
bar_data=data.frame(gr=seq(1,10),bar_y=rnorm(10,5,1))

library(ggplot2)



sec_axis_plot <- ggplot(point_data, aes(y=point_y, x=gr,col="red")) +  #Enc vs Wafer
geom_point(size=5.5,alpha=1,stat='identity')+
geom_bar(data=bar_data,aes(x = gr, y = bar_y, fill = gr),stat = "identity") +
scale_y_continuous(sec.axis = sec_axis(trans=~ .*15,
                                         name = 'bar_y',breaks=seq(0,10,0.5)),breaks=seq(0.10,0.5,0.05),limits = c(0.1,0.5),expand=c(0,0))+

facet_wrap(~gr, strip.position = 'bottom',nrow=1)+
theme_bw()

as it can be seen that bar_data is removed. Is is possible to plot them together in this context ??

thx

解决方案

You're running into problems here because the transformation of the second axis is only used to create the second axis -- it has no impact on the data. Your bar_data is still being plotted on the original axis, which only goes up to 0.5 because of your limits. This prevents the bars from appearing.

In order to make the data show up in the same range, you have to normalize the bar data so that it falls in the same range as the point data. Then, the axis transformation has to undo this normalization so that you get the appropriate tick labels. Like so:

# Normalizer to bring bar data into point data range. This makes
# highest bar equal to highest point. You can use a different
# normalization if you want (e.g., this could be the constant 15
# like you had in your example, though that's fragile if the data
# changes).
normalizer <- max(bar_data$bar_y) / max(point_data$point_y)


sec_axis_plot <- ggplot(point_data,
                        aes(y=point_y, x=gr)) +

  # Plot the bars first so they're on the bottom. Use geom_col,
  # which creates bars with specified height as y.
  geom_col(data=bar_data,
           aes(x = gr,
               y = bar_y / normalizer)) + # NORMALIZE Y !!!

  # stat="identity" and alpha=1 are defaults for geom_point
  geom_point(size=5.5) +

  # Create second axis. Notice that the transformation undoes
  # the normalization we did for bar_y in geom_col.
  scale_y_continuous(sec.axis = sec_axis(trans= ~.*normalizer,
                                         name = 'bar_y')) +
  theme_bw()

This gives you the following plot:

I removed some of your bells and whistles to make the axis-specific stuff more clear, but you should be able to add it back in no problem. A couple of notes though:

  • Remember that the second axis is created by a 1-1 transformation of the primary axis, so make sure they cover the same limits under the transformation. If you have bars that should go to zero, the primary axis should include the untransformed analogue of zero.

  • Make sure that the data normalization and the axis transformation undo each other so that your axis lines up with the values you're plotting.

这篇关于在ggplot2中使用双y轴(第二轴)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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