使用facet_grid选项使用ggplot2绘制数据框的列 [英] Use facet_grid option to plot column of dataframe with ggplot2
问题描述
我从仿真中得到了一些结果,我想制作一个如下图所示的构面,并且我不知道是否可以使用ggplot2和facet_grid选项来实现这一点.
我的模拟结果具有这种简化的"形式,模拟一行:
dat <- read.table(textConnection("P1 P2 P3 P4 R
1 2e-5 1.0 0.6 3 1
2 4e-6 1.5 0.7 1.5 2
3 6e-7 1.2 0.6 2.5 3
4 8e-8 1.45 0.65 3.2 4
"))
在这里您可以看到我想用ggplot2和facet_grid制作的简化图形
按参数我有一个构面/图,每个我的参数的最小/最大值对应于不同的比例...您还可以在此图形上看到颜色对应关系,以将每个模拟定位在参数空间中. >
非常感谢您的帮助
SR.
这是一个开始:
library(plyr)
library(ggplot2)
dat.melt <- melt(data, id.vars='R')
ggplot(dat.melt, aes(x=0, y=value)) +
geom_boxplot() +
geom_point(aes(colour=factor(R))) +
facet_wrap(~variable, ncol=1, scales='free') +
coord_flip()
还有很多其他调整,但是要点仍然存在.通常,最好的第一步是将数据长melt
,然后开始摆弄.附带说明一下,避免使用data
作为变量名是一个好主意,因为它是内置函数.
I get some result from simulation, and i want to make a facetting like this diagram below, and i don't know if it's possible to make this with ggplot2 and the facet_grid option.
My result for simulations have this "simplified" form, one line by simulation :
dat <- read.table(textConnection("P1 P2 P3 P4 R
1 2e-5 1.0 0.6 3 1
2 4e-6 1.5 0.7 1.5 2
3 6e-7 1.2 0.6 2.5 3
4 8e-8 1.45 0.65 3.2 4
"))
And here you can see the simplified graphic i want to produce with ggplot2 and facet_grid
I have one facet/graph by parameter, with different scale corresponding to min/max values for each of my parameter... You can also see on this graphic the color correspondance to localize each simulation in parameter space.
Thanks a lot for help
SR.
Here is a start:
library(plyr)
library(ggplot2)
dat.melt <- melt(data, id.vars='R')
ggplot(dat.melt, aes(x=0, y=value)) +
geom_boxplot() +
geom_point(aes(colour=factor(R))) +
facet_wrap(~variable, ncol=1, scales='free') +
coord_flip()
Plenty of other tweaks, but the gist is there. Usually a good first step is to melt
the data long and then start fiddling. As a side note, its a good idea to avoid using data
for a variable name since its a built in function.
这篇关于使用facet_grid选项使用ggplot2绘制数据框的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!