我的问题与 this question 非常相似,但不一样。
我正在寻找一种方法来创建一个只有图例的空 ggplot。但是,与我在顶部链接的问题的自动名称相反,我实际上需要创建 图例,而图像中不包含 绘图区域。
我尝试了以下代码:
ggplot(NULL, aes(color = ""))+
geom_blank()+
scale_color_manual(values = "black", labels = "Something")+
guides(color = guide_legend())+
theme(legend.box.background = element_rect(color = "black"))
但我得到的结果与我想要的相反 - 我得到一个没有图例的空地 block 区域,如下所示:
我希望我的最终结果看起来像这样(我在 Paint 中画了这个):
任何帮助将不胜感激!
最佳答案
您可以制作一个正常的情节,然后玩主题以达到预期的效果。
library(ggplot2)
ggplot(data.frame(x = 1, y = 1, colour = 'Something'), aes(x, y, fill = colour))+
geom_point(alpha=0, shape = 0)+ # completely transparent rectangular point
scale_fill_manual(values='black', drop=FALSE) +
guides(fill = guide_legend(override.aes = list(alpha=1, size = 40)))+ # showing the point in the legend
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = c(0.5, 0.5), # move the legend to the center
legend.title = element_blank(),
legend.text = element_text(size = 40),
legend.key = element_rect(fill='NA'),
panel.grid = element_blank(),
panel.border = element_rect(colour = "black", fill='white', size=1)
)
关于r - 如何仅用图例绘制一个空的ggplot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63763215/