打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
利用ggplot2画双坐标轴曲线
userphoto

2015.08.27

关注
library(ggplot2)library(gtable)library(grid)grid.newpage()# two plotsp1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue") + theme_bw()p2 <- ggplot(mtcars, aes(mpg, drat)) + geom_line(colour = "red") + theme_bw() %+replace%    theme(panel.background = element_rect(fill = NA))# extract gtableg1 <- 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)ia <- which(g2$layout$name == "ylab")ga <- g2$grobs[[ia]]ga$rot <- 270g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)g <- gtable_add_grob(g, ga, pp$t, length(g$widths) - 1, pp$b)# draw itgrid.draw(g)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
用R来拼图和排版,告别AI和PS(二):调节宽度和高度
如何用patchwork拼R语言大部分类型出图和图片
图上嵌图!
ggplot2 - 更改分面的相对宽度
R语言ggplot2画图-Pathway富集分析气泡图
【R分享|实战】地表最全R拼图教程,告别AI和PS
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服