4

我最近开始使用 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 还是很陌生。非常感谢!

4

2 回答 2

6

我可能不会使用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("")

在此处输入图像描述

绘制线条只需添加geom_line,并确保分组设置正确。

对 x 轴进行排序,就像 ggplot 中的其他所有内容一样,只需要创建一个因子并正确排序级别。

诚然,对齐地块有点棘手。尝试按摩刻面为您完成大部分对齐会有所帮助。为此,我rbind将您的数据的两个副本一起编辑,并创建了一个分组变量,该变量将作为不同的 y 轴标签。

然后我们可以使用facet_grid强制分面条在 y 轴上,允许自由的 y 比例,然后只将适当的数据子集传递给每个几何图形。

感谢 agstudy,提醒我使用theme.

于 2013-07-11T16:20:00.987 回答
2

虚拟刻面的替代方法,如果您想单独控制每个图,

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)

在此处输入图像描述

于 2013-07-11T16:51:10.257 回答