ggplot2中两个数据集的两个具有不同比例的y轴 [英] two y-axes with different scales for two datasets in ggplot2

查看:85
本文介绍了ggplot2中两个数据集的两个具有不同比例的y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个共享相同x值的数据集(可以组合成一个),而y值却不同-我想在一个数据集中绘制y值,并将y轴放在绘制,同时在另一个数据集中绘制y值,并将y轴放在同一图的右侧.当然,两个y轴值的相对比例是不同的(实际上应根据第一个数据集中的y值进行调整".两个数据集中的点将使用不同的颜色以区分这两个比例

I have two datasets (can be combined into a single one) that share common x values, while the y values are different - I want to plot the y values in one dataset and put the y-axis on the left of the plot, while plotting the y values in the other dataset and put the y-axis on the right of the same plot. Of course, the relative scales for the two y-axis values are different (actually should be "adjusted" according to the y values in the first dataset. The points in the two datasets will be in different colors in order to distinguish the two scales.

示例如下:

d1 = data.frame(x=c(100, 200, 300, 400), y=seq(0.1, 0.4, by=0.1)) # 1st dataset
d2 = data.frame(x=c(100, 200, 300, 400), y=seq(0.8, 0.5, by=-0.1)) # 2nd dataset
p1 = ggplot(data = d1, aes(x=x, y=y)) + geom_point()
p2 = ggplot(data = d2, aes(x=x, y=y)) + geom_point() +
  scale_y_continuous(position = "right")
p1
p2

ggplot2中,我无法执行p1+p2,因为它将显示错误消息Error: Don't know how to add o to a plot.请帮忙.谢谢!

In ggplot2, I cannot do p1+p2 as it will show an error message Error: Don't know how to add o to a plot. Please help. Thank you!

推荐答案

在最前面,这种类型的图是一个很好的例子,说明了为什么要花很长时间才能将第二个轴插入到ggplot2中:很容易造成混淆,导致误解.因此,我将在这里竭尽全力提供去往何处的多个指标.

Up front, this type of graph is a good example of why it took so long to get a second axis into ggplot2: it can very easily be confusing, leading to mis-interpretations. As such, I'll go to pains here to provide multiple indicators of what goes where.

首先,使用 sec_axis 要求在原始轴上进行转换.通常以截距/斜率公式(例如~ 2*. + 10)的形式完成此操作,其中周期表示要缩放的值.在这种情况下,我认为我们可以简单地使用~ 2*.

First, the use of sec_axis requires a transformation on the original axis. This is typically done in the form of an intercept/slope formula such as ~ 2*. + 10, where the period indicates the value to scale. In this case, I think we could get away with simply ~ 2*.

但是,这意味着您需要在原始轴上绘制所有数据,这意味着需要将d2$y预缩放到d1$y的范围.足够简单,您只需要进行逆向转换,就像在sec_axis中将要使用的那样.

However, this implies that you need to plot all data on the original axis, meaning you need d2$y to be pre-scaled to d1$y's limits. Simple enough, you just need the reverse transformation as what will be used in sec_axis.

不过,为了使用ggplot2的分组,我将把数据合并到单个data.frame中.

I'm going to combine the data into a single data.frame, though, in order to use ggplot2's grouping.

d1 = data.frame(x=c(100, 200, 300, 400), y=seq(0.1, 0.4, by=0.1)) # 1st dataset
d2 = data.frame(x=c(100, 200, 300, 400), y=seq(0.8, 0.5, by=-0.1)) # 2nd dataset
d1$z <- "data1"
d2$z <- "data2"
d3 <- within(d2, { y = y/2 })
d4 <- rbind(d1, d3)
d4
#     x    y     z
# 1 100 0.10 data1
# 2 200 0.20 data1
# 3 300 0.30 data1
# 4 400 0.40 data1
# 5 100 0.40 data2
# 6 200 0.35 data2
# 7 300 0.30 data2
# 8 400 0.25 data2

为了控制所有组件的颜色,我将对其进行手动设置:

In order to control color in all components, I'll set it manually:

mycolors <- c("data1"="blue", "data2"="red")

最后,剧情:

library(ggplot2)
ggplot(d4, aes(x=x, y=y, group=z, color=z)) +
  geom_path() +
  geom_point() +
  scale_y_continuous(name="data1", sec.axis = sec_axis(~ 2*., name="data2")) +
  scale_color_manual(name="z", values = mycolors) +
  theme(
    axis.title.y = element_text(color = mycolors["data1"]),
    axis.text.y = element_text(color = mycolors["data1"]),
    axis.title.y.right = element_text(color = mycolors["data2"]),
    axis.text.y.right = element_text(color = mycolors["data2"])
  )

但是,坦率地说,我不喜欢不同的斜坡.也就是说,蓝色轴上的两个块为0.1,而红色轴上的两个块为0.2.如果您要谈论两个截然不同的事物",那么这可能很好.但是,如果两条直线的斜率可以直接比较,则您可能希望将每个块的大小保持相同.为此,我们将使用仅截距的变换,而不改变斜率.这意味着in-data.frame转换可能是y = y - 0.4,情节补语~ . + 0.4,产生:

Frankly, though, I don't like the different slopes. That is, two blocks on the blue axis are 0.1, whereas on the red axis they are 0.2. If you're talking about two vastly different "things", then this may be fine. If, however, the slopes of the two lines are directly comparable, then you might prefer to keep the size of each block to be the same. For this, we'll use a transformation of just an intercept, no change in slope. That means the in-data.frame transformation could be y = y - 0.4, and the plot complement ~ . + 0.4, producing:

PS:提示来自 https://stackoverflow.com/a/45683665/3358272 https://stackoverflow.com/a/6920045/3358272

这篇关于ggplot2中两个数据集的两个具有不同比例的y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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