I have a large, long dataset like this example:
df <- data.frame("Sample" = c("CM","PB","CM","PB"),"Compound" = c("Hydrogen","Hydrogen","Helium","Helium"), "Value" = c(8,3,3,2))
however I have about 162 rows (81 sample/compound pairs)
I am trying to write a loop that prints individual geom_col() plots of each compound where
x=Sample
y=Value
and there are 81 plots for each compound.
I think I am close with this loop:
I want i in "each compound"
etc.
for (i in df$Compound){
print(ggplot(data = i),
aes(x=Sample,
y=Value))
geom_col()
}
What am I missing from this loop? I have also tried facet_wrap(~Compound) However it looks like 81 is too large and each plot is tiny once made. I am looking for a full size bar graph of each compound.
CodePudding user response:
Two issues with your code:
- Your
aes
needs to be combined withggplot(.)
somehow, not as a second argument toprint
. - Your
geom_col
needs to be added to theggplot(.)
chain, not toprint
.
I think then that your code should be
for (i in df$Compound){
print(
ggplot(data = i)
aes(x = Sample, y = Value)
geom_col()
)
}
A known-working example:
for (CYL in unique(mtcars$cyl)) {
print(
ggplot(subset(mtcars, cyl == CYL), aes(mpg, disp))
geom_point()
labs(title = paste("cyl ==", CYL))
)
}
produces three plots (rapidly).
Note:
- If you want a break, consider adding
readline("next ...")
after yourprint
. - I tend to use
gg <- ggplot(..) ... ; print(gg)
(instead ofprint(ggplot(.) ...)
) mostly out of habit, but it can provide a little clarity in errors if/when they occur. It's minor and perhaps more technique than anything.
CodePudding user response:
I think you can loop and pull out the selected data set for each index.
for (i in df$Compound){
print(ggplot(data = df[df$Compound == i,],
aes(x=Sample,
y=Value))
geom_col())
}
(This code also fixes the problems/misplaced parentheses pointed out by @r2evans)
There are a variety of other ways to do this, e.g. split()
the data frame by Compound
, or something tidyverse-ish, or ...