0

I have a dataset named dd_season, I want to plot a figure with two Y axis.


library(sciplot )
library(lubridate)
require(ggplot2)

dd_season<- structure(list(season = c("winter", "spring", "summer", "autumn"
), SWC = c(0.215367272727273, 0.188693721438356, 0.165640213985765, 
0.197952897071685), SWC_se = c(0.00609369460525235, 0.00350287531034881, 
0.00459177231120467, 0.00250949141069349), RE = c(0.392283320197926, 
2.24605063260665, 4.03181267180274, 1.97919858486903), RE_se = c(0.0280110258468948, 
0.130007812249964, 0.144945068745065, 0.0730592546747523), GPP = c(-1.40787214533974, 
-3.7014004923165, -5.00924058460171, -4.36969118708317), GPP_se = c(0.0949534820731806, 
0.212371890215556, 1.19938762590705, 0.264536736436456), Tsoil = c(4.50054545454545, 
7.89332191780822, 16.3528113879004, 10.9254659498208), Tsoil_se = c(0.103643607843182, 
0.267353395939063, 0.149404523797333, 0.149633265630656), ratio = c(0.27863561438902, 
0.60681102660171, 0.804875031196632, 0.452937862226866), ratio_se = c(0.111968594007954, 
0.111968594007954, 0.111968594007954, 0.111968594007954)), row.names = c(NA, 
4L), class = "data.frame")


dd_season$ratio<- (-dd_season$RE/dd_season$GPP)
dd_season$ratio_se<- se(-dd_season$RE/dd_season$GPP)

Here is the code,

ggplot(data = dd_season, aes(x= as.factor(season), y= ratio))+
  geom_col( width = 0.5, color = "black")+
  geom_errorbar(aes(x=season, ymin=ratio-ratio_se, ymax=ratio+ratio_se), width= 0.1)+
  labs(x= 'seasons', y= 'RE/-GPP')+  
  geom_point(aes(x= season, y= SWC), color= 'red')+
  geom_errorbar(aes(x=season, ymin=SWC-SWC_se, ymax=SWC+SWC_se), width= 0.1, color= 'red')+
  scale_y_continuous(name= 'RE/-GPP',       #geom_col: the height of the bar represents the values in the data.
                     sec.axis = sec_axis(~.*0.3,  name="SWC"))+
  theme_bw()+
  theme(axis.text.y.right = element_text(color = "red"),
        axis.line.y.right = element_line(color = "red"), 
        axis.title.y.right = element_text(color = "red"), 
        axis.ticks.y.right = element_line(color = "red"), 
  )+
  theme(plot.title = element_text(hjust = 0.5),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.line = element_line(colour = "black"))

Here, I met several problems, the height of the SWC column doesn't match with the second Y axis, but still with the first Y axis. I have no idea why it doesn't work. The second problem is the color of the bar doesn't work when changed from black to green or other colors in the geom_col . And also the order of the bar doesn't match the dataset. I mean it's supposed to be winter, spring, summer, and autumn, right? I don't really understand why it started in autumn.

LEE
  • 316
  • 2
  • 8
  • The data is scaled according to first y axis, you need to scale the data with the same ratio you scaled the secondary axis (0.3). – Josep Pueyo Sep 15 '22 at 08:56
  • I just find the way to change the order of the x axis, ```ggplot(data = dd_season, aes(x= factor(season, level= c( 'spring', 'summer', 'autumn','winter')), y= ratio)) ``` – LEE Sep 15 '22 at 09:20

0 Answers0