2015-09-23 44 views
0

上barplot显示X轴值我有一个数据帧如下所示,的R - 如何从数据帧

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" ... 

我试图创建这个数据帧barplot。

如果我跑,

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

这是好的,给我的barPlot,但X轴是空白。我需要在X轴上显示Time列。

我试过,

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

但是这给了我,

错误-0.01 *高:非数值参数二元运算

什么是正确的方法去做这个?

回答

4

使用names.arg参数barplot

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

欢呼为 – IGGt

2

您还可以使用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