R 从图形中提取一个方面
我有两个图,每个图显示不同类型的数据(疫苗和疾病),我想提取这些图的片段,以生成每个受试者的图,其中包含疫苗和疾病,根据原始图上色R 从图形中提取一个方面,r,ggplot2,gtable,R,Ggplot2,Gtable,我有两个图,每个图显示不同类型的数据(疫苗和疾病),我想提取这些图的片段,以生成每个受试者的图,其中包含疫苗和疾病,根据原始图上色 vac subject age vaccine 1 E11 1.0 DTaP-IPV 2 E11 3.0 DTaP-IPV 3 E22 1.0 DTaP-IPV 4 E22 2.0 Rota 5 E22 3.0 DTaP-IPV 6 E22 3.3 Rota ill subject age
vac
subject age vaccine
1 E11 1.0 DTaP-IPV
2 E11 3.0 DTaP-IPV
3 E22 1.0 DTaP-IPV
4 E22 2.0 Rota
5 E22 3.0 DTaP-IPV
6 E22 3.3 Rota
ill
subject age illness
1 E11 0.5 ear infection
2 E11 2.0 ear infection
3 E22 0.8 fever
4 E22 1.2 fever
5 E22 3.0 ear infection
ggplot(vac,aes(x=age,y=subject,color=vaccine))+geom_point(size=5) +
scale_color_brewer(palette="Set1",drop = FALSE)+facet_grid(subject~.)
现在,我想为E11绘制一个图,其中有一行疫苗(用Set1颜色表示),一行疾病(用Set3颜色表示),E22也是如此
我有很多数据类型,约40个主题,所以当然,我希望这是自动完成的,而不是通过illustrator等
我认为GTTable是一条路要走,但不确定如何提取一个方面
多谢各位 这里有一个快速的解决方案,可能会帮助您朝着正确的方向前进。试试看
require(reshape2)
require(plyr)
dat <- melt(join(vac, ill, type="full"), id.vars = c("subject", "age"), na.rm=TRUE)
ggplot(dat, aes(x=age,y=variable, color=value))+geom_point(size=5) +
scale_color_brewer(palette="Set1",drop = FALSE)+facet_grid(subject~.)
require(重塑2)
需要(plyr)
dat下面的代码将vac
和ill
组合在一个数据帧中(添加一个新变量type
以区分疫苗和疾病),这将允许我们在调用ggplot
的一次调用中绘制疾病和疫苗。代码有点笨重,但我希望它能让你更接近你想要的东西
library(RColorBrewer)
library(gridExtra)
# Combine data frames vac and ill by adding a new column
# called "type" and changing name of the third column to "condition"
vac$type = "Vaccine"
names(vac)[3] = "condition"
ill$type = "Illness"
names(ill)[3] = "condition"
dat = rbind(vac, ill)
# Convert condition to a factor to get the levels ordered properly
dat$condition = factor(dat$condition,
levels=c(unique(dat$condition[dat$type=="Vaccine"]),
unique(dat$condition[dat$type=="Illness"])))
dat
subject age condition type
1 E11 0.5 ear infection Illness
2 E11 2.0 ear infection Illness
3 E22 3.0 ear infection Illness
4 E22 0.8 fever Illness
5 E22 1.2 fever Illness
6 E11 1.0 DTaP-IPV Vaccine
7 E11 3.0 DTaP-IPV Vaccine
8 E22 1.0 DTaP-IPV Vaccine
9 E22 3.0 DTaP-IPV Vaccine
10 E22 2.0 Rota Vaccine
11 E22 3.3 Rota Vaccine
现在我们为每个主题生成一个图,将每个图放在一个列表中,然后将所有图保存在一个PDF文件中。ggplot
代码的工作原理如下:
- 通过
主题
刻面,这样我们就可以得到一个带有主题ID的条带
- 通过
类型
,我们可以得到疾病和疫苗的单独图表
- 使用下面创建的调色板获得我们想要的颜色
pal1=c(brewer.pal(n=3,name=“Set1”)[1:2],
brewer.pal(n=3,name=“Set3”)[1:2])
下面是图表:
在您在问题中创建的面中,没有在面中的主题有空行。拥有约40个主题,这将创建一个填充行和39个空行的面,这可能不是您想要的
另一种解决方案:
# merging the dataframes together
dat <- merge(vac, ill, by=c("subject","age"), all=TRUE, sort=TRUE)
# creating the plot
ggplot() +
geom_point(data=dat[!is.na(dat$vaccine),], aes(x=age, y=subject, fill=vaccine), size=10, shape=22) +
geom_point(data=dat[!is.na(dat$illness),], aes(x=age, y=subject, color=illness), size=7, shape=17) +
scale_fill_brewer(palette="Set1") +
scale_color_brewer(palette="Set2") +
theme_bw()
#将数据帧合并在一起
dat谢谢Leandro,但这里的问题是我只能为所有点提供一种颜色方案。如果我单独生成绘图,我可以为每个变量提供不同的颜色方案。谢谢我不确定除了使用单独的配色方案来形成一个“主”配色方案,然后应用于整个绘图之外,是否还有其他方法。但为了做到这一点,您需要了解每种数据类型有多少因素。如果你在这方面需要帮助,我可以提供一个例子。你想要一个传奇吗?因为如果你这样做的话,那将是另一个问题。我可以不用传奇。谢谢@你的问题有几个有用的答案。由于您对Stack Overflow非常陌生,因此我想提醒您注意以下帮助页面:
p = list()
for (i in unique(dat$subject)) {
p[[i]] = ggplot(dat[dat$subject==i,],
aes(x=age, y=condition, colour=condition)) +
geom_point(size=5) +
scale_color_manual(values=pal1, drop=FALSE) +
facet_grid(type ~ subject, scale="free") + ylab("") +
guides(colour=FALSE)
}
pdf("plots.pdf", 9,5)
do.call("grid.arrange", p)
dev.off()
# merging the dataframes together
dat <- merge(vac, ill, by=c("subject","age"), all=TRUE, sort=TRUE)
# creating the plot
ggplot() +
geom_point(data=dat[!is.na(dat$vaccine),], aes(x=age, y=subject, fill=vaccine), size=10, shape=22) +
geom_point(data=dat[!is.na(dat$illness),], aes(x=age, y=subject, color=illness), size=7, shape=17) +
scale_fill_brewer(palette="Set1") +
scale_color_brewer(palette="Set2") +
theme_bw()