1

I got data.frame in the nested list after I split them by given threshold. However, I am going to generate stack bar plot to make data more informative and easy to understand. I think using ggplot2 packages could be good choice, but I am quite new for using this packages, doing this is not intuitive.How can I get stack bar plot for data.frame in the nested list ? Any way getting bar plot, or pie graph for data.frame object easily ? Any idea ?

mini data :

myList <- list(
  hola= data.frame( from=seq(1, by=4, len=15), to=seq(3, by=4, len=15), value=sample(30, 15)),
  boo = data.frame( from=seq(3, by=7, len=20), to=seq(6, by=7, len=20), value=sample(45, 20)),
  meh = data.frame( from=seq(4, by=8, len=25), to=seq(7, by=8, len=25), value=sample(36, 25))
)

helper function :

splitter <- function(mlist, threshold) {
  res <- lapply(mlist, function(x) {
    splt <- split(x, ifelse(x$value >= threshold, "pass", "fail"))
  })
  return(res)
}

#' @example 
splitMe <- splitter(myList, threshold = 10)

I want to generate stack bar plot, pie graph by using ggplot2 packages. How can I make this happen easily ? Can any one point me how to do this task ?

How can I get stack bar plot for data.frame in the nested list ? How can I achieve my desired output plot ? Thanks a lot

Andy.Jian
  • 417
  • 3
  • 15

2 Answers2

2

You may not get these chart directly with this. But i think you will get the idea. Key is you need to get your data in proper format in order to plot.

Here i do some data manipulation for getting the data into data frame.

df=as.data.frame(unlist(lapply(splitMe,function(x) unlist(x))))
df$col=row.names(df)
names(df)[1]='val';row.names(df)=NULL

You can make making the new columns more dynamic.

df$col=gsub(paste("\\.|*[0-9]",lapply(splitMe[[1]], function(x) paste(names(x), collapse = "|"))[1], collapse = "", sep = "|"),"",df$col)
df$col1=gsub(paste(lapply(splitMe, function(x) paste(names(x), collapse = "|"))[1], collapse = "", sep = "|"),"",df$col)
df$col2=gsub(paste(names(splitMe), collapse = "|"),"",df$col)

Now i get the data in format which ggplot can easily work with.

library(ggplot2)
ggplot(data = df, aes(x = col1,  fill = col2)) + geom_bar()

And you will get a plot like this.enter image description here

Chirayu Chamoli
  • 2,076
  • 1
  • 17
  • 32
  • you would have to grab list names and feed them accordingly to make it dynamic. For pie charts look [here](http://www.sthda.com/english/wiki/ggplot2-pie-chart-quick-start-guide-r-software-and-data-visualization). – Chirayu Chamoli Nov 25 '16 at 17:09
  • Thanks. Any chance to make your solution more dynamic ? I mean, name of list, and column could be changed by different input list. As a beginner, can I get bit more idea on that ? sorry for this question. – Andy.Jian Nov 25 '16 at 18:13
  • 1
    getting names of the list could be more dynamic. look at the edit. – Chirayu Chamoli Nov 26 '16 at 14:02
  • How to indicate number of observation in each data.frame explicitly with corresponding name ? – Andy.Jian Nov 26 '16 at 17:03
  • 1
    i think you are looking for [this](http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2). – Chirayu Chamoli Nov 26 '16 at 17:45
  • if I use your solution, actual number of observation in each data.frame don't match, why is that ? – Andy.Jian Nov 27 '16 at 19:39
  • I used this in your solution: geom_text(aes(label=n), position = position_stack(vjust = .5)), but it didn't work. Why ? – Andy.Jian Nov 28 '16 at 14:23
  • Its because im my data thee is no n for label which you have created using below. plz do the same transformation and run the ggplot again. – Chirayu Chamoli Nov 28 '16 at 14:33
0
I get inspiration form Chirayu Chamoli' solution :

plot_data <- df %>% 
  group_by(col1, col, col2) %>% 
  tally %>% 
  group_by(col, col2) %>% 
  mutate(percentage = n/sum(n), cumsum = cumsum(percentage))


library(ggplot2)
ggplot(data = plot_data, aes(x = col1,  y=n ,fill = col2, width = .85)) + 
  geom_bar(stat = "identity")+
  geom_text(aes(label=n), position = position_stack(vjust = .5))

enter image description here

Andy.Jian
  • 417
  • 3
  • 15