搜索
人大经济论坛 附件下载

附件下载

所在主题:
文件名:  PLSM.xlsx
资料下载链接地址: https://bbs.pinggu.org/a-2221490.html
附件大小:
我已按网上查到的方法做出了双坐标图,但仍有两个问题不知如何解决:1. 右边y轴与图片分离,如何调整?
2. 我已将背景和网格线去除,为何图中还有白线呢?

我所用代码:
library(ggplot2)
library(gtable)
library(grid)

PLSM<-read.csv("PLSM.csv")
PLSM$mon<-factor(as.integer(rownames(PLSM)),labels=PLSM$mon) ## 设置x轴顺序

# two plots
p1 <- ggplot(PLSM, aes(x=mon))
p1 <- p1 + geom_bar( aes(y=pre),stat="identity" , width = 0.5, fill = "cornflowerblue", colour="black") ##图层,柱大小.颜色
p1<-p1 + geom_line(aes(x=mon,y=lea,group=treat,colour=treat)) + geom_point(aes(x=mon,y=lea,group=treat,colour=treat,shap=treat))+ theme_bw()+theme(panel.grid.major=element_line(colour=NA))

p2 <- ggplot(PLSM, aes(x=mon,y=sm, group=treat,colour=treat)) + geom_point() +geom_line() +theme_bw() %+replace%(theme(panel.background = element_rect(fill = NA))+theme(panel.grid.major=element_line(colour=NA)))

# extract gtable
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

# overlap the panel of 2nd plot on that of 1st plot
pp <- 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 tweaks
ia <- 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 it
grid.draw(g)



数据见附件



    熟悉论坛请点击新手指南
下载说明
1、论坛支持迅雷和网际快车等p2p多线程软件下载,请在上面选择下载通道单击右健下载即可。
2、论坛会定期自动批量更新下载地址,所以请不要浪费时间盗链论坛资源,盗链地址会很快失效。
3、本站为非盈利性质的学术交流网站,鼓励和保护原创作品,拒绝未经版权人许可的上传行为。本站如接到版权人发出的合格侵权通知,将积极的采取必要措施;同时,本站也将在技术手段和能力范围内,履行版权保护的注意义务。
(如有侵权,欢迎举报)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

GMT+8, 2026-1-9 02:36