简体   繁体   English

ggplot 对于两个不同的数据集有两个不同的 y 轴

[英]ggplot wih two different y-axis for two different datasets

I'm trying to use the main y-axis for mean_section_eur, and use the second y-axis for incr_eur, currently the output is not what I want since both of the data still use the main y-axis.我正在尝试将主 y 轴用于 mean_section_eur,并将第二个 y 轴用于 incr_eur,目前输出不是我想要的,因为两个数据仍使用主 y 轴。 Here's my data:这是我的数据:

tibble::tribble(
                                                      ~Wells_per_section, ~mean_section_eur,        ~incr_eur,
                                                                      1L,  746279.041157111, 746279.041157111,
                                                                      2L,  1431269.95778565, 684990.916628538,
                                                                      3L,  2108357.80794982,  677087.85016417,
                                                                      4L,  2772843.81583265, 664486.007882829,
                                                                      5L,  3405157.94680437, 632314.130971724,
                                                                      6L,  3649485.94300659, 244327.996202213,
                                                                      7L,  3815891.88964587, 166405.946639284,
                                                                      8L,  3954427.79923768, 138535.909591812,
                                                                      9L,  4080577.72763043, 126149.928392747,
                                                                     10L,   4191966.2500121, 111388.522381674,
                                                                     11L,  4296140.38025762,  104174.13024552,
                                                                     12L,  4400781.49373418, 104641.113476554,
                                                                     13L,   4499603.6595165,  98822.165782324,
                                                                     14L,  4594918.07191796, 95314.4124014592,
                                                                     15L,  4685908.80682599, 90990.7349080276,
                                                                     16L,  4768224.63681244, 82315.8299864484
                                                      )
sec.axis = sec_axis( trans=~.*1, name="Second Axis")

My code:我的代码:

avg_dual_plot <- function(reservoir_model, simulation_case, optimal_spacing,
                          Wells_per_section, well_eur, tolerance){
  avg_section <- qualified_eur %>%
    dplyr::group_by(Wells_per_section) %>%
    dplyr::summarise(mean_section_eur = mean(section_eur)) %>%
    dplyr::mutate(incr_eur = incrementalEUR(mean_section_eur))
  avg_dual_plots <- avg_section %>%
    ggplot2::ggplot(avg_section, mapping = aes(x = Wells_per_section)) + 
    geom_line(mapping =aes(y = mean_section_eur)) +
    geom_line(mapping =aes(y = incr_eur)) +
    scale_y_continuous(
      # Features of the first axis
      name = "section eur",
      # Add a second axis and specify its features
      sec.axis = sec_axis(~rescale(., c(0, 500000)), name="incr eur"))
  #return(avg_dual_plots)
  return(avg_section)
}

My code is a function based on other functions, so avg_section is a result from another function, and I'm using the avg_section which is the data I provided to make the plot.我的代码是基于其他函数的函数,因此 avg_section 是另一个函数的结果,我正在使用 avg_section 这是我提供的数据来制作绘图。 The result I'm getting is:我得到的结果是: 输出

The output I'm expecting:我期待的输出: 预期产出

Remember, when you add a secondary axis to a plot, it is just an inert annotation.请记住,当您向绘图添加辅助轴时,它只是一个惰性注释。 It in no way changes the appearance of the lines or points on your plot.它绝不会改变绘图上线条或点的外观。 If the lines look wrong without a secondary axis, they will look wrong with one too.如果没有辅助轴的线条看起来不对,那么使用辅助轴它们也会看起来不对。

What you need to do is multiply (or divide, or otherwise transform) one of your data series so that it is the size you want it on the plot.您需要做的是乘以(或除以,或以其他方式转换)您的数据系列之一,使其成为您想要在绘图上的大小。 The secondary axis takes the inverse transformation simply so that we can interpret the numbers of the transformed series correctly.次轴简单地进行逆变换,以便我们可以正确解释转换后的系列的数字。

In your example, the incr_eur line is about one-sixth the vertical size you wanted, so we need to multiply the incr_eur data by 6 to get it the size we want.在您的示例中, incr_eur行大约是您想要的垂直大小的六分之一,因此我们需要将incr_eur数据乘以 6 以获得我们想要的大小。 We then tell sec_axis to show y values that are 1/6 the value of those on the primary y axis:然后我们告诉sec_axis显示的 y 值是主 y 轴上的值的 1/6:

ggplot(avg_section, mapping = aes(x = Wells_per_section)) + 
    geom_line(mapping =aes(y = mean_section_eur),
              color = '#ec7e34') +
    geom_point(aes(y = mean_section_eur), color = '#ec7e34') +
    geom_line(mapping = aes(y = incr_eur * 6),
              color = '#2e4a7d') +
  geom_point(aes(y = incr_eur * 6), color = '#2e4a7d') + 
    scale_y_continuous(
      labels = scales::comma,
      name = "section eur",
      sec.axis = sec_axis(~.x/6, name="incr eur", labels = scales::comma)) +
  lims(x = c(0, 25)) +
  theme_light()

在此处输入图像描述

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

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