0

I am trying to plot (Time VS Speed and Precipitation) using ggplot but I am getting a messy plot. the data is below (df) and then the code that I tried.

    df <- structure(list(Agency.Station.ID = c("MI270N003.6D", "MI270N004.7D", 
"MI270N005.7D", "MI270N007.3D", "MI270N008.5D", "MI270N003.6D", 
"MI270N004.7D", "MI270N005.7D", "MI270N007.3D", "MI270N008.5D", 
"MI270N003.6D", "MI270N004.7D", "MI270N005.7D", "MI270N007.3D", 
"MI270N008.5D", "MI270N003.6D", "MI270N004.7D", "MI270N005.7D", 
"MI270N007.3D", "MI270N008.5D"), date_time = structure(c(1427846400, 
1427846400, 1427846400, 1427846400, 1427846400, 1427846700, 1427846700, 
1427846700, 1427846700, 1427846700, 1427847000, 1427847000, 1427847000, 
1427847000, 1427847000, 1427847300, 1427847300, 1427847300, 1427847300, 
1427847300), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Date = structure(c(16526, 16526, 16526, 16526, 16526, 16526, 
    16526, 16526, 16526, 16526, 16526, 16526, 16526, 16526, 16526, 
    16526, 16526, 16526, 16526, 16526), class = "Date"), Time = c("00:00:00", 
    "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:05:00", 
    "00:05:00", "00:05:00", "00:05:00", "00:05:00", "00:10:00", 
    "00:10:00", "00:10:00", "00:10:00", "00:10:00", "00:15:00", 
    "00:15:00", "00:15:00", "00:15:00", "00:15:00"), Speed = c(59, 
    34, 46, 61, 46, 58, 39, 51, 36, 52, 62, 47, 49, 57, 50, 59, 
    50, 35, 54, 44), Precipitation = c(0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 20L
), class = "data.frame")

and here is the code that I tried:

df <- df %>% gather(Aspect, Value, -Agency.Station.ID, -Date, -Time)
df$Agency.Station.ID <- factor(df$Agency.Station.ID)
df$Date <- factor(df$Date)
for(i in levels(df$Agency.Station.ID)) {
  pdf(paste0(i,'.pdf'))

  for(j in levels(df$Date)) {
    subset.data <- df[which(df$Agency.Station.ID==i & df$Date==j),]
    if (nrow(subset.data)!=0) {
      p <- ggplot(data=subset.data, aes(Time, Value, group = 1)) +
        geom_line(aes(color=Aspect)) +
        labs(title=paste('Station:',i, " Date:",j)) +
        theme_bw()
      invisible(print(p))
    }
  }
  dev.off()
}

My concern is about the time(I have 288 observations/day) since it will be compacted on x-axis. the how my output looks like

mustafa
  • 203
  • 1
  • 8
  • can we change the labels on x-axis to be specific numbers between (1:24) which are the number of the day hours? – mustafa May 14 '20 at 16:30
  • You haven't provided a fully reproducible example here, so I can't test an answer, but I would look into using `scale_x_date` or `scale_x_datetime` to customize the tick locations and labels in your plots. – ulfelder May 14 '20 at 16:43

1 Answers1

1

Instead of storing Date Time in separate columns, you can combine the:

df$Datetime <- as.POSIXct(paste0(df$Date, df$Time))

Right now ggplot prints all labels on the x-axis, as Time is given as character. ggplot calls functions to prettify the axis labels, i.e. print informative, non-overlapping labels. This requires variations of numeric variables though, for example R's date-time representation POSIXct.

PS: Your example is not working for me, Aspect is missing.

thorepet
  • 421
  • 2
  • 9
  • I need to have 43 plots for each Agency.Station.ID. then each plot has the 24hours divided into 5 minutes. In this case, the Date.Time column might not work to me. – mustafa May 14 '20 at 16:50
  • In this case the following might be a work-around [click](https://stackoverflow.com/questions/30607514/setting-limits-with-scale-x-datetime-and-time-data). The times of day are represented `POSIXct`, all for a single day. This way you benefit from the automated date handling within `ggplot2`, and keep the columns separated. – thorepet May 14 '20 at 16:58
  • I have passed the Time labels issues. However, the plots still messy as shown above. I think the plot is taking values from more than one day. – mustafa May 14 '20 at 17:45
  • I am expecting the two plots to be two smooth lines and so clear showing the ups and downs of both variables. – mustafa May 14 '20 at 17:46
  • Please edit your original question, and provide a working example. Then this can be addressed. – thorepet May 14 '20 at 17:55
  • please see the new edited question and notice that Precipitation has only 0 values here but in my original data it has more values. – mustafa May 15 '20 at 06:30
  • You now have 3 columns with date and time information, you need only two in this case. The `date_time` column now gets transformed into an `Aspect` when you `gather` the data. This messes up the data for plotting. You can remove the `Time` column, and use `df <- df %>% gather(Aspect, Value, -Agency.Station.ID, -Date, -date_time)`. This should fix the issue. Also in the `ggplot` call change the `mapping` to `aes(date_time, Value, group = Aspect)`. – thorepet May 15 '20 at 07:30
  • Thank you so much! That works. I was missing to group by Aspect. – mustafa May 15 '20 at 07:36
  • Should you now add that to your answer? I have voted to you and accepted it. – mustafa May 15 '20 at 07:37