【问题标题】:R: combining different X axes on a scatter plot when the Y axes are the same?R:当 Y 轴相同时,在散点图上组合不同的 X 轴?
【发布时间】:2022-01-24 02:34:36
【问题描述】:

我有一个数据集,可以在温暖月份和寒冷月份根据温度测量人口指数。

我不想制作一个散点图来显示夏季温度与流行指数的对比,然后另一个显示冬季温度与流行指数的对比,我想将 2 结合起来 - 但是冬季数据集中的 X 轴通常从 -1 开始度到10度,夏天10-25度。由于它们的比例相同,有什么方法可以将 2 个 X 轴组合在一起,让它们彼此相邻,这样夏季和冬季相对于人口指数的温度就可以显示在一个散点图中?

现在我有plot(winter_RA, pop_RA)plot(summer_RA, pop_RA);我尝试了plot(winter_RA+summer_RA, pop_RA),但它没有显示 X 轴上的全部温度范围。

如果这有一个明显的答案,我对 R 完全陌生,很抱歉。 TIA

【问题讨论】:

    标签: r


    【解决方案1】:

    这是我制作的。不确定它是否接近您正在寻找的东西,但我正在使用 ggpubr 包来解决这些问题:

    # Load ggpubr library for scatterplot:
    library(ggpubr)
    
    # Create temperature by population data frame:
    cold_temp <- c(3,5,2,1,0,20)
    hot_temp <- c(70,60,80,50,99,80)
    pop <- c(500, 600, 200, 400, 300, 100)
    temps <- data.frame(cold_temp,hot_temp, pop)
    
    # Create scatterplot colored by temps:
    ggscatter(temps,
           x="pop",
           y=c("hot_temp", "cold_temp"),
           merge = T)
    

    创建此图:

    Can decorate it more with this code:
    
    # Create scatterplot colored by temps:
    ggscatter(temps,
           x="pop",
           y=c("hot_temp", "cold_temp"),
           merge = T)+
      labs(title = "Average Temperature High/Cold by Population",
           subtitle = "Scatterplot Using GGPUBR Package",
           caption = "Data obtained from (insert place).",
           x="Population",
           y="Temperature (F)")+
      theme_bw()+
      theme(plot.title = element_text(face = "bold"),
            plot.caption = element_text(face = "italic"))
    

    这是什么原因:

    正如 Vishal 已经指出的那样,由于没有数据存在,这会更容易一些,因为您可能会考虑那里的数据。例如,您可以像这样使用 pivot_longer:

    # Load tidyverse for "pivot_longer" function:
    library(tidyverse)
    
    # Pivot data:
    pivot_temp <- temps %>% 
      pivot_longer(cols = c(hot_temp,cold_temp),
                   names_to = "Temperature_Type",
                   values_to = "Fahrenheit")
    
    # Make faceted plot:
    ggscatter(pivot_temp,
              x="pop",
              y="Fahrenheit",
              color = "Temperature_Type",
              palette = "jco")+
      facet_wrap(~Temperature_Type)+
      labs(title = "Average Temperature High/Cold by Population",
           subtitle = "Scatterplot Using GGPUBR Package",
           caption = "Data obtained from (insert place).",
           x="Population",
           y="Temperature (F)")+
      theme_bw()+
      theme(plot.title = element_text(face = "bold"),
            plot.caption = element_text(face = "italic"))
    

    这是什么原因:

    也可以添加行:

      # Make faceted plot:
      ggscatter(pivot_temp,
                x="pop",
                y="Fahrenheit",
                color = "Temperature_Type",
                palette = "jco",
                merge = T)+
        geom_line(aes(color=Temperature_Type))+
        labs(title = "Average Temperature High/Cold by Population",
             subtitle = "Scatterplot Using GGPUBR Package",
             caption = "Data obtained from (insert place).",
             x="Population",
             y="Temperature (F)")+
        theme_bw()+
        theme(plot.title = element_text(face = "bold"),
              plot.caption = element_text(face = "italic"))
    

    这使得这条线和散点图看起来更好看:

    【讨论】:

      【解决方案2】:

      由于您没有提供数据以及您的问题,我将使用 RStudio 的内置数据集 (mtcars) 来回答它。

      以下是使用ggplot 的方法。

      mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
        geom_point()
      
      mt + facet_grid(vars(cyl), scales = "free")
      

      情节将如下所示:

      正如您在代码中看到的,您需要使用 facet_grid 并提供 vars 即变量。在你的情况下,这将是季节。并设置scales = "free"

      【讨论】:

        猜你喜欢
        • 2018-06-19
        • 1970-01-01
        • 2013-01-22
        • 2021-01-09
        • 2022-11-22
        • 2019-07-07
        • 1970-01-01
        • 2020-11-05
        • 2015-11-26
        相关资源
        最近更新 更多