使用ggplot2隐藏刻度时如何保持绘图大小?

时间:2012-12-04 19:55:33

标签: r ggplot2

我正在使用ggplot2绘制散点图。当我隐藏比例尺时,情节会自动因为有点大一点。例如:

ggplot(data = iris, geom = 'blank', aes(y = Petal.Width, x = Petal.Length)) + geom_point()

ggplot(data = iris, geom = 'blank', aes(y = Petal.Width, x = Petal.Length)) +
geom_point() +
theme(axis.title.x = element_blank(),
      axis.title.y = element_blank(),
      legend.position = "none")

第二个更大。我怎么能避免呢?我只想隐藏标尺和标签,但保留情节作为第一个,因为我想要将两者结合起来,一个是尺度,一个是没有,但保持情节大小相同。感谢。

1 个答案:

答案 0 :(得分:4)

棘手但有效。 白色轴

ggplot(data = iris, geom = 'blank', aes(y = Petal.Width, x = Petal.Length)) + geom_point()

+ theme (axis.title.x = element_text(family = "sans", face = "bold"))
ggplot(data = iris, geom = 'blank', aes(y = Petal.Width, x = Petal.Length)) +
  geom_point() +
  theme(axis.title.x = element_text(family = "sans", face = "bold",colour='white'))+
  theme(axis.title.y = element_text(family = "sans", face = "bold",colour='white'))

编辑:一般解决方案

p1 <- ggplot(data = iris, geom = 'blank', aes(y = Petal.Width, x = Petal.Length)) + geom_point()

p2 <- ggplot(data = iris, geom = 'blank', aes(y = Petal.Width, x = Petal.Length)) +
  geom_point() +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "none")

gA <- ggplot_gtable(ggplot_build(p1))
gB <- ggplot_gtable(ggplot_build(p2))
gA$widths  <- gB$widths
gA$heights <- gB$heights

plot(gA)
plot(gB)