Siewmei Loh
Siewmei Loh

Reputation: 45

How to plot histogram in R from data?

Interval    Occupancy Rate
18:35:00    100%
18:40:00    100%
18:45:00    100%
18:50:00    100%
18:55:00    99.78%
19:00:00    100%

Hi all, I would like to plot a histogram where x-axis is the time interval and y-axis is the occupancy. How am I suppose to do with the hist() code as I've tried basic hist and ggplot2, but it seems like histogram gives y-axis as freq often. Is there any way to do this or I should use another stats graphic?

Thanks in advance!

Upvotes: 0

Views: 575

Answers (1)

S. Elzwawi
S. Elzwawi

Reputation: 551

Given that D is the name of the data frame, and Interval and Occupancy are the names of the columns, as Pascal suggested above, you need a bar plot to visualize the data the way you indicated. It depends whether your Interval variable is already in a date format, in such a case you won't need a conversion of the variable. Otherwise, you need to use as.Date() to convert the data as shown below. You also need to convert the percentage occupancy values to numeric as shown below in the code I used to produce the plot:

library(ggplot2)
Interval <- as.Date(c(18:35:00, 18:40:00, 18:45:00, 18:50:00, 18:55:00, 19:00:00), format = "%H:%M:%S") 
Occupancy <- c("100%", "100%", "100%", "100%", "98.78%" ,"100%") 
D <- data.frame(Interval, Occupancy)
D$Occupancy <- as.numeric(sub("%", "", D$Occupancy))
qplot(Interval, Occupancy, data=D, geom="bar", stat="identity") 

Hope this was helpfull

Upvotes: 1

Related Questions