如何解决在ggplot中为图例留空
如何在这两个图中使绘图区域相同?
library(ggplot2)
ggplot(data=iris,mapping = aes(Sepal.Length,Sepal.Width)) + geom_point()
ggplot(data=iris,Sepal.Width,col=Species)) + geom_point()
目标是在动画中叠加绘图,我希望所有间距都相同。
解决方法
一种保留空间的方法是允许创建图例,就像在第二个情节中那样,但是将所有图例元素设置为在背景下不可见。
library(ggplot2)
ggplot(data=iris,mapping = aes(Sepal.Length,Sepal.Width,color = Species)) +
geom_point() +
scale_color_manual(values = rep("black",3)) +
theme(legend.key = element_rect(fill = "white"),legend.text = element_text(color = "white"),legend.title = element_text(color = "white")) +
guides(color = guide_legend(override.aes = list(color = NA)))
ggplot(data=iris,color = Species)) +
geom_point()