简体   繁体   English

如何将自动绘图中呈现的两个图中的线系列组合成一个 plot,共享相同的 y 轴和两个不同的 x 轴?

[英]How to combine the line series in two plots rendered in autoplot into a single plot, sharing the same y-axis and with two different x-axes?

In the below code, I render two separate plots using the autoplot() function. The first plot ( transPlot1 ) allows the user to transform the data series via slider input, whereas the 2nd plot shows the original untransformed series ( transPlot2 ) and is completely static. I'd like to show the two data series in the same plot, with the first series shown on the primary x-axis (on the left, and its x-axis values vary depending on the slider input) and the 2nd series shown on the secondary x-axis (on the right, and it always remains fixed as this shows the original data before transformation).在下面的代码中,我使用autoplot() function 渲染了两个单独的图。第一个 plot ( transPlot1 ) 允许用户通过 slider 输入转换数据系列,而第二个 plot 显示原始未转换系列 ( transPlot2 ) 并且完全static。我想在同一个 plot 中显示两个数据系列,第一个系列显示在主 x 轴上(在左侧,其 x 轴值根据 slider 输入而变化)和第二个系列显示在次要 x 轴上(在右侧,它始终保持固定,因为它显示了转换前的原始数据)。 I'd like to place the two series on the same plot so the user can see the effect on the transformation on the shape of the data.我想将这两个系列放在同一个 plot 上,这样用户就可以看到数据形状转换的效果。 This is the sort of thing that was easy to do in XLS but I have migrated to R.这是在 XLS 中很容易做到的事情,但我已经迁移到 R。

Any recommendations for how to do this?有关如何执行此操作的任何建议?

Code:代码:

library(feasts)
library(fabletools)
library(ggplot2)
library(shiny)
library(tsibble)

DF <- data.frame(
  Month = c(1:12),
  StateX = c(59,77,45,42,32,26,27,21,19,22,24,10)
)
DF1 <- DF %>% as_tsibble(index = Month)

library(shiny)
ui <- fluidPage(
  sliderInput("lambda","Transformation lambda:",min=-2,max=2,value=0.5,step = 0.1),
  plotOutput("transPlot1"),
  plotOutput("transPlot2"),
)
server <- function(input, output) {
  
  output$transPlot1 <- renderPlot({
    DF1 %>%
      autoplot(box_cox(StateX, input$lambda)) +
      labs(y = "",
           title = latex2exp::TeX(paste0(
             "Transformed units reaching StateX",
             round(input$lambda,2))))
  })
  
  output$transPlot2 <- renderPlot({
    autoplot(DF1, StateX) +
      labs(y = "")
  })  
}
shinyApp(ui, server)

One option to achieve your desired result would be to add your untransformed series to the autoplot via a geom_line and to add a secondary scale.实现所需结果的一种选择是通过autoplot将未转换的系列添加到自动geom_line并添加辅助比例。 As usual when adding a secondary scale to a ggplot this requires some transformation of the data to be displayed on the secondary axis.像往常一样,在向ggplot添加辅助刻度时,这需要对要显示在辅助轴上的数据进行一些转换。 To this end I added a reactive to do all the transformations, including the Box-Cox transformation and the transformation needed for the secondary scale.为此,我添加了一个反应来完成所有的转换,包括 Box-Cox 转换和二级尺度所需的转换。 For the last part I use scales::rescale .对于最后一部分,我使用scales::rescale

library(feasts)
#> Loading required package: fabletools
library(fabletools)
library(ggplot2)
library(shiny)
library(tsibble)
library(dplyr)
library(scales)

DF <- data.frame(
  Month = c(1:12),
  StateX = c(59, 77, 45, 42, 32, 26, 27, 21, 19, 22, 24, 10)
)
DF1 <- DF %>% as_tsibble(index = Month)

library(shiny)
ui <- fluidPage(
  sliderInput("lambda", "Transformation lambda:", min = -2, max = 2, value = 0.5, step = 0.1),
  plotOutput("transPlot1")
)
server <- function(input, output) {
  DF1_trans <- reactive({
    DF1 %>%
      mutate(
        state_x_box = box_cox(StateX, input$lambda),
        state_x_raw = scales::rescale(StateX, to = range(state_x_box))
      )
  })
  
  output$transPlot1 <- renderPlot({
    to_range <- range(DF1_trans()$StateX)

    DF1_trans() %>%
      autoplot(state_x_box) +
      geom_line(data = DF1_trans(), aes(Month, state_x_raw, color = "Untransformed Series")) +
      scale_y_continuous(
        sec.axis = sec_axis(
          name = "Untransformed",
          trans = ~ scales::rescale(.x, to = to_range))) +
      labs(
        y = "",
        title = latex2exp::TeX(paste0(
          "Transformed units reaching StateX",
          round(input$lambda, 2)
        ))
      )
  })
}
shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:3492

暂无
暂无

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

相关问题 如何在R中重叠具有相同y轴但不同x轴的2个图 - How to superimpose 2 plots with same y-axis, but different x-axes in R 如何在同一面板的同一X轴上绘制具有两个不同y轴范围的点? - How do I plot points with two different y-axis ranges on the same panel in the same X axis? 如何绘制镜像图? (两个图共享相同的x轴,但一个图上下颠倒-在该轴下方) - How to plot mirror plots? (Two plots sharing the same x-axis, but one is upside down - below the axis) 具有相同 arguments 的两个图在 ggplot 中具有不同的 x 轴 - Two graphs with the same arguments have different x-axes in ggplot 合并两个不同的图:一个在X轴上,另一个在Y轴上 - Merge two different plots: one in the X-axis and the other in the Y-axis 如何使用ggplot将两个不同比例的y轴放在绘图的同一侧? - How to put two y-axis with different scale on the same side of the plot with ggplot? ggplot2-如何使用主要和次要y轴在同一图上对具有不同比例的两个变量进行箱图绘制? - ggplot2 - How to boxplot two variables with different scales on same plot using a primary and secondary y-axis? 如何 plot 两个 Y 轴刻度相同但值不同的图 - How to plot two graphs with same Y-axis scale but different values R 中的 Plot 行,两个 y 轴使用 twoord.plot:缺少第一个 y 轴的值 - Line Plot in R with two y-axes using twoord.plot: Values of first y-axis missing 如何在网格中绘制多个时间序列图,其中每个图都有两个 y 轴? - How to plot multiple time series plots in a grid, where each plot has two y axes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM