tashu
tashu

Reputation: 11

How to create a plot using the hourly data for each day

I am a beginner in R, and I have a CSV file with 2 variables. The column name <> has date and time value combine for each day for 12 months and the variable has <> column respective to the date. This is the example of the data: enter image description here

I want to make a simple plot to see the air quality of each month's respective day and time. Something like this Expectedpicture

I do not need to make any calculation as the data is hourly mean already. But I am quite struggling to make the plot.

This is how I load the data.

Data= read.csv("EarthSense.csv") ** Then I tried to separate the time and hours but this is not working as it converts everything NA.

**Hours <- format(as.POSIXct(strptime(Data$date,"%d/%m/%Y %H:%M",tz="")) 
,format = "%H:%M")
 glimpse(Hours)
 Dates <- format(as.POSIXct(strptime(Data$date,"%d/%m/%Y %H:%M",tz="")) 
 ,format = "%d/%d/%Y")
 glimpse(Dates)
 **

chr [1:8750] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA ...

**ggplot(data=Data,aes(x=date, y=value)) + 
  geom_path(colour="red") + 
  ylab("Temperatura (ºC)") + 
  xlab("Fecha")

**

Upvotes: 0

Views: 1005

Answers (1)

Ben
Ben

Reputation: 30474

Edit: This uses your example data. Format of date appears to be %Y-%m-%d %H:%M:%S.

df$fdate <- as.POSIXct(df$date, format = "%Y-%m-%d %H:%M:%S")
df$hour <- as.numeric(format(df$fdate, "%H"))
df$mo_yr <- as.factor(format(df$fdate, "%b-%Y"))

ggplot(data=df, aes(x=hour, y=PM2P5, col=mo_yr)) + 
  geom_line() + 
  ylab("Temperatura (ºC)") + 
  xlab("Hora")

edited plot of temp vs hour

Note that this creates a Month-Year factor. Other ways to deal with month-year objects include yearmonth in tsibble package and yearmon in zoo package.

Data

df <- structure(list(date = c("2019-01-01 02:00:00", "2019-01-01 03:00:00", "2019-01-01 04:00:00", "2019-01-01 05:00:00", "2019-01-01 06:00:00", "2019-01-01 07:00:00"), 
               PM2P5 = c(20.8, 7.1, 6.1, 9.7, 6.8, 12.2 )), row.names = c(NA, 6L), class = "data.frame")

Upvotes: 1

Related Questions