gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:36:06 24 4
gpt4 key购买 nike

我最近开始使用 ggplot2,但我发现很多困难......此时我只想将 2 个不同的变量绘制成一个带有点和线的图(类型 = 都在 plot 函数中),并得到这个结果图放置并对齐在共享相同 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 还是很陌生。非常感谢!

最佳答案

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

enter image description here

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

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

无可否认,对齐图有点棘手。尝试按摩刻面以帮助您完成大部分对齐工作。为此,我 rbind将数据的两个副本放在一起,并创建了一个分组变量,作为不同的 y 轴标签。

然后我们可以使用 facet_grid强制面带位于 y 轴上,允许自由 y 比例,然后仅将数据的适当子集传递给每个几何体。

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

关于r - ggplot2:绘制 2 个变量(线和点)并对齐 2 个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17597720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com