0

I have data like this:

Day Hour Minute   averagespeed   
1    0    14       55
1    0    29       65
1    0    44       70  
1    0    59       55 
1    1    14       32
1    1    29       50
1    1    44       96
1    1    59       54
1    2    14       64
1    2    29       70

I want to plot this data as bar chart or histogram but the x axis is in hours from 0 to 23 and divided into 4 intervals of 15 minute intervals like this :

0 14 29 44 59 1 14 29 44 59 2 14 29 44 59 ..............23

against averagespeed for the y axis.

this data is for 30 days and I do not know how to make the x axis like that based on my columns shown above.

gaut
  • 5,771
  • 1
  • 14
  • 45
SALEM
  • 19
  • 1
  • 2
    Please provide your data in an easy-to-paste form. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips on how to do that. What have you tried so far and where you fail? – Roman Luštrik Oct 20 '18 at 09:26
  • This axis choice seems like a bad choice. First, the intervals at not even. The gaps in minutes will repeat `14, 15 15, 15, 1, 14, 15, 15,15, 1,...`. Next, you are asking to put 120 labels on the time axis. That is going to be crowded and hard to read. Why not just put the hours? You can still plot the points in between; just don't label every point. – G5W Oct 20 '18 at 10:09
  • Are you asking to plot 30 * 24 * 4 = 2880 points on a single graph? – G. Grothendieck Oct 20 '18 at 13:39
  • No , 24*4 =96 points as a single graph for one day t .and then I plot 30 graphs for 30 days in one month. – SALEM Oct 20 '18 at 19:50

1 Answers1

0

You just need to define your own labels and custom x-axis. One additional difficulty is that by default the labels of such an axis do not align properly with barplot (but do with regular plot).

So you have to use the below workaround, saving the barplot as an object to then force the position of the x labels:

mylabels <- paste0(df$Hour, "h", df$Minute,"min")
# labels not aligned ------------------------------------------------------
#barplot(df$averagespeed, xaxt = "n", xlab="Time")
#axis(1, at=1:length(df$averagespeed), labels=mylabels)
# labels are aligned ------------------------------------------------------
m<-barplot(df$averagespeed, xaxt = "n", xlab="Time")
m
axis(1, at=m,labels=mylabels)

enter image description here The creation of an x-axis is explained here : https://stackoverflow.com/a/5182416/5224236

The workaround for barplots here: https://stackoverflow.com/a/15332146/5224236

gaut
  • 5,771
  • 1
  • 14
  • 45