R ggplot2:打印2个变量(直线和点)并对齐2个打印
我最近开始使用ggplot2,但我发现有很多困难。。。此时,我只想将两个不同的变量绘制成一个带有点和线的图(在plot函数中type=两者),并将生成的图放置在共享相同x轴的直方图上方并对齐 所以我有这个data.frame:R ggplot2:打印2个变量(直线和点)并对齐2个打印,r,ggplot2,R,Ggplot2,我最近开始使用ggplot2,但我发现有很多困难。。。此时,我只想将两个不同的变量绘制成一个带有点和线的图(在plot函数中type=两者),并将生成的图放置在共享相同x轴的直方图上方并对齐 所以我有这个data.frame: GO.df <- data.frame(GO.ID=paste("GO",c(1:29),sep=""), occ=c(1:29), pv=c(5.379594e-05, 3.05295
GO.df <- data.frame(GO.ID=paste("GO",c(1:29),sep=""),
occ=c(1:29),
pv=c(5.379594e-05, 3.052953e-03, 3.052953e-03, 3.052953e-03, 3.052953e-03, 3.052953e-03, 3.052953e-03, 3.052953e-03, 6.096906e-03, 6.096906e-03, 6.096906e-03, 6.096906e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 1.215791e-02, 1.215791e-02, 1.215791e-02, 1.517502e-02, 1.517502e-02, 1.517502e-02, 1.517502e-02, 1.818323e-02, 1.818323e-02, 1.818323e-02),
adj.pv=c(0.004088492, 0.029003053, 0.029003053, 0.029003053, 0.029003053, 0.029003053, 0.029003053, 0.029003053, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.042000065, 0.042000065, 0.042000065, 0.044357749, 0.044357749, 0.044357749, 0.044357749, 0.047652596, 0.047652596, 0.047652596))
在直方图(变量“occ”)上方,并与其对齐。
这是我到目前为止对ggplot2的了解:
#install.packages("ggplot2")
library(ggplot2)
#install.packages("reshape")
library(reshape)
#install.packages("gridExtra")
library(gridExtra)
GO.df2 <- melt(GO.df, measure.vars=c("pv", "adj.pv"))
p1 <- ggplot(GO.df2, aes(x=GO.ID, y=value, colour=variable)) + geom_point() + ylab("p-values") + xlab(NULL)
p2 <- ggplot(GO.df2, aes(x=GO.ID, y=occ)) + geom_bar(stat="identity") + ylab("Num of Ocurrences")
grid.arrange(
p1,
p2,
nrow = 2,
main = textGrob("GO!", vjust = 1, gp=gpar(fontface = "bold", cex = 1.5)))
#安装程序包(“ggplot2”)
图书馆(GG2)
#安装软件包(“重塑”)
图书馆(重塑)
#安装程序包(“gridExtra”)
图书馆(gridExtra)
GO.df2我可能不会使用grid.arrange
,而是执行类似的操作:
dat <- rbind(GO.df2,GO.df2)
dat$grp <- factor(rep(c('p-values','Num of Ocurrences'),each = nrow(GO.df2)),
levels = c('p-values','Num of Ocurrences'))
dat$GO.ID <- factor(dat$GO.ID,levels = unique(dat$GO.ID))
ggplot(dat,aes(x = GO.ID)) +
facet_grid(grp~.,scales = "free_y") +
geom_point(data = subset(dat,grp == 'p-values'),
aes(y = value,colour = variable)) +
geom_line(data = subset(dat,grp == 'p-values'),
aes(y = value,colour = variable,group = variable)) +
geom_bar(data = subset(dat,grp == 'Num of Ocurrences'),
aes(y = occ),stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ylab("")
dat虚拟切面的替代方案,如果您希望单独对每个绘图进行更多控制
library(gtable) ; library(grid)
p1 <- qplot(GO.ID, value, colour=variable, group = variable,
data = GO.df2, geom=c("point", "line")) +
theme(plot.margin = unit(c(1, 1, -0.5, 0.5), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank())
p2 <- qplot(GO.ID, occ, data = GO.df2, geom="bar", stat="identity") +
theme(plot.margin = unit(c(0, 1, 0.5, 0.5), "lines"))
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g2 <- gtable::gtable_add_cols(g2, widths=unit(0,"mm"))
g <- gtable:::rbind_gtable(g1, g2, "first")
grid.newpage()
grid.draw(g)
库(gtable);图书馆(网格)
非常感谢,这个解决方案看起来非常好。然而,值仍然是“分散的”,而不是有序的。。。有没有可能在图例中添加一个代表“出现次数”的黑色正方形(以及删除“变量”标记)?@DaniCee排序是我的疏忽,现已修复。或者,只需将图例放在上面,并在上面绘制,是否有可能?@DaniCee删除“变量”+主题(legend.title=element_blank())
@DaniCee查看?主题
。您需要仔细研究一长串选项。包括图例。位置
和图例。方向
。
library(gtable) ; library(grid)
p1 <- qplot(GO.ID, value, colour=variable, group = variable,
data = GO.df2, geom=c("point", "line")) +
theme(plot.margin = unit(c(1, 1, -0.5, 0.5), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank())
p2 <- qplot(GO.ID, occ, data = GO.df2, geom="bar", stat="identity") +
theme(plot.margin = unit(c(0, 1, 0.5, 0.5), "lines"))
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g2 <- gtable::gtable_add_cols(g2, widths=unit(0,"mm"))
g <- gtable:::rbind_gtable(g1, g2, "first")
grid.newpage()
grid.draw(g)