3

我正在尝试删除带有第三个 ggplot 的 gtable 中的两个 ggplot 上的 y 轴。我想在 gtable 中显示最左边图表的 y 轴,并从后续图表中完全删除 y 轴;但是,我希望 x 轴保留在所有图上。

我的图表如下所示:![核苷酸多样性][1]

[1]:代码生成的图像

library("ggplot2")
library("gridExtra")
library("gtable")

theme_set(theme_bw(base_size=16))


p1 <- ggplot(a.pi, aes(x=window, y=measure, fill=key, colour=key)) + 
  geom_line() + 
  scale_colour_manual(values=c("#000099", "#333333", "#FF0000")) + 
  ylab(expression(pi)) + 
  xlab("Position") +
  scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
  scale_y_continuous(limits=c(0.0,0.0004)) +
  theme(#axis.text.y = element_blank(),
        #axis.ticks.y = element_blank(),
        #axis.title.y = element_blank(),
        #axis.title.x = element_blank(),
        plot.margin = unit(c(0,-3,0,0), "lines"),
        plot.background = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        legend.position="none",
        axis.line = element_line()
        ) 

p2 <- ggplot(b.pi, aes(x=window, y=measure, fill=key, colour=key)) + 
  geom_line() + 
  scale_colour_manual(values=c("#333333", "#FF0000")) + 
  #ylab(expression(pi)) + 
  xlab("Position") +
  scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
  scale_y_continuous(limits=c(0.0,0.0004)) +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        #axis.title.x = element_blank(),
        plot.margin = unit(c(0,-3,0,0), "lines"),
        plot.background = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        legend.position="none",
        axis.line = element_line()
        ) 

p3 <- ggplot(c.pi, aes(x=window, y=measure, fill=key, colour=key)) + 
  geom_line() + 
  scale_colour_manual(values=c("#333333", "#FF0000")) + 
  #ylab(expression(pi)) + 
  xlab("Position") +
  scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
  scale_y_continuous(limits=c(0.0,0.0004)) +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        #axis.title.x = element_blank(),
        plot.margin = unit(c(0,-3,0,0), "lines"),
        plot.background = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        legend.position="none",
        axis.line = element_line()
        ) 

grid.arrange(p1,p2,p3, nrow=1)


gt1 <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- ggplot_gtable(ggplot_build(p3))

newWidth = unit.pmax(gt1$widths[1:3], gt2$widths[1:3], gt3$widths[1:3])

gt1$widths[1:3] = as.list(newWidth)
gt2$widths[1:3] = as.list(newWidth)
gt3$widths[1:3] = as.list(newWidth)

# New gtable with space for the three plots plus a right-hand margin
gt = gtable(widths = unit(c(1, 1, 1, 0.3), "null"), height = unit(1, "null"))


# Instert gt1, gt2 and gt2 into the new gtable
gt <- gtable_add_grob(gt, gt1, 1, 1)
gt <- gtable_add_grob(gt, gt2, 1, 2)
gt <- gtable_add_grob(gt, gt3, 1, 3)

grid.newpage()
grid.draw(gt)
4

2 回答 2

1

您的链接图像未显示,但这是我在黑暗中拍摄的照片:

从此改变:

plot1 <- theme(#axis.text.y = element_blank(),...

对此:

plot1 <- theme(axis.text.y = element_text(),...

如果要更改标签,请执行以下操作:

plot1 <- ... + ylab("y-axis label")
于 2013-08-09T13:44:28.787 回答
0

你需要做两件事:

一个是在选项(opts)下将axis.title.x和axis.title.y设置为空白。另一个是设置 xlab("") 和 ylab("")

我已经包含了一个代码片段以防万一:

ggplot(space[1:closetos[i],], aes(dim1, dim9, colour = name, 
shape=shape))+ opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank(),
panel.background = theme_blank(),
axis.title.x = theme_blank(),
axis.title.y = theme_blank())+
               xlab("") +
               ylab("")+
               theme(text = element_text(size=15, colour = "black"),
                     axis.text.x = element_text(angle=0, vjust=1, colour = "black"),
                     axis.text.y = element_text(angle=0, vjust=1, colour = "black"),
                     axis.line = element_line(colour = 'black', size = 1),
                     axis.ticks = element_line(colour = 'black', size = 1),
                     axis.ticks.length = unit(0.3, "cm"),
                     axis.title.y=element_text(vjust=0.4),
                     legend.position = "none") + 
               geom_point(size=5)+
               scale_color_manual("Status", values = mycolours) +
               xlim((space$dim1[closetos[i]]-0.01), (space$dim1[closetos[i]]+0.01)) + 
               ylim((space$dim9[closetos[i]]-0.01), (space$dim9[closetos[i]]+0.01))
于 2014-10-22T01:54:15.960 回答