我最近开始使用 ggplot2 但我发现了很多困难......此时我只想将 2 个不同的变量绘制成一个带有点和线的图(type=both 在绘图函数中),并得到这个结果图放置并对齐在共享相同 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.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))
并想重现这个:
plot(GO.df$pv, type="b", col="red", ylim=c(0,0.05),ylab="",xlab="",xaxt="n")
lines(GO.df$adj.pv, type="b", col="blue")
axis(1, at=c(1:length(GO.df$GO.ID)), labels=GO.df$GO.ID, las=2)
在直方图(变量“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)))
如您所见,我无法:
1-绘制线和点
2-在两个图中都没有散布数据,而是按应有的顺序排列数据(使用 plot 函数保持顺序)。
3-使两个图与它们之间的最小距离对齐,并且在上面的图中没有 x 轴。
4-使图对齐,但仍保持上述图例。
我希望你能帮我解决这个问题,我对 ggplots2 还是很陌生。非常感谢!