Hi,
Anyone can help me.
I have a problem to tidy up this graph.
I would like to reduce space in the datetime and remove the line from NA data. (graph 2)
Attached here the picture of the overall graph and below were the code.
p1 <- ggplot(data=df,aes(x=datetime, y=P_Rain_mm))+
geom_bar(stat="identity",color="black")+
labs(y= "Rainfall (mm) ", x="Datetime")
p1
p1<- p1+
#Top x axis with no labels and title
scale_x_datetime(sec.axis = dup_axis(labels = NULL, name =element_blank())) +
#Right y axis with no labels and title
scale_y_continuous(sec.axis = dup_axis(labels = NULL, name=element_blank()))+
theme(
axis.ticks.length.x.top = unit(-5, "pt"),
#Direction of ticks (negative value means it will be inward)
axis.ticks.length.y.right=unit(-5,"pt"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = "grey50")
)
p1
p1<- p1 +labs(x = NULL)+
theme(axis.text.x.bottom = element_blank())
p1<- p1 +
geom_hline(yintercept = 1000, linetype = "dashed")
p1
p2 <- ggplot(df1,aes(x=datetime)) +
geom_line(aes(y=Mg_CO2_ha_yr_exp_flux,color=micro.form))+
labs(y= "Mg_CO2_ha_yr ", x="Datetime")
p2
p2<- p2+#Top x axis with no labels and title
scale_x_datetime(sec.axis = dup_axis(labels = NULL, name =element_blank())) +
#Right y axis with no labels and title
scale_y_continuous(sec.axis = dup_axis(labels = NULL, name=element_blank()))+
theme(
axis.ticks.length.x.top = unit(-5, "pt"),
#Direction of ticks (negative value means it will be inward)
axis.ticks.length.y.right=unit(-5,"pt"),
#Legend position inside the plot
legend.position = c(.20, .99),
legend.justification = c("right", "top"),
#Line around legend plate
legend.background = element_rect(color = "grey50"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = "grey50")
)
p2
p2<- p2 +labs(x = NULL)+
theme(axis.text.x.bottom = element_blank())
p2
p3 <- ggplot() +
geom_line(data = df,aes(x=datetime,y=RH_1_1_1.x,group=month(datetime)))+
labs(y= "Rel_Hum ", x="Datetime")+
theme(legend.position= "none")
p3
p3<- p3 +
scale_x_datetime(sec.axis = dup_axis(labels = NULL, name =element_blank())) +
scale_y_continuous(sec.axis = dup_axis(labels = NULL, name=element_blank()))+
theme(
axis.ticks.length.x.top = unit(-5, "pt"),
axis.ticks.length.y.right=unit(-5,"pt"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = "grey50")
)
p3
p3<- p3 +labs(x = NULL)+
theme(axis.text.x.bottom = element_blank())
df$minSWC_1_1_1.x <- df$SWC_1_1_1.x - sd(df$SWC_1_1_1.x, na.rm = TRUE)
df$maxSWC_1_1_1.x <- df$SWC_1_1_1.x + sd(df$SWC_1_1_1.x, na.rm = TRUE)
p4 <- ggplot(df,aes(x=datetime,y=SWC_1_1_1.x,group=month(datetime)))+
geom_ribbon(aes(ymin = minSWC_1_1_1.x, ymax = maxSWC_1_1_1.x ), alpha = .5,
fill = "darkseagreen3", color = "transparent")+
geom_line(color = "aquamarine4", size = 1) +
labs(y= "SWC ", x="Datetime")
p4
p4 <- p4 +
scale_x_datetime(sec.axis = dup_axis(labels = NULL, name =element_blank())) +
scale_y_continuous(sec.axis = dup_axis(labels = NULL, name=element_blank()))+
theme(
axis.ticks.length.x.top = unit(-5, "pt"),
axis.ticks.length.y.right=unit(-5,"pt"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = "grey50")
)
p4
p4<- p4 +labs(x = NULL)+
theme(axis.text.x.bottom = element_blank())
p4
df$minWTD_1_1_1.x <- df$WTD_1_1_1.x - sd(df$WTD_1_1_1.x, na.rm = TRUE)
df$maxWTD_1_1_1.x <- df$WTD_1_1_1.x + sd(df$WTD_1_1_1.x, na.rm = TRUE)
p5 <- ggplot(df,aes(x=datetime,y=WTD_1_1_1.x,group=month(datetime)))+
geom_ribbon(aes(ymin = 0, ymax = WTD_1_1_1.x), alpha = .5,
fill = "darkseagreen3", color = "transparent")+
geom_line(color = "aquamarine4", size=1) +
labs(y= "WTD ", x="Datetime")
p5
p5<- p5 +
scale_x_datetime(sec.axis = dup_axis(labels = NULL, name =element_blank())) +
scale_y_continuous(sec.axis = dup_axis(labels = NULL, name=element_blank()))+
theme(
axis.ticks.length.x.top = unit(-5, "pt"),
axis.ticks.length.y.right=unit(-5,"pt"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = "grey50")
)
p5
library(gridExtra)
grid.arrange(p1, p2,p3, p4,p5,
ncol = 1, nrow= 5)