[英]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
這是我制作的。 不確定它是否接近您正在尋找的東西,但我正在使用 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.