Philipp Neuber
Philipp Neuber

Reputation: 144

Ggplotting over a list or list to dataframes

I splitted a dataframe to a list, which looks like this:

data <- list(data.frame(name = sample(c("John","Jo","Marc","Donald Dumb"),3, replace = T), sales = sample(1:10,3, replace = T)),
     data.frame(name = sample(c("John","Jo","Marc","Donald Dumb"),3, replace = T), sales = sample(1:10,3, replace = T)))
data

I want too ggplot the sales over the different data.frames in the list, but I dont how exactly to do this. I tried unlist but the result isnt proper.

Thank you and stay healthy

Upvotes: 1

Views: 120

Answers (2)

akrun
akrun

Reputation: 887781

Using tidyverse

library(purrr)
library(dplyr)
library(ggplot2)
map(data, ~ .x %>%
               ggplot(aes(x = name, y = sales)) +
                geom_col(aes(fill = name), color = 'black'))

Upvotes: 1

Duck
Duck

Reputation: 39613

Try this:

library(ggplot2)
#Plot
lapply(data, function(x) ggplot(x,aes(x=name,y=sales))+
         geom_col(aes(fill=name),color='black'))

Upvotes: 1

Related Questions