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)
数据见附件