从CSV分组数据并使用R创建图表

时间:2013-11-20 22:42:02

标签: r csv graph charts bar-chart

好吧,用R. 我有这个CSV:

"ID","Kind","Variety","Category","Kgrs","Kgrs_m2"
"120-1","HOLLAND","MULHACEN","12G",1206,0.25
"120-1","HOLLAND","MULHACEN","12M",4708,0.99
"120-2","HOLLAND","MULHACEN","12G",2190,0.46
"120-2","HOLLAND","MULHACEN","12P",5746,1.20
"120-3","CHERRY","MINISTAR","A",9165,3.05
"120-3","CHERRY","MINISTAR","B",50,0.017
"120-3","CHERRY","MINISTAR","DES",1182,0.39
"120-3","HOLLAND","MULHACEN","12G",372,0.12
"120-7","CHERRY","MINISTAR","A",6584,3.05
"120-7","CHERRY","MINISTAR","B",321,1.05

我想做这样的事情,用R:

enter image description here

我读了CSV,但我不知道如何从CSV分组数据并将这些数据放入条形图。

另外,我需要为每组ID添加Variety。

这是我的代码:

path <- "./"

cosechas <- read.csv(paste(path, "test.csv",  sep="/"),header=T)

cosechas$Kgms <-  as.numeric(cosechas$Kgms)



fi <- c(cosechas$Kgms)
names(fi) <- levels(cosechas$Category)


png(paste(path, "cosechas.png", sep="/"))

barplot(fi, ylab="Kg", xlab="Categoria")
dev.off()

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果您想要全部单色,可以将“填充”更改为“分组”。

df <- read.csv('your/file.csv')
library(ggplot2)
ggplot(df, aes(x = ID, y = Kgrs, fill = Category)) + 
geom_bar(stat = 'identity', position='dodge')