I have a dataset that measures population index against temperature in warmer months and in colder months.
Instead of making a scatter plot that shows the summer temp against the pop index and then another showing the winter temp against pop index I'd like to combine the 2 - however the X axis in the winter dataset typically runs from -1 degrees to 10 degrees, and in the summer 10-25 degrees. Since they are the same scale is there any way I can combine the 2 X axes to have them next to eachother so summer and winter temps against population index can be shown in one scatter plot?
Right now I have plot(winter_RA, pop_RA)
and plot(summer_RA, pop_RA)
; I tried plot(winter_RA summer_RA, pop_RA)
but it didn't come out showing the full range of temperatures on the X axis.
I'm completely new to R so sorry if this has an obvious answer. TIA
CodePudding user response:
Since you have not provided the data along with your question, I'll answer it with the in-built dataset from RStudio called mtcars
.
Here's how you can do it using ggplot
.
mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl)))
geom_point()
mt facet_grid(vars(cyl), scales = "free")
As you can see in the code, you'll need to use facet_grid
and provide vars
i.e. variable. In your case, it will be seasons. And set scales = "free"
.
CodePudding user response:
Here is what I crafted. Not sure if it is close to what you are looking for, but I'm using the ggpubr package for these:
# 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)
Creates this 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"))
Which makes this:
As Vishal already noted, since there is no data present, it would be a bit easier, as you could potentially factor what is there. For example, you could use pivot_longer like so:
# 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"))
Which makes this:
Can alternatively add lines with:
# 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"))
Which makes this slightly better looking line and scatterplot: