繁体   English   中英

R:当 Y 轴相同时,在散点 plot 上组合不同的 X 轴?

[英]R: combining different X axes on a scatter plot when the Y axes are the same?

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

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

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

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

由于您没有连同您的问题一起提供数据,我将使用来自 RStudio 的名为mtcars的内置数据集来回答它。

这是使用ggplot的方法。

mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
  geom_point()

mt + facet_grid(vars(cyl), scales = "free")

plot 将如下所示: 在此处输入图像描述

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

这是我制作的。 不确定它是否接近您正在寻找的东西,但我正在使用 ggpubr package 来处理这些:

# 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)

创建此 plot:

在此处输入图像描述

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"))

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

在此处输入图像描述

暂无
暂无

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

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