I want to render range plot or scatter plot for temperature time series in R. Basically, for each region, I need to calculate first 10 year and last 10-year' temperature mean and precipitation total sum respectively; then going to make a range plot that reference year' gdp_percapita (let's say gdp_percapita in 1995) against first 10 year and last 10-year' temperature mean and precipitation total sum.
reproducible data:
Here is the reproducible data that simulated with actual temperature time series:
dat= data.frame(index = rep(c('dex111', 'dex112', 'dex113','dex114','dex115'), each = 30), year =1980:2009,
region= rep(c('Berlin','Stuttgart','Böblingen','Wartburgkreis','Eisenach'), each=30),
gdp_percapita=rep(sample.int(40, 30), 5),gva_agr_perworker=rep(sample.int(45, 30), 5),
temperature=rep(sample.int(50, 30), 5), precipitation=rep(sample.int(60, 30), 5))
update: Here is what I did so far:
library(tidyverse)
func <- dat %>%
group_by(temperature, precipitation) %>%
summarize_all(funs(mean, sum))
seems I was wrong about to get first ten years and last ten years of mean temperature and total precipitation. Any correction.
func %>%
gather(year, region, temperature, precipitation, gdp_percapita) %>%
separate(col, into = c("Measurement", "stat")) %>%
arrange(region) %>%
mutate_at(vars(col, Measurement), fct_inorder) %>%
spread(col, val)
But above code is not well fit for making plot, don't know what went wrong in my code? Any idea?
I knowggplot2
is amazing to render expected range plot for this data, but my attempt to reshape the data for making plot is not correct. Any way to make this plot in R? How can I make this happen in ggplot2
? Any idea?
update:
not that I choose gdp_percapita
of 2000 for all regions in x-axis while periodic mean temperature difference and precipitation sum difference along the y-axis for all regions.
desired plot:
here is the desired range plot for temperature and precipitation:
How do I accomplish my desired output with minimal code and efficiently? Could someone point me in the right direction?