Reputation: 13
I'd like to plot multiple data frames on one plot using ggplot2
.
I have about 30 data frames, and each data frame contains the same variables (x=value, y=index, and standard error) like the example below.
e.g.)
df1 <-
value SE index
1 -3.447418 0.4308221 19.00978
2 -3.999097 0.4308221 147.79562
3 -4.316288 0.4308221 268.78998
4 -4.449099 0.4308221 332.87519
5 -3.696987 0.4308221 447.74797
6 -3.313633 0.4308221 565.46903
7 -3.335039 0.4308221 709.58848
8 -2.486115 0.4308221 838.49382
9 -1.230000 0.4308221 993.37466
10 -2.558116 0.4308221 1150.04461
df2
.
.
.
.
.
df30
I want to plot all 30 data frames with different colors with error bars shown for each value. I am new to R and I was barely able to plot a single data frame with an error bar with the following code.
df1 = read.table("data_1.txt", header=TRUE, sep="\t")
p = ggplot(df1, aes(x=index, y=value)) +
geom_line(size=1, colour = "coral") +
geom_point(size=2.5, colour = "coral") +
ylab("value") + xlab("index")
gp = p + scale_y_continuous(limits=c(-5.5, 0.5), breaks=seq(-5.5, 0.5, 0.5)) +
scale_x_continuous(limits=c(0, 1530), breaks=seq(0, 1530, 250)) +
geom_errorbar(aes(ymax=value+sd, ymin=value-sd), width=20, size=0.2, colour = "black") +
theme_classic()
plot(gp)
I have been looking into Displaying multiple data frames in ggplot2 and R : ggplot2 plot several data frames in one plot, but I could not figure out the best solution. Can someone tell me how I would be able to do what I'm looking for?
Thank you very much for your help in advance.
Upvotes: 1
Views: 2599
Reputation: 1450
You may add to each dataframe a new column identifying it.
df1$id <- "df1" # or any other description you want
df2$id <- "df2"
Then you join all dataframes:
df.all <- rbind(df1, df2, ..., df30)
Now you can plot all the data together by using the group=
and color=
aesthetics options:
p = ggplot(df.all, aes(x=index, y=value, group=id, color=id)) +
geom_line(size=1, colour = "coral") +
geom_point(size=2.5, colour = "coral") +
ylab("value") + xlab("index") +
scale_y_continuous(limits=c(-5.5, 0.5), breaks=seq(-5.5, 0.5, 0.5)) +
scale_x_continuous(limits=c(0, 1530), breaks=seq(0, 1530, 250)) +
geom_errorbar(aes(ymax=value+sd, ymin=value-sd), width=20, size=0.2, colour = "black") +
theme_classic()
I am afraid, though, that 30 overlapping data sets on a single plot may look crowded. You may also want to try with facets, which are small panels for each individual plot, as indicated below.
p = ggplot(df.all, aes(x=index, y=value)) +
geom_line(size=1, colour = "coral") +
geom_point(size=2.5, colour = "coral") +
ylab("value") + xlab("index") +
scale_y_continuous(limits=c(-5.5, 0.5), breaks=seq(-5.5, 0.5, 0.5)) +
scale_x_continuous(limits=c(0, 1530), breaks=seq(0, 1530, 250)) +
geom_errorbar(aes(ymax=value+sd, ymin=value-sd), width=20, size=0.2, colour = "black") +
theme_classic() +
facet_wrap(~id)
Upvotes: 1
Reputation: 295
If you create a list of the dataframes you will be able to use bind_rows
from the dplyr
package, like
df_plot <- bind_rows(df_list, .id = original_df)
ggplot(df_plot, aes(index, value, group = original_df, colour = original_df) +
geom_line(size=1) +
geom_point(size=2.5) +
labs(x = 'index', y = "value") +
scale_y_continuous(limits=c(-5.5, 0.5), breaks=seq(-5.5, 0.5, 0.5)) +
scale_x_continuous(limits=c(0, 1530), breaks=seq(0, 1530, 250)) +
geom_errorbar(aes(ymax=value+sd,
ymin=value-sd),
width=20,
size=0.2,
colour = "black") +
theme_classic()
Upvotes: 2