简体   繁体   中英

Best way to plot by hour by day by month in r

currently I have created the following dataframe in R but I am having trouble with my visualisation.

The Dataframe looks as follows:

date   weekday   dayhour    amount
2017-06    0         1         100
2017-06    0         2         200
2017-06    0         3         150
2017-06    0         4         600
2017-06    0         5         75
....
2018-06    6         21        60
2018-06    6         22        90
2018-06    6         23        150
2018-06    6         24        110

The amount is the average of that weekday by hour for that month. So for example the month june in 2017 on the first hour of each monday in june has an average amount of 100.

Now the idea is to plot my data in R in several graphs which will show me the data by hour by weekday for that given month. So 12 plots with each the amount on the y axis and the hour+weekday on the x axis.

I have tried several approaches such as looping through the months and plotting them with par(mfrow = c(2,6)). Also I tried plotting them one by one. However I am still a rookie with R and I can't find any good documentation or tutorial on how to do this. For now I have only been able to stack the datapoints in one loop by weekday and not by hour by doing the following on the dataset without hours included yet:

increase = 7
for (i in (length(occupancy_by_day)/7)) {
  data = head(occupancy_by_day,increase:increase+increase)
  plot(average_occupancy ~ Weekday, data=data)
  increase = increase + 7
}

My closest guess to the correct answer at this moment is something like this:

par(mfrow = c(2,6))

increase = 06
for (i in (length(occupancy_by_day)/30,5)) {
  data = occupancy_by_day[occupancy_by_day$date == paste(c('2017-',increase)), ]
  plot(amount ~ weekday, data=data)
  increase = increase + 1
}

This gives me the error:

Error in plot.window(...) : need finite 'xlim' values

Does anyone know a good solution to plotting the data in R?

Thanks in advance for any help/comments!

EDIT: priority on this post would be how to plot data by hour by weekday. I could iterate through the months manually however I would still need to plot them. A loop for each month would be added bonus. Right now I have the following:

data =occupancy_by_day[occupancy_by_day$date == '2017-06', ]
plot(Amount ~ weekday+dayhour, data=data)

This sadly only plots the data by dayhour.

ADDED DRAWING OF CONCEPT: https://imgur.com/qKFbbmJ

ANSWER:

Eventually I did a litle workaround to plot them with:

ggplot(data = data[data$date == '2017-12', ], aes(plotstamp, Amount, group=Weekday, col=Weekday)) + 
   geom_line() +
   geom_point() + 
   ggtitle("December 2017")

the plotstamp is an extra column/index I added to my DF which allowed my to plot the values continously. Then I just plotted them seperately per month.

Make similar data

I think this is the partial solution you ask for in your edit (if I understand your task correctly), but I believe you can loop through months in the same way. The only way I could think of was to transform the dates you have to date class. I used some prepared date data but you can fix yours using the strptime() and paste() commands to match mine. Also, the data I made is only for two days.

date1 <- c(rep("2017-06-1",24),rep("2017-06-2",24))
weekday <- c(rep(0,24),rep(1,24))
dayhour <- c(1:24,1:24)
# Add dayhour to date
date <- paste(date1, dayhour, sep = " ")
date <- strptime(date, "%Y-%m-%d %H")
amount <- c(1:24,(48:25)*2)

dat <- data.frame(date,weekday,dayhour,amount)
View(dat)

plot(x=dat$date, y=dat$amount)

This is how my created data looks like.

                 date weekday dayhour amount
1 2017-06-01 01:00:00       0       1      1
2 2017-06-01 02:00:00       0       2      2
3 2017-06-01 03:00:00       0       3      3
4 2017-06-01 04:00:00       0       4      4
....
46 2017-06-02 22:00:00       1      22     54
47 2017-06-02 23:00:00       1      23     52
48 2017-06-03 00:00:00       1      24     50

Loop for the plot.

If you write this in an R markdown document you will get nice pages for each plot so you don't have to use par(mfrow = c(1,2)) . You probably also need to fix the loop arguments to fit your data.

par(mfrow = c(1,2))
start <- 0
end <- 23
step = 1
for (i in 1:(length(dat$date)/24)) {
  data <- dat[(start+step) : (end+step), ]  # The parenteses at (start+step) and (end+step) are important!
  plot(x = data$date, y = data$amount)
  step = step + 23
}

I hope this help you.

PS This is the first answer I write, so feel free to edit or improve my answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM