2016-07-26 97 views
1

barplot我無法創建barplot與下面的數據幀如何創建從數據幀

    Age  Male.Average Female.Average 
    ---------------------------------------------------------  
1:  0-19 Childhood  20.29543  18.14773 
2: 20-39 Young Adult  22.35352  22.41548 
3: 40-64 Mid Aged Adult  23.75663  23.64908 
4:  65-84 Senior Age  21.45002  21.71446 
5: 85+ Old Senior Age  21.72200  21.31565 

我想創建分組barplot如x =年齡,Y = male.average,female.average在旁邊。它提供了錯誤:

Error in barplot.default(d) : 'height' must be a vector or a matrix

回答

1

假設我們有一個data.frame

barplot(`colnames<-`(t(df1[-1]), df1[,1]), beside=TRUE, 
    legend.text = TRUE, col = c("red", "green"), 
    args.legend = list(x = "topleft", bty = "n", inset=c(-0.05, 0))) 

enter image description here