I would like to plot 100 graphs corresponding to 100 data series in one single plot. To be more precise, I have a list ( name : ROC.df
) containing 100 elements where each element is a data frame (name : df
) including two columns POD
and POFD
. I want to plot all 100 graphs in one single plot in a different color. I tried to change the data
argument in ggplot in a loop :
ggplot() +
for (i in 1:100) {
df <- ROC.df[[i]][["df"]]
RGB <- c("R" = runif(1, 0.0, 1.0), "G" = runif(1, 0.0, 1.0), "B" = runif(1, 0.0, 1.0))
geom_line(data = df,
mapping = aes(x = POFD, y = POD), color = rgb(RGB["R"], RGB["G"], RGB["B"]), alpha=0.5, size = 1) +
geom_point(data = df,
mapping = aes(x = POFD, y = POD), color = rgb(RGB["R"], RGB["G"], RGB["B"]), alpha=0.5, size=4)
}
+ theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
The code does not work.
Here is how every df
looks like : (the following content corresponds to ROC.df[[1]][["df"]]
):
POFD POD
1 0.00000000 0.1666667
2 0.01449275 0.1666667
3 0.02898551 0.1666667
4 0.02898551 0.3333333
5 0.04347826 0.3333333
6 0.05797101 0.3333333
7 0.07246377 0.3333333
8 0.08695652 0.3333333
9 0.08695652 0.5000000
10 0.10144928 0.5000000
11 0.10144928 0.6666667
12 0.10144928 0.8333333
13 0.11594203 0.8333333
14 0.13043478 0.8333333
15 0.14492754 0.8333333
16 0.15942029 0.8333333
17 0.31884058 0.8333333
18 0.33333333 0.8333333
19 0.34782609 0.8333333
20 0.34782609 1.0000000
21 0.40579710 1.0000000
22 0.42028986 1.0000000
23 0.43478261 1.0000000
24 0.44927536 1.0000000
25 0.46376812 1.0000000
I searched a bit and found out that it was recommended to merge all data using the melt
function from the reshape2
package. I tried but I did not achieve. Thank you in advance for your help.
Instead of using 100 layers, you could row bind all data.frames in the list and use the col
argument to apply different colors.
Here an example with 10 random data.frames in a list:
library(dplyr)
library(ggplot2)
# Create list of data frames
list_of_dfs <- data.frame(POD = rep(1:10, 10) + rnorm(100),
POFD = rep(5:14, 10) + rnorm(100),
group = factor(LETTERS[rep(1:10, each = 10)])) %>%
split(rep(1:10, each = 10))
# Bind data frames
df <- bind_rows(list_of_dfs)
# Create plot
ggplot(df) +
geom_line(aes(x = POD, y = POFD, col = group)) +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))