1

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 code
    – antonio
    Feb 27, 2014 at 16:10
  • My fault. Please see my answer. Feb 27, 2014 at 16:11

2 Answers 2

1

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

7
  • I was try to mel but I failed, error: Error: id variables not found in data: sum
    – antonio
    Feb 27, 2014 at 16:26
  • It's possible that the seventh element is causing trouble, since it has no dimensions. Try setting pino[[7]]=NULL Feb 27, 2014 at 16:33
  • You'd also need to get rid of pino[[3]], since it's empty, too. that doesn't work, I'm not sure what you shoul do, since there are always columns named "sum" in those datasets. Feb 27, 2014 at 16:45
  • Please check my revised answer. It works on my end. Feb 27, 2014 at 16:47
  • @user1362215 Thank u, itis perfect. Basically i need both :) Now I have to improve stuff such as the graphical effect etc..try to understand your coding...it's a miracle :)
    – antonio
    Feb 27, 2014 at 17:21
1

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()
6
  • i have tried but it doesnt work...: > lapply(1:length(result12), function(i) { barplot(result12[[i]]$sum, names.arg = result12[[i]]$users)}) Error in result12[[i]]$sum : $ operator is invalid for atomic vectors
    – antonio
    Feb 27, 2014 at 17:14
  • Result12 is a shorter version of Pino. i have modified my inital question to make it more convenient
    – antonio
    Feb 27, 2014 at 17:17
  • I have revised it. You need to remove the empty data frames from the list first. Feb 27, 2014 at 17:26
  • yes but it create only one plot regarding the last dataframe result12[[4]].
    – antonio
    Feb 27, 2014 at 17:37
  • Ok but It shows very fast all the plots...but it's too fast...I would need to see all of them distinctly
    – antonio
    Feb 27, 2014 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.