我有一个数据框列表,我想为每个数据框制作一个条形图。详细信息sum
是条形的高度,而users
是每个条形的标签。
result12
[[1]]
users sum
1 00250902DC7D 34
2 00250902FA92 34
3 00250902FB05 34
4 002509030C41 34
5 002509030E53 34
[[2]]
users sum
1 00250902DC7D 24
2 00250902FA92 24
3 00250902FB05 24
4 002509030C41 24
5 002509030E53 24
[[3]]
[1] times users signal mode diff
<0 rows> (or 0-length row.names)
[[4]]
users sum
1 00250902DC7D 1
2 00250902FA92 1
3 00250902FB05 1
4 002509030C41 1
5 002509030E53 71
dput(result12)
列表(结构(列表(用户=结构(c(1L,3L,4L,10L,13L),. Label = c(“00250902DC7D”, “00250902FA91”,“00250902FA92”,“00250902FB05”,“00250902FB2E”, “00250902FE0A”,“00250902FE63”,“002509030AD2”,“002509030B9D”, “002509030C41”,“002509030C8D”,“002509030CE4”,“002509030E53”, “002509030E63”),class =“factor”),sum = c(34,34,34,34,34 )),. Name = c(“users”,“sum”),row.names = c(NA,-5L),class =“data.frame”), 结构(列表(用户=结构(c(1L,3L,4L,10L,13L),. Label = c(“00250902DC7D”, “00250902FA91”,“00250902FA92”,“00250902FB05”,“00250902FB2E”, “00250902FE0A”,“00250902FE63”,“002509030AD2”,“002509030B9D”, “002509030C41”,“002509030C8D”,“002509030CE4”,“002509030E53”, “002509030E63”),class =“factor”),sum = c(24,24,24,24, 24)),. Name = c(“users”,“sum”),row.names = c(NA,-5L),class =“data.frame”), 结构(列表(时间=结构(数字(0),类= c(“POSIXct”, “POSIXt”),tzone =“”),users =结构(整数(0),。Label = c(“00250902DC7D”, “00250902FA91”,“00250902FA92”,“00250902FB05”,“00250902FB2E”, “00250902FE0A”,“00250902FE63”,“002509030AD2”,“002509030B9D”, “002509030C41”,“002509030C8D”,“002509030CE4”,“002509030E53”, “002509030E63”),class =“factor”),signal = structure(integer(0),。Label = c(“false”, “true”),class =“factor”),mode = structure(integer(0),。Label = c(“OFF”, “ON”),class =“factor”),diff = numeric(0)),. Name = c(“times”, “users”,“signal”,“mode”,“diff”),row.names = integer(0),class =“data.frame”), 结构(列表(用户=结构(c(1L,3L,4L,10L,13L),. Label = c(“00250902DC7D”, “00250902FA91”,“00250902FA92”,“00250902FB05”,“00250902FB2E”, “00250902FE0A”,“00250902FE63”,“002509030AD2”,“002509030B9D”, “002509030C41”,“002509030C8D”,“002509030CE4”,“002509030E53”, “002509030E63”),class =“factor”),sum = c(1,1,1,1,71 )),。Name = c(“users”,“sum”),row.names = c(NA,-5L),class =“data.frame”))
如何自动生成我的条形图?
答案 0 :(得分:1)
您需要先从列表中删除空数据帧。
> res <- result12[sapply(result12, dim)[1,] != 0]
> res
[[1]]
users sum
1 00250902DC7D 34
2 00250902FA92 34
3 00250902FB05 34
4 002509030C41 34
5 002509030E53 34
[[2]]
users sum
1 00250902DC7D 24
2 00250902FA92 24
3 00250902FB05 24
4 002509030C41 24
5 002509030E53 24
[[3]]
users sum
1 00250902DC7D 1
2 00250902FA92 1
3 00250902FB05 1
4 002509030C41 1
5 002509030E53 71
然后使用
查看所有内容(在pdf文档中)> pdf("result12.pdf")
> lapply(1:length(res), function(i){
barplot(res[[i]]$sum, names.arg = res[[i]]$users)
})
> dev.off()
答案 1 :(得分:1)
您可以使用melt()
包中的reshape2
来重新整理数据。然后,您可以使用ggplot2
制作条形图。
require(reshape2)
require(ggplot2)
pino2=melt(pino,id.vars=c('users','sum'))
ggplot(pino2,aes(x=factor(users),y=sum))+geom_bar(stat='identity')+
facet_grid('L1~.')
L1
是输入列表的编号,因此分组的是(如果我理解正确的话)。如果您不想这样做,请不要在ggplot行的末尾插入+ facet_grid('L1~.')
。