Reputation: 55
My goal is to plot several dataframes (they all have the same structure) using ggplot2. I need to read a csv file so I get a single dataframe then I split it which gives me a list with my datframes.
Dataframe_A <- read.csv("mycsv.csv")
Dataframe_A_split <- split.data.frame(Dataframe_A, list(Dataframe_A$V1,Dataframe_A$V2), drop=TRUE)
Dataframe_A <- data.frame(y1 = c(1, 2, 3,4,5,6,7,9,0,1), y2 = c(1, 3, 3,4,7,6,14,9,7,1), y3 =c("Yes","No","No","Yes","No","No","Yes","No","No","No"), y4=c("A","A","B","A","A","B","A","A","B","A"))
Dataframe_A_split<-split.data.frame(Dataframe_A, list(Dataframe_A$y3, Dataframe_A$y4), drop=TRUE)
$No.A
y1 y2 y3 y4
2 2 3 No A
5 5 7 No A
8 9 9 No A
10 1 1 No A
$Yes.A
y1 y2 y3 y4
1 1 1 Yes A
4 4 4 Yes A
7 7 14 Yes A
$No.B
y1 y2 y3 y4
3 3 3 No B
6 6 6 No B
9 0 7 No B
I know I can use Dataframe_A_split[[1]] to get to the first dataframe but I have twenty dataframe in my list and using ggplot (to do a scatter plot for example) to loop through my list would be useful and easier to read. In my example I would end up with three graphs.
Upvotes: 1
Views: 901
Reputation: 16881
Like I said in my comment above, if there's a reason why you need to work on separate data frames, it's not wrong to go about it with a list of data frames. Just think about your intentions. I do this often when I need the same type of plot repeated for different groups that each need their own separate output. Facets are great for when you have reason to compare groups—think of them like small multiples.
You can use a function that works across a list to create the plots. I'm partial to the purrr::map_*
family, but the base apply
family works too. Using imap
gives you access to the names created by splitting, so you can then identify the plots easily.
library(tidyverse)
plot_list <- Dataframe_A_split %>%
imap(function(df, name) {
ggplot(df, aes(x = y1, y = y2, color = y3)) +
geom_point() +
labs(title = name)
})
plot_list$Yes.A
Created on 2019-03-06 by the reprex package (v0.2.1)
Upvotes: 1
Reputation: 6921
What you want to do is actually probably:
ggplot(Dataframe_A) +
geom_point(aes(x = y1, y = y2)) +
facet_grid(y3 ~ y4)
Consider using an aesthetic to avoid having too many plots.
ggplot(Dataframe_A) +
geom_point(aes(x = y1, y = y2, colour = y3)) +
facet_wrap(~y4)
The possibilities are endless:
ggplot(Dataframe_A) +
geom_point(aes(x = y1, y = y2, colour = y3, shape = y4), size = 5)
Upvotes: 1