Home > Enterprise >  Create climate diagram in R with temperature and precipitation
Create climate diagram in R with temperature and precipitation

Time:07-31

I have a dataset like this

Month Year Temperature Precipitation
1 2019 2 30
2 2019 1 40
3 2019 7 50
4 2019 10 60
5 2019 12 60
6 2019 18 70
7 2019 20 80
8 2019 19 90
9 2019 17 80
10 2019 10 50
11 2019 9 40
12 2019 8 40
1 2018 4 10
2 2018 2 20
3 2018 7 70
4 2018 12 30
5 2018 16 30
6 2018 20 20
7 2018 24 20
8 2018 22 40
9 2018 19 40
10 2018 13 50
11 2018 11 20
12 2018 9 10

I would like to create a climate table like enter image description here

I gave that code, but i struggling

ggplot(df, aes(x= Month, y=Precipitation ))  
  geom_bar(stat="identity")   
      scale_x_discrete(limits = month.name)  
  theme(text = element_text(size=15),
        axis.text.x = element_text(angle=90, hjust=1))   ggplot(df, aes(x= Month, y=Temperature ))  
  geom_bar(stat="identity")   
      scale_x_discrete(limits = month.name)  
  theme(text = element_text(size=15),
        axis.text.x = element_text(angle=90, hjust=1))

But error code

argument not used

But the temperature is not coming? I try everything but all wrong. How can I code that, that the mean Month Temperature and mean Precipitation is shown as a climate diagram? I also had problems with adding a title.

CodePudding user response:

Maybe you want something like this, were you use a geom_bar for Precipitation and geom_line for Temperature with two y-axis and a facet_wrap for both years like this:

library(ggplot2)
ggplot(df, aes(x= Month))  
  geom_bar(aes(y= Precipitation/4, fill = "Precipitation"), stat="identity")   
  geom_line(aes(y = Temperature, color = "Temperature"))  
  geom_point(aes(y = Temperature))  
  scale_x_discrete(limits = month.name)  
  scale_y_continuous(
    expression("Average Monthly Temperature " ( degree*C)), 
    sec.axis = sec_axis(~ . * 4, name = "Monthly Precipitation (mm)")
  )  
  scale_colour_manual("", values = c("Temperature" = "black"))  
  scale_fill_manual("", values = "orange")  
  theme(text = element_text(size=15),
        axis.text.x = element_text(angle=90, hjust=1),
        legend.position = "bottom")  
  facet_wrap(~Year)

Created on 2022-07-31 by the enter image description here Code for Average:

library(lubridate)
library(tidyverse)
coeff=5

df %>% 
  mutate(date = dmy(paste(1, Month, Year))) %>% 
  group_by(Month) %>% 
  mutate(mean_temp = mean(Temperature),
         mean_precip = mean(Precipitation)) %>% 
  ggplot(aes(x = date, y = mean_precip))  
  geom_col()  
  geom_line(aes(y = mean_temp*coeff), color = "orange", size=1.5)  
  geom_point(aes(y = mean_temp*coeff), color = "orange", size=2.5) 
  scale_x_date(date_breaks = "1 month",
               date_labels = "%B %y") 
  scale_y_continuous("Average \n Precipitation", 
                     sec.axis = sec_axis(~./coeff, name = "Average \n Temperature"))  
  theme_bw(base_size = 14) 
  labs(x="") 
  ggtitle("Climatogram XXX")
  theme(axis.line.y.right = element_line(color = "orange"), 
        axis.ticks.y.right = element_line(color = "orange"),
        axis.text.y.right = element_text(color = "orange"), 
        axis.title.y.right = element_text(color = "orange"),
        axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))
  )  
  ggtitle("Climatogram for Oslo (1961-1990)")

enter image description here

  •  Tags:  
  • r
  • Related