2

I would like to do a climatographe or a climat chart using ggplot.

My data look like

> data.df
   mese Temperatura.media pioggia
1   Jan              -0.3    46.6
2   Feb               2.6    40.9
3   Mar               7.1    68.5
4   Apr              11.5    72.5
5   May              15.6    92.0
6   Jun              19.1    79.6
7   Jul              21.4    79.2
8   Aug              20.6    81.2
9   Sep              17.2    85.4
10  Oct              11.3    98.9
11  Nov               4.9    93.7
12  Dec               0.8    52.1

I have

data.df$mese<-month(data.df$mese, label = T)

climagg<-ggplot(data=data.df)+
  geom_bar(aes(x=mese, y=pioggia),stat = "identity")
climagg

that give me

bar plot whit ggplot

But I need have 2 axes one left and one right with different range (e.g. climatographe)... but it seem not easy with ggplot.

So the climat chart is may be easyer to do ... with non axes and using the multiplot fonction for ggplot

ggtheme<-theme(strip.text.x = element_text(size=14),
               strip.text.y = element_text(size=14),
               legend.title = element_text(size=14),
               panel.background = element_blank()
)

pioggiagg<-ggplot(data=data.df)+
  geom_bar(aes(x=mese.asdate, y=pioggia),stat = "identity",fill="#67a9cf")+
  geom_text(aes(x=mese.asdate, y=pioggia+3, label=pioggia),size=4)+
  theme(axis.text.y=element_blank(),
        axis.title.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_text(face="bold", size=12))+
  scale_x_discrete("")+
  ggtheme
pioggiagg

tempgg<-ggplot()+
        geom_bar(data=data.df,aes(x=mese.asdate, y=pioggia),stat = "identity",alpha=0)+
        geom_bar(data=data.df,aes(x=mese.asdate, y=temp_max),stat = "identity",alpha=0)+
        geom_bar(data=data.df,aes(x=mese.asdate, y=temp_mini),stat = "identity",alpha=0)+
        geom_rect(data=data.df,stat = "identity",aes(xmin=mese-0.4, xmax=mese+0.4,ymin=temp_mini, ymax=temp_max),fill="#ef8a62")+
        geom_text(data=data.df,aes(x=mese.asdate, y=temp_mini-3, label=temp_mini),size=4)+
        geom_text(data=data.df,aes(x=mese.asdate, y=temp_max+3, label=temp_max),size=4)+
        theme(axis.title.x = element_blank(),
              axis.title.y = element_blank(),
              axis.ticks = element_blank(),
              axis.text.x=element_blank(),
              axis.text.y=element_blank())+
        ggtheme
tempgg

multiplot(tempgg,pioggiagg,cols=1)

that give me

enter image description here

Community
  • 1
  • 1
delaye
  • 1,357
  • 27
  • 45
  • Yes but it doesn't seem to have find some solution ... :-S – delaye Dec 16 '14 at 13:06
  • It's simply not implemented in ggplot2. You'd have to hack something using grid functions. It's preferable to use base graphics for an example as simple as this. – Roland Dec 16 '14 at 13:11

1 Answers1

1

This is possible since ggplot 2.2.0 -- the key is that the second axis is a simple transformation of the other axis. In a climate graph a 10 C increase in temperature must be compensated by a 20 mm increase in precipitation, otherwise plant growth is limited by precipitation.

library("tidyverse")
data.df <- read_delim("mese Temperatura.media pioggia
Jan -0.3 46.6
Feb 2.6 40.9
Mar 7.1 68.5
Apr 11.5 72.5
May 15.6 92.0
Jun 19.1 79.6
Jul 21.4 79.2
Aug 20.6 81.2
Sep 17.2 85.4
Oct 11.3 98.9
Nov 4.9 93.7
Dec 0.8 52.1", delim = " ")

data.df <- mutate(data.df,
                  mese = factor(mese,
                                levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")))
ggplot(data = data.df,
       mapping = aes(x = mese, y = Temperatura.media, group = 1)) + 
  geom_bar(mapping = aes(y = pioggia/2), stat = "identity") + 
  geom_line() + 
  scale_y_continuous(
    "temperature, C", 
    sec.axis = sec_axis(~ . * 2, name = "precipitation, mm")
  ) 

So in this instance the plants are limited by temperature, not precipitation.

Created on 2018-08-22 by the reprex package (v0.2.0).

atiretoo
  • 1,812
  • 19
  • 33