繁体   English   中英

ggplot2:绘制2个变量(线和点)并对齐2个图

[英]ggplot2: plot 2 variables (lines and points) and align 2 plots

我最近开始使用ggplot2但是我发现了很多困难......此时我只想将2个不同的变量绘制成一个带点和线的图(类型=两者都在绘图函数中),并得到这个结果图在共享相同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 - 数据没有散布在周围,但在两个图中都按照应有的顺序排序(顺序用绘图函数维护)。

3-使两个图对齐,它们之间的距离最小,而上图中没有x轴。

4 - 将图形对齐但仍然保持上面的图例。

我希望你可以帮助我,我仍然是ggplots2的新手。 非常感谢!

我可能不会使用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 ,并确保正确设置分组。

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

无论如何,对齐这些情节有点棘手。 它有助于尝试按摩刻面以完成大部分对齐。 为此,我rbind编你的数据的两个副本在一起,并创建了一个分组变量,将在站为不同的y轴的标签。

然后我们可以使用facet_grid强制facet条在y轴上,允许自由y比例,然后只将相应的数据子集传递给每个geom。

感谢agstudy,提醒我使用theme旋转x轴标签。

虚拟构面的替代方案,如果您想要单独控制每个绘图,

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)

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM