IGGt
IGGt

Reputation: 2759

R - How to display X axis values on barplot from data frame

I have a data Frame that looks like the following,

str(data2)
'data.frame':   516 obs. of  2 variables:
 $ Jobs      : num  2 1 5 0 0 0 0 0 0 0 ...
 $ Time         : chr  "06:00" "06:01" "06:02" "06:04" ...

I am trying to create a barplot from this data Frame.

If I run,

barplot(data2$Jobs,
    col="orange", 
    xlab="Time of Day", 
    ylab="Files With Jobs", 
    main="Jobs by Time of Day Received")

This is fine, and gives me the barPlot, but the X axis is blank. I need to have the Time column showing on the X Axis.

I tried,

barplot(data2$Time,data2$Jobs,
    col="orange", 
    xlab="Time of Day", 
    ylab="Files With Jobs", 
    main="Jobs by Time of Day Received")

But this gives me,

Error in -0.01 * height : non-numeric argument to binary operator

What is the correct way to do this?

Upvotes: 0

Views: 3471

Answers (2)

Soheil
Soheil

Reputation: 974

You can also use ggplot:

data<- data.frame(jobs=c(2, 1, 5, 0), Time=c("06:00", "06:01", "06:02", "06:04"))
data$Time <-  strptime(data$Time, format="%H:%M")

ggplot(data=data,aes(x=Time,y=jobs)) + 
  geom_bar( stat="identity", colour = "brown1", size = 1.5)+
  theme(axis.text.x = element_text(angle = 90,hjust=1,vjust=0.3))+
  xlab("Time of Day") +
  ylab("Files With Jobs")

enter image description here

Upvotes: 2

DGKarlsson
DGKarlsson

Reputation: 1101

Use the names.arg argument to barplot:

barplot(data2$Jobs, 
        names.arg=data2$Time,
        col="orange", 
        xlab="Time of Day", 
        ylab="Files With Jobs", 
        main="Jobs by Time of Day")

Upvotes: 4

Related Questions