使用ggplot2从3个不同的数据集中绘制3个图

时间:2014-09-13 19:15:47

标签: r ggplot2 facet

我有3个数据集df1,df2,df3,每个包含三列(csv文件:https://www.dropbox.com/s/56qh1l5kchsiof0/datasets.zip?dl=0

每个数据集代表三列的放样条形图,如下所示:

This example shows df3, where the three columns of the dataset df3.csv are stacked one on top of the other 此示例显示df3,其中数据集df3.csv的三列一个堆叠在另一个

这是我的代码,用于生成上述情节:

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

我现在需要构建一个方面图,其中df1,df2和df3(每个显示三个列,放样)将共享相同的x轴刻度,如下所示:

I'm sorry for the terrible doodle... Also, each subplot should be a stacked bar graph, as on figure 1, not a density plot 我很抱歉这个可怕的涂鸦......而且,每个子图应该是一个堆积的条形图,如图1所示,而不是密度图

我可以这样做:

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( _???_ )

或者我需要以不同方式组织数据吗?

1 个答案:

答案 0 :(得分:1)

如果您重新组织数据会更容易。您希望所有数据都在一个data.frame中,这样您就可以调用ggplot一次。为此,您需要堆叠所有熔化的data.frames并添加一个列,指示它来自哪个文件。当我需要读取一堆文件时,我使用了一个名为read.stack()的辅助函数,但是有几百种不同的方法可以准备数据。

这是我尝试过的。首先,我们准备数据

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

注意我们是如何添加一个列,指示我们稍后将用于指定构面的数据源。现在我们绘制......

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~.)

结果是

enter image description here