【问题标题】:Overlay two ggplots with different x and y axes scales用不同的 x 和 y 轴比例覆盖两个 ggplots
【发布时间】:2020-04-28 03:08:06
【问题描述】:

假设我有两个 data.frames 如下:

df_1=data.frame(x=c(1:200), y=rnorm(200))

df_2=rpois(100000, 1)
df_2=data.frame(table(df_2))
df_2$df_2=as.numeric(levels(df_2$df_2))[df_2$df_2]

当我单独绘制它们时,我得到:

library(ggplot2)
p1=ggplot() +
  geom_line(data=df_1, aes(x=x,y=y))

print(p1)

p2=ggplot() +
  geom_line(data=df_2, aes(x=df_2,y=Freq))

print(p2)

这两个图有不同的 x 和 y 轴。

如何将两个图叠加为一个?

谢谢

【问题讨论】:

    标签: r ggplot2 plot overlay axes


    【解决方案1】:

    这是另一种选择。我们可以使用scale_*_continuous() 函数中的sec.axis 参数通过转换将数据相互映射。

    ggplot() +
      geom_line(data=df_1, aes(x=x,y=y))+
      geom_line(data=df_2, aes(x=df_2*25,y=Freq/9000))+
      scale_x_continuous(sec.axis = sec_axis(trans = ~./25))+
      scale_y_continuous(sec.axis = sec_axis(trans = ~.*9000))
    

    重新缩放是任意的,所以你可以玩它直到你觉得合适为止。

    【讨论】:

    • 嗨,谢谢。但如果我重新调整比例,我会失去分布的原始形状。
    • 我不同意。通过乘法转换数据不会改变分布的形状。
    【解决方案2】:

    由于它们没有匹配的比例,ggplot 不允许我们将它们一个一个地绘制在另一个之上。 (ggplot 甚至不允许两个 y 尺度)为了解决这个问题,我们必须将它们视为图像而不是绘图。

    为了使 to 图像尽可能相似,我们需要去除除线条之外的所有内容。拥有轴、标签、标题等会“错位”“图”。

    library(ggplot2)
    library(magick)
    #> Linking to ImageMagick 6.9.7.4
    #> Enabled features: fontconfig, freetype, fftw, lcms, pango, x11
    #> Disabled features: cairo, ghostscript, rsvg, webp
    
    df_1 <- data.frame(x=c(1:200), y=rnorm(200))
    
    df_2 <- rpois(100000, 1)
    df_2 <- data.frame(table(df_2))
    df_2$df_2 <- as.numeric(levels(df_2$df_2))[df_2$df_2]
    
    p1 <- ggplot() +
      geom_line(data=df_1, aes(x=x,y=y)) +
      theme_void() +
      theme(
        panel.background = element_rect(fill = "transparent"), # bg of the panel
        plot.background = element_rect(fill = "transparent", color = NA), # bg of the plot
        panel.grid.major = element_blank(), # get rid of major grid
        panel.grid.minor = element_blank(), # get rid of minor grid
        legend.background = element_rect(fill = "transparent"), # get rid of legend bg
        legend.box.background = element_rect(fill = "transparent") # get rid of legend panel bg
      )
    ggsave(filename = 'plot1.png', device = 'png', bg = 'transparent')
    #> Saving 7 x 5 in image
    
    p2 <- ggplot() +
      geom_line(data=df_2, aes(x=df_2,y=Freq)) +
      theme_void() +
      theme(
        panel.background = element_rect(fill = "transparent"), # bg of the panel
        plot.background = element_rect(fill = "transparent", color = NA), # bg of the plot
        panel.grid.major = element_blank(), # get rid of major grid
        panel.grid.minor = element_blank(), # get rid of minor grid
        legend.background = element_rect(fill = "transparent"), # get rid of legend bg
        legend.box.background = element_rect(fill = "transparent") # get rid of legend panel bg
      )
    ggsave(filename = 'plot2.png', device = 'png', bg = 'transparent')
    #> Saving 7 x 5 in image
    
    cowplot::plot_grid(p1, p2, nrow = 1)
    

    两个情节:

    
    plot1 <- image_read('plot1.png')
    plot2 <- image_read('plot2.png')
    
    img <- c(plot1, plot2)
    
    image_mosaic(img)
    

    由reprex package (v0.3.0) 于 2020-01-10 创建

    两个“情节”结合起来:

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 2018-12-05
      相关资源
      最近更新 更多