How to plot a chart using a list of dataframes in R

I have several dataframes(40) inside a list and I need a way to create a chart that can be modified according to the selected dataframe using plotly or ggplot2

I've put below an example template of how I'm currently doing to create the chart.

aa <- c("José", "Sue")
ab <- c(10, 40)
a <- data.frame(aa,ab)

ba <- c("Marie", "Pablo")
bb <- c(25, 20)
b <- data.frame(ba,bb)

ca <- c("Elizabeth", "Mike")
cb <- c(50, 20)
c <- data.frame(ca,cb)

abc <- list(a,b,c)

names(abc) <- c("a","b","c")



p <- plot_ly() %>%
  add_trace(type = 'bar', x = abc$a$aa,
            y = abc$a$ab, visible=T, marker = list(color = 'blue'))  %>%

  add_trace(type = 'bar',x = abc$b$ba,
            y = abc$b$bb, visible=F, marker = list(color = 'red')) %>%

  add_trace(type = 'bar', x = abc$b$ba,
            y = abc$c$cb, visible=F, marker = list(color = 'yellow'))  %>%

  layout(
    updatemenus = list(
      list(
        yanchor = 'auto',
        buttons = list(
          list(method = "restyle",
               args = list("visible", list(T, F,F)),
               label = 'a'),

          list(method = "restyle",
               args = list("visible", list(F,T, F)),
               label = 'b'),

          list(method = "restyle",
               args = list("visible", list(F, F,T)),
               label = 'c')
        ))))
p

Example: (p) Link of the graphic

I need a way to override the creation of multiple add_trace, using x = abc$a$aa and y = abc$a$ab, for each dataframe accessed in the list.

Upvotes: 2

Views: 1065

Answers (2)

Prem
Prem

Reputation: 11955

I tweaked your code to run in a loop

library(plotly)

p <- plot_ly() 
layout_buttons <- list()
color_list <- palette()  #color list - you may define your own list of colors here
visible_default_layout <- c(T, rep(F, length(abc)-1))    

for (i in seq_along(abc)){
  p <- p %>% add_trace(x = abc[[i]][[1]], y = abc[[i]][[2]], 
                       type = 'bar', visible=visible_default_layout[i], marker = list(color = color_list[i]))
  
  visible_layout <- rep(F, length(abc))
  visible_layout[i] <- T
  layout_buttons[[i]] <- list(method = "restyle",
                              args = list("visible", as.list(visible_layout)),
                              label = names(abc)[i])
  }

p <- p %>%
  layout(updatemenus = list(list(yanchor = 'auto', buttons = layout_buttons)))
p

Sample data:

abc <- structure(list(a = structure(list(aa = structure(1:2, .Label = c("José", 
"Sue"), class = "factor"), ab = c(10, 40)), .Names = c("aa", 
"ab"), row.names = c(NA, -2L), class = "data.frame"), b = structure(list(
    ba = structure(1:2, .Label = c("Marie", "Pablo"), class = "factor"), 
    bb = c(25, 20)), .Names = c("ba", "bb"), row.names = c(NA, 
-2L), class = "data.frame"), c = structure(list(ca = structure(1:2, .Label = c("Elizabeth", 
"Mike"), class = "factor"), cb = c(50, 20)), .Names = c("ca", 
"cb"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("a", 
"b", "c"))

Upvotes: 3

nya
nya

Reputation: 2250

Plotting all data from a list of data.frames can be done with base functions.

par(mfrow = c(1, length(abc)))    # optional, divides plotting area
lapply(abc, FUN = function(x) barplot(x[, 2], names.arg = x[, 1]))

However, overriding an existing plot needs a selection step for the desired data.frame

i <- readline("Enter the name of the data: ")

or a loop that waits until the user has viewed the data

for(i in 1:length(abc)){
   ...
   waiting <- readline("Press enter to view next plot")
}

In both alternatives, i will contain the list name or number with the respective data.frame. We can then use it to select the data for plotting. Note that elements from a list are retrieved with double square brackets, and from a data.frame with single square brackets.

# finds plotting area limits
lims <- max(unlist(lapply(abc, function(x) x[,2])))
# plots chosen data
barplot(abc[[i]][,2], names.arg = abc[[i]][,1], ylim = c(0, lims))

Upvotes: 2

Related Questions