1
votes

I have a list of dataframes and i would like to make a barplot for each of them. In detail sum is the height of the bars, while users are the labels of each bar.

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)
list(structure(list(users = structure(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 )), .Names = c("users", "sum"), row.names = c(NA, -5L), class = "data.frame"), structure(list(users = structure(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)), .Names = c("users", "sum"), row.names = c(NA, -5L), class = "data.frame"), structure(list(times = structure(numeric(0), class = c("POSIXct", "POSIXt"), tzone = ""), users = structure(integer(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)), .Names = c("times", "users", "signal", "mode", "diff"), row.names = integer(0), class = "data.frame"), structure(list(users = structure(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 )), .Names = c("users", "sum"), row.names = c(NA, -5L), class = "data.frame"))

How can I generate automatically my barplots?

2
yes it doesn't work. Probabily i write wrong the codeantonio
My fault. Please see my answer.Rich Scriven

2 Answers

1
votes

You can use melt() from the reshape2 package to reshape the data this. Then you can use the ggplot2 to make the barplots.

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 is the number of the list entered, so that's what's being grouped by (if I understand you correctly). If you don't want to do that, don't insert the + facet_grid('L1~.') at the end of the ggplot line(s).

1
votes

You will need to remove the empty data frames from the list first.

> 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

And then view them all (in a pdf document) by using

> pdf("result12.pdf")
> lapply(1:length(res), function(i){
    barplot(res[[i]]$sum, names.arg = res[[i]]$users)
  })
> dev.off()