0
votes

I am struggling to get a date field on the x-axis in ggplot. The data, I am using, is a time-series data of aerosol concentration from two different sensors. The date field in the time-series is not continuous. Rather, it contains daily observations of just three months of a year, i.e. March, April, and May, starting from 2007 to 2016. So while I am assigning the date column in the data frame as 'Date' field, it is showing this figure in the link which I don't like. ggplot output. However, without assigning the date column as 'Date' it shows me this figure. ggplot output2. The problem with this new figure is the unreadability of the x-axis text. I guess, the reason behind this is- the date field which has been read as a factor variable. With some modifications in the code, I am able to remove the x-axis ticks and axis text. Now I want the original dates in the data frame to be put in the x-axis with reasonable breaks. I am sharing the code and a subset of data I have used.

library(ggplot2)
library(reshape2)
df = read.csv('./mydata.csv')
names(df) = c('Date','MODIS','MISR')
df_melt = melt(df, id.vars = 'Date')
head(df_melt)
names(df_melt) = c('Date','grp','AOD')
ggplot(df_melt, aes(x = Date, y = AOD, group = 1, colour = grp))+
  theme(axis.text.x = element_blank()
        ,axis.ticks.x = element_blank())+
  geom_line()+
  facet_wrap(~ grp, ncol = 1)
1

1 Answers

0
votes

This is indeed a bit of a pain to calculate. My advice is to use run-length encoding to find the first occurrance of a year/month and use that as the axis breaks.

# Loading your data
df <- read.csv(file.choose())
df <- setNames(df, c("Date", "MODIS", "MISR"))

# Convert dates to ordered factors (totally assumes dates are in correct order)
df$Date <- ordered(as.character(df$Date), levels = as.character(df$Date))

# Find appropriate breaks
char_date <- as.character(df$Date)
nch <- nchar(char_date)

## Find which factor level is the first of a new year
years <- rle(substr(char_date, nch - 3, nch))
year_breaks <- cumsum(years$lengths) - years$lengths + 1

## Find which factor level is the first day of a new month
months <- rle(substr(char_date, 4, 5))
month_breaks <- cumsum(months$lengths) - months$lengths + 1

# Melt data
df_melt <- reshape2::melt(df, id.vars = 'Date')
names(df_melt) = c('Date','grp','AOD')

# Use date as numeric
ggplot(df_melt, aes(x = as.numeric(Date), y = AOD, group = 1, colour = grp))+
  geom_line()+
  scale_x_continuous(breaks = year_breaks, labels = years$values,
                     minor_breaks = month_breaks) +
  facet_wrap(~ grp, ncol = 1)

enter image description here