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