Facetting 3 plots from 3 different datasets with ggplot2
I have 3 datasets df1, df2, df3 each containing three columns (csv files: https://www.dropbox.com/s/56qh1l5kchsiof0/datasets.zip?dl=0 )
Each dataset is a bar graph of three columns, for example:
This example shows df3 where three columns of the df3.csv dataset are stacked one on top of the other
Here's my r code to create the above graph:
require(reshape2)
library(ggplot2)
library(RColorBrewer)
df = read.csv(".../df3.csv",sep=",", header=TRUE)
df.m = melt(df,c("density"))
c = ggplot(df.m, aes(x = density, y = value/1e+06,fill = variable)) + labs(x = "Density", y = "Cumulated ranks",fill = NULL)
c = c + geom_bar(stat = "identity", position = "stack") + scale_fill_grey(..., start = 0.2, end = 0.8, na.value = "grey50")
c = c + ggtitle('Relative valuation of 75-node resilient networks\naccording to their density') + theme(plot.title = element_text(lineheight=.8, face="bold"))
c
Now I need to plot a facet graph where df1, df2 and df3 (each showing three columns placed on the table) will share the same x-axis scale, as such:
Sorry for the horrible scribble ... Also, each subheading should be a stacked bar chart like in Figure 1, not a density
Can I just do something like this:
require(reshape2)
library(ggplot2)
library(RColorBrewer)
df = read.csv(".../df1.csv",sep=",", header=TRUE)
df.m = melt(df,c("density"))
a = ggplot(df.m, aes(x = density, y = value/1e+06,fill = variable)) + labs(x = "Density", y = "Cumulated ranks",fill = NULL)
a = a + geom_bar(stat = "identity", position = "stack") + scale_fill_grey(..., start = 0.2, end = 0.8, na.value = "grey50")
a = a + ggtitle('subtitle 1') + theme(plot.title = element_text(lineheight=.8, face="bold"))
df = read.csv(".../df2.csv",sep=",", header=TRUE)
df.m = melt(df,c("density"))
b = ggplot(df.m, aes(x = density, y = value/1e+06,fill = variable)) + labs(x = "Density", y = "Cumulated ranks",fill = NULL)
b = b + geom_bar(stat = "identity", position = "stack") + scale_fill_grey(..., start = 0.2, end = 0.8, na.value = "grey50")
b = b + ggtitle('subtitle 2') + theme(plot.title = element_text(lineheight=.8, face="bold"))
df = read.csv(".../df3.csv",sep=",", header=TRUE)
df.m = melt(df,c("density"))
c = ggplot(df.m, aes(x = density, y = value/1e+06,fill = variable)) + labs(x = "Density", y = "Cumulated ranks",fill = NULL)
c = c + geom_bar(stat = "identity", position = "stack") + scale_fill_grey(..., start = 0.2, end = 0.8, na.value = "grey50")
c = c + ggtitle('subtitle 3') + theme(plot.title = element_text(lineheight=.8, face="bold"))
all = facet_grid( _???_ )
Or do I need to order my data differently?
source to share
It would be easier if you refactor your data. You want all of your data to be in one data.frame, so you would call it ggplot
once. To do this, you need to collect all of your melted data.frames and add a column indicating which file it came from. When I need to read a bunch of files, I use the read.stack () helper function , but there are hundreds of different ways you can prepare your data.
Here's what I've tried. First, we prepare the data
ff<-list.files("~/Downloads/datasets/", full=T);
dd<-read.stack(ff, sep=",", header=T, extra=list(file=basename(ff)))
mm<-melt(dd,c("density","file"))
head(mm)
# density file variable value
# 1 0.12 df1.csv modu 50
# 2 0.12 df1.csv modu 472
# 3 0.12 df1.csv modu 145
# 4 0.12 df1.csv modu 59
# 5 0.12 df1.csv modu 51
# 6 0.12 df1.csv modu 86
Note that we just added a column indicating the data source, which we will later use to indicate the face. Now we will speak ...
ggplot(mm, aes(x=density, y=value/1e6, fill=variable)) +
geom_bar(stat="identity", position="stack") +
scale_fill_grey(start = 0.2, end = 0.8, na.value = "grey50") +
labs(x = "Density", y = "Cumulated ranks",fill = NULL) +
ggtitle('Relative valuation of 75-node resilient networks\naccording to their density') +
theme(plot.title = element_text(lineheight=.8, face="bold")) +
facet_grid( file~.)
And the result
source to share