3

我正在使用 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")

第二个更大。我怎样才能避免它?我只想隐藏比例和标签,但将情节保留为第一个,因为我想将两者结合起来,一个有比例,一个没有,但保持情节大小相同。谢谢。

4

1 回答 1

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)
于 2012-12-04T20:24:25.527 回答