r - 使用ggplot2隐藏比例时如何保持绘图大小?

标签 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")

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

最佳答案

棘手但有效。 白色轴

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)

关于r - 使用ggplot2隐藏比例时如何保持绘图大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13710842/

相关文章:

r - 在 R 传单中添加许多折线

r - R lubridate 中两个日期之间的严格差异

列表列表中的R求和向量

r - 通过散点图上垂直堆叠的点绘制线段

r - ggplot2 中 qplot() 和 ggplot() 之间的选择

r - 创建条形图时的 Ggplot() 图形问题

r - Purrr:哪个嵌套数据框遇到了错误?

r - 按行索引(数据的原始顺序)而不是按字母顺序设置 ggplot 中 x 或 y 的顺序

r - 如何将误差范围的阴影添加到图表中

r - 提取递增子序列