1
votes

I am trying to graph two different datasets, reconstructed temperatures (10-16) and charcoal data (0-140), with two different time series values, using ggplot. Is this possible?

I used this code (see below) but unfortunately it produced a plot (see below) that limits the variability of the temperature reconstruction. Is there a way to adjust the y axis so we can see more variability in the temperature record?

Thank you very much for your support.

R code

df <- data.frame(Charfiretempdata$AGETEMPS, Charfiretempdata$FIREAGE, Charfiretempdata$Comp2TEMPS,Charfiretempdata$Char.Acc.Rate..Char...cm.2.yr.1.)

ggplot(df)  + 
  
geom_col(mapping = aes(x = Charfiretempdata$FIREAGE, 
y = Charfiretempdata$Char.Acc.Rate..Char...cm.2.yr.1. * 16/150), size = 2, color = "darkblue", 
fill = "white") +
  
geom_line(mapping = aes(x = Charfiretempdata$AGETEMPS, y = Charfiretempdata$Comp2TEMPS)) + 
  
geom_point(mapping = aes(x = Charfiretempdata$AGETEMPS, y = Charfiretempdata$Comp2TEMPS), size 
= 3, shape = 21, fill = "white")+
  scale_y_continuous(
    name = expression("Temperature ("~degree~"C)"),
    sec.axis = sec_axis(~ . * 150/16 , name = "Charcoal (mm)"))

R plot

enter image description here

1
It would be easier to help if you create a small reproducible example. Read about how to give a reproducible example.Ronak Shah

1 Answers

1
votes

I create a random sample data that would share similar characteristics to your data.

library(dplyr)
library(ggplot2)

set.seed(282930)
df <- tibble(x_axis = c(1400, 1500, 1600, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
                   2007, 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016, 2017),
             y_axis_1 = runif(20, min = 10, max = 16),
             y_axis_2 = runif(20, min = 0, max = 150))

Here is the df

> df
# A tibble: 20 x 3
   x_axis y_axis_1 y_axis_2
    <dbl>    <dbl>    <dbl>
 1   1400     15.7     5.28
 2   1500     11.8   141.  
 3   1600     14.5   149.  
 4   2000     11.6   121.  
 5   2001     15.6    37.3 
 6   2002     15.0    72.5 
 7   2003     10.7   130.  
 8   2004     15.4    84.7 
 9   2005     11.5   118.  
10   2006     10.4    17.4 
11   2007     11.3   124.  
12   2008     13.6    22.6 
13   2009     13.0    14.5 
14   2010     15.9   142.  
15   2011     12.3   103.  
16   2012     10.3   131.  
17   2013     12.6    93.6 
18   2015     14.6    12.4 
19   2016     11.4    27.9 
20   2017     15.3   116. 

Here is the ggplot similar to your but with the different Axis adjustment

ggplot(df, 
       # as they sharing same X-axis you can define share variable aes in the 
       # main call of ggplot
       aes(x = x_axis))  + 
  geom_col(mapping = 
             # added 10 to 2nd axis value as will scale from 10 instead of 0
             aes(y = (y_axis_2 * 10 / 150) + 10), 
           # the size here is size of the border - and due to the nature of
           # your data, the col suppose to be very thin to match with that one
           # tick on x-axis - so the inner fill is covered by dark blue border
           size = 2, color = "darkblue", 
           # The fill is not really useful as you cannot see it.
           fill = "white") +
  geom_line(mapping = aes(y = y_axis_1)) + 
  geom_point(mapping = aes(y = y_axis_1), size 
             = 3, shape = 21, fill = "white") +
  # Set the main Axis start at 10 instead of 0 so it would allow more zoom into it
  coord_cartesian(ylim = c(10, 20), expand = c(0, 0)) +
  scale_y_continuous(
    name = expression("Temperature ("~degree~"C)"),
    # The calculation of second axis lable is calculate base on 1st axis.
    # and as the 1st axis start at 10, there fore the fomular need to minus 10
    # before multiply back 15 - I keep 150 / 10 so it clear reverse of original 
    # transform of the 2nd axis value above.
    sec.axis = sec_axis(~ (. - 10) * 150 / 10 , name = "Charcoal (mm)"))

Here is the sample output plot First plot

And even with the adjsut y-axis we can hardly see the temperature at the end of the data because there are a lot more data points at the end. I think if you don't need all of data point at the end you may just take every 10 x as the data was on the range of 600 years so you don't need to graph so much details at the end. And if you need details just graph that time frame separately

Filter data at the end to only take every 10 year instead

ggplot(df %>% filter(x_axis <= 2000 | x_axis %% 10 == 0),
       aes(x = x_axis)) + 
  # similar code to above but I use geom_bar instead
  geom_bar(mapping = 
             aes(y = (y_axis_2 * 10 / 150) + 10),
           stat = "identity", size = 2, color = "darkblue",
           fill = "white") +
  geom_line(mapping = aes(y = y_axis_1)) + 
  geom_point(mapping = aes(y = y_axis_1), size 
             = 3, shape = 21, fill = "white")+
  scale_y_continuous(
    name = expression("Temperature ("~degree~"C)"),
    sec.axis = sec_axis(~ (. - 10) * 150/10 , name = "Charcoal (mm)")) +
  coord_cartesian(ylim = c(10, 20), expand = c(0, 0))

filter every 10 year

(As you can see that with less data point, we started to see the fill as plot have more space)

Zoom in at the end of the data

ggplot(df %>% filter(x_axis >= 2000),
       aes(x = x_axis)) + 
  # similar code to above but I use geom_bar instead
  geom_bar(mapping = 
             aes(y = (y_axis_2 * 10 / 150) + 10),
           stat = "identity", size = 2, color = "darkblue",
           fill = "white") +
  geom_line(mapping = aes(y = y_axis_1)) + 
  geom_point(mapping = aes(y = y_axis_1), size 
             = 3, shape = 21, fill = "white")+
  scale_y_continuous(
    name = expression("Temperature ("~degree~"C)"),
    sec.axis = sec_axis(~ (. - 10) * 150/10 , name = "Charcoal (mm)")) +
  coord_cartesian(ylim = c(10, 20), expand = c(0, 0))

Graph zoom in at the end

(Now we can see both the darkblue border and the white fill inside)