欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

R ggplot绘制双纵坐标轴

发布时间:2023/12/20 编程问答 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 R ggplot绘制双纵坐标轴 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
p1 <- ggplot(data = DNA_result, mapping = aes( x= num, y = nd)) + geom_point(colour = "red") + xlab( "样本序号") + ylab("浓度")+theme(plot.caption = element_text(hjust = 0))+ theme_bw() p1p2 <- ggplot(data = DNA_result, mapping = aes( x= num, y = A280)) + geom_point(colour = "blue") + xlab( "样本序号") + ylab("A260/A280")+theme(plot.caption = element_text(hjust = 0))+ theme_bw() %+replace% theme(panel.background = element_rect(fill = NA)) p2double_y_axis <- function(p1, p2){g1 <- ggplot_gtable(ggplot_build(p1))g2 <- ggplot_gtable(ggplot_build(p2))# overlap the panel of 2nd plot on that of 1st plotpp <- c(subset(g1$layout, name == "panel", se = t:r))g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, pp$l, pp$b, pp$l)# axis tweaksia <- which(g2$layout$name == "axis-l")ga <- g2$grobs[[ia]]ax <- ga$children[[2]]ax$widths <- rev(ax$widths)ax$grobs <- rev(ax$grobs)ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)# draw itgrid.draw(g)return(g) } double_y_axis(p1,p2)

ref1  

ref2

ref3

总结

以上是生活随笔为你收集整理的R ggplot绘制双纵坐标轴的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。