How to reduce space between datetime and remove straight line from NA data?

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)

Hi @mhab,
Putting broken axes in ggplots is not easily achieved. However, using ideas from these two posts it is possible to get your desired result by creating facets and then only plotting the selected ones:
https://stackoverflow.com/questions/7194688/using-ggplot2-can-i-insert-a-break-in-the-axis
https://stackoverflow.com/questions/49110877/how-to-adjust-facet-size-manually/49225527#49225527

library(tidyverse)
# Create some dummy data
set.seed(42)
df <- data.frame(fulldate = seq(as.Date("2019-01-01"), as.Date("2019-12-31"), by=1),
                 tmp = rnorm(365, 20, 2))

# Create some NA gaps in the data
df$tmp[41:70] <- NA
df$tmp[201:250] <- NA

plot(tmp ~ fulldate, data=df, typ="l")

# Create a factor to describe facets
df$myfacets <- c(rep(1, 40), rep(2, 30), rep(3, 130), rep(4, 50), rep(5,115))

head(df)
#>     fulldate      tmp myfacets
#> 1 2019-01-01 22.74192        1
#> 2 2019-01-02 18.87060        1
#> 3 2019-01-03 20.72626        1
#> 4 2019-01-04 21.26573        1
#> 5 2019-01-05 20.80854        1
#> 6 2019-01-06 19.78775        1

# Plot all facets
ggplot(df, aes(x=fulldate, y=tmp)) +
  geom_line() +
  facet_grid(. ~ myfacets, scales="free_x", space="free_x")

# Only plot facets we are interested in
ggplot(df[df$myfacets %in% c(1,3,5),], aes(x=fulldate, y=tmp)) +
  geom_line() +
  facet_grid(. ~ myfacets, scales="free_x", space="free_x") +
  theme(axis.text.x = element_text(angle=90),
        strip.text.x = element_blank())

Created on 2021-04-08 by the reprex package (v2.0.0)

1 Like

Hi there,

Thank you very much.

That really help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.