简体   繁体   English

R 中的 ggplot 中的辅助 y 轴?

[英]Secondary y-axis in ggplot in R?

Went through a few of secondary axis solutions proposed here but didn't get it right.浏览了这里提出的一些secondary axis解决方案,但没有做对。 I am trying to plot Elevation on the left y-axis and FlowA & Flowb on the right y-axis .我正在尝试 plot Elevation on the left y-axisFlowA & Flowb on the right y-axis My sample code will do the Elevation plotting however, struggling to get the FlowA & FlowB variables on the secondary axis .但是,我的示例代码将进行Elevation plotting ,努力在secondary axis获取FlowA & FlowB变量。 Any help would be appreciated.任何帮助,将不胜感激。

library(lubridate)
library(tidyverse)

set.seed(123)

FakeData <- data.frame(Date = seq(as.Date("2001-01-01"), to= as.Date("2001-12-31"), by="day"),
                 Elevation = runif(365, 806.8,807.8),
                 FlowA = runif(365,8,15),
                 FlowB = runif(365,1,3))
ggplot(FakeData, aes(x = Date, y = Elevation))+
  geom_line()

I would suggest next approach.我会建议下一种方法。 Also to mention that the output would depend on your data.还要提到 output 将取决于您的数据。 Here all Elevation values are close to 800. For second axis, you have to define a scaling factor around all variables so that they are properly showed.这里所有的Elevation值都接近 800。对于第二个轴,您必须围绕所有变量定义一个比例因子,以便正确显示它们。 Next the code:接下来是代码:

library(lubridate)
library(tidyverse)

set.seed(123)
#Data
FakeData <- data.frame(Date = seq(as.Date("2001-01-01"), to= as.Date("2001-12-31"), by="day"),
                       Elevation = runif(365, 806.8,807.8),
                       FlowA = runif(365,8,15),
                       FlowB = runif(365,1,3))
#Scale factor
scalefactor <- max(FakeData$Elevation)/max(max(FakeData$FlowA),max(FakeData$FlowB))
#Plot
ggplot(FakeData, aes(x = Date))+
  geom_line(aes(y = Elevation,group=1,color='Elevation'),show.legend = T)+
  geom_line(aes(y = FlowA*scalefactor, color = 'FlowA'))+
  geom_line(aes(y = FlowB*scalefactor, color = 'FlowB'))+
  scale_y_continuous(sec.axis = sec_axis(~./scalefactor, name = 'Flow A and Flow B'))

The output: output:

在此处输入图像描述

The solution in here works just fine for me. 这里的解决方案对我来说很好。 But I would like to add some adjustments because the code will be slightly different for your data.但我想添加一些调整,因为您的数据的代码会略有不同。 According to the solution, we will use 3 geom objects that represent the elevation, FlowA, and FlowB.根据解决方案,我们将使用 3 个几何对象来表示高程、FlowA 和 FlowB。 We will also make the secondary axis for FlowA and FlowB.我们还将为 FlowA 和 FlowB 制作辅助轴。

ggplot(FakeData, aes(x = Date))+
  geom_line(aes(y = Elevation)) +
  geom_col(aes(y = FlowA), fill="blue") +
  geom_col(aes(y = FlowB), fill='red')+
  scale_y_log10(sec.axis = sec_axis(~ .*1, labels = scales::number_format(scale=1/10),name="Flow"))

In the code above, I will show the elevation as a line plot and flows as a bar plot.在上面的代码中,我将高程显示为一条线 plot 并以条形 plot 显示。 Why did I use the logarithmic scale here?为什么我在这里使用对数刻度? Because the distance of the elevation's value range(between 806.8 until 807.8) and the flows' value range is very far.因为海拔的取值范围(806.8到807.8之间)和流量取值范围的距离很远。 If you proceed with a regular y-axis ( scale_y_continuous() ), you will have this plot below:如果您继续使用常规 y 轴( scale_y_continuous() ),您将在下面看到这个 plot: 在此处输入图像描述 See that the plot is not so meaningful.看到 plot 没有那么有意义。 You can't see clearly how the flows change over time.您无法清楚地看到流量如何随时间变化。 Here's what it looks in logarithmic scale:这是它在对数刻度中的样子: 在此处输入图像描述 I use the logarithmic scale for the left y-axis and regular scale on the right y-axis.我在左侧 y 轴上使用对数刻度,在右侧 y 轴上使用常规刻度。 Now we can see clearly the changes in the flows over time.现在我们可以清楚地看到流量随时间的变化。 The Elevation will definitely be a straight line because you set it to be a random uniform distribution.高程肯定是一条直线,因为您将其设置为随机均匀分布。

However, personally, I don't suggest you use a double y-axis because it confuses the plot user.但是,就个人而言,我不建议您使用双 y 轴,因为它会使 plot 用户感到困惑。 I suggest you split the plot into two different plots.我建议您将 plot 分成两个不同的图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM