I am having a problem aligning my graphics using facet_wrap(). I have multiple years of data but I am subsetting to display only two years. For some unknown reason the upper plot is shifted to the left and the lower plot is shifted to the right (See attached snapshot). My dataset is very large to post here so it can be downloaded from the link below if someone is willing to help me to align the plots. https://login.filesanywhere.com/fs/v.aspx?v=8c6b678a5c61707ab0ae
Here is the snapshot:
This is what I have tried:
library(writexl)
library(readxl)
library(tidyverse)
library(lubridate)
mydat <- read_excel("all.xlsx", sheet="Sheet1")
#subset 2 years
start <- 1998
end <- 1999
a <- dplyr::filter(mydat, year %in% start:end)
ggplot(a,aes(date,Salinity,color=Box)) +
geom_line(size=.8) +
theme(legend.position = 'none') +
scale_x_date(date_breaks = "1 month",date_labels = "%b",expand=c(0,0.5)) +
labs(x="",y= "test") +
facet_wrap(~year,ncol=1)
I will be subsetting up to 10 years in the future. I am also wondering what's the best to subset multiple years using dplyr or base. Thanks beforehand.
I get the following error after trying your suggestion:
subset 2 years
start <- 1998
end <- 2000
a <- mydat %>%
dplyr::filter(year %in% start:end) %>% # if you need to subset the years
mutate(date = as.Date(gsub("\\d{4}", "0000", date)))
ggplot(a,aes(date, Salinity,color=Box)) +
geom_line(size=.8) +
scale_x_date(date_breaks = "1 month",date_labels = "%b",expand=c(0,0.5)) +
labs(x="",y= "test") +
facet_wrap(~year,ncol=1,scales="free_x")
Error in charToDate(x) :
character string is not in a standard unambiguous format
Maybe 'date' doesn't like the zeros? Question: Does it work for you using the dataset from the link?