使用ggplot2对3个不同数据集的3个图进行分面 [英] Facetting 3 plots from 3 different datasets with ggplot2

查看:595
本文介绍了使用ggplot2对3个不同数据集的3个图进行分面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 represents a staked bar graph of the three columns, like so:

此示例显示了df3,其中数据集df3.csv的三列堆叠在另一列之上

这是我的r代码,可产生以上曲线:

Here's my r code to produce the above plot:

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 now need to build a facet plot where df1, df2, and df3 (each showing the three columns, staked) would share the same x axis scale, as such:

对不起,我为这可怕的涂鸦感到抱歉...此外,每个子图都应该是一个堆叠的条形图,因为在图1上,不是密度图对于糟糕的涂鸦,我感到抱歉...而且,每个子图都应该是一个堆叠的条形图,如图1所示,而不是密度图

I'm sorry for the terrible doodle... Also, each subplot should be a stacked bar graph, as on figure 1, not a density plot

我可以做这样的事情吗?

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 need I organize my data differently?

推荐答案

如果重新组织数据,会更容易.您希望所有数据都在一个data.frame中,因此您只需调用一次ggplot.为此,您将需要堆叠所有融化的data.frames并添加一列以指示其来自哪个文件.当我需要读取一堆文件时,可以使用一个名为 read.stack()的帮助程序函数. a>,但是您可以使用数百种不同的方式来准备数据.

It would be easier if you re-organized your data. You want all your data to be in one data.frame so you would call ggplot once. In order to do this, you will need to stack all your melted data.frames and add a column indicating which file it came from. When i need to read in a bunch of files, I use a helper function called read.stack() but there are hundreds of different ways you can prepare your data.

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

Here's what I 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

请注意,我们只是如何添加一列来指示数据源,稍后我们将使用该列来指定构面.现在我们绘图...

Notice how we just added a column indicating the source of the data which we will later use to specify a facet. Now we plot...

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

结果是

这篇关于使用ggplot2对3个不同数据集的3个图进行分面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆