在空的ggplot上绘制图例 [英] Draw a Legend to an empty ggplot

查看:66
本文介绍了在空的ggplot上绘制图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个没有数据的空ggplot,但可以根据R中的定义值绘制图例?

那么创建的图像仅仅是图例吗?

最初,我以为我可以绘制数据并在其上打印一个白色矩形,但是鉴于并非每个背景都是白色的,所以这是不现实的.我尝试了下面的方法,但是ggplot似乎想要一些数据框,因此我不得不添加一个.是否可以不添加一个图形或以其他方式创建空白图形?

 库(ggplot2)结果<-c("A","B","C","D","E")镜头<-rep('Label',5)xc< -c(1:5)yc< -c(1:5)df< -data.frame(shots,Outcome,xc,yc)p<-ggplot()+geom_point(数据= df,aes(x = xc,y = yc,填充=结果),shape = 22,size = 4,color ='black',stroke = 1)+#颜色和图例scale_fill_manual(values = c('chartreuse3','gainsboro','dodgerblue3','firebrick2','cornsilk4'),标签= c("A","B","C","D",'E'),掉落=假)+#主题主题(panel.background = element_rect(fill ="transparent",colour = NA),plot.margin = unit(c(0.1,0.1,0.1,0.1),"cm"),plot.title = element_text(大小= 14,高度= 0.5,垂直= 1),plot.background = element_rect(填充=透明",颜色= NA),axis.title = element_blank(),axis.text = element_blank(),axis.ticks = element_blank(),legend.position ='左',legend.title = element_text(大小= 15),legend.text = element_text(大小= 15),legend.background = element_rect(填充=透明"))p 

解决方案

是否要使用完整的地块大小,但空间为空?如果是这样,我喜欢您对主题所做的修改,但是我在这里省略了它们,因此可以更清楚地看出在何处绘制了哪些元素.

如果这不是您要的内容,是否可以通过 解决> geom_blank() ?

Is it possible to create a empty ggplot with no data, but draw a legend based on defined values in R?

So the image created is simply the legend?

Originally I thought I could plot the data and print a white rectangle over it, but given that not every background is white, it is unrealistic. I have tried the below, but it looks like ggplot wants some dataframe, therefore I had to add one. Is it possiable not to add one or create blank graph a different way?

library(ggplot2)

Outcome <- c("A", "B", "C", "D",'E')
shots <- rep('Label',5)
xc <-c(1:5)
yc <-c(1:5)
df <-data.frame(shots,Outcome,xc,yc)
p <- ggplot() +
  geom_point(data = df, aes(x = xc, y = yc, fill = Outcome), shape=22, size=4,color = 'black', stroke=1) +

  #Color and Legend
  scale_fill_manual(values=c('chartreuse3','gainsboro','dodgerblue3','firebrick2','cornsilk4'),
                    labels = c("A", "B", "C", "D",'E'), 
                    drop = FALSE) +
  #Theme
  theme(
    panel.background = element_rect(fill = "transparent",colour = NA),
    plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"),
    plot.title = element_text(size = 14, hjust = 0.5, vjust = 1),
    plot.background = element_rect(fill = "transparent", colour = NA),
    axis.title=element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    legend.position = 'left',
    legend.title=element_text(size=15),
    legend.text=element_text(size=15),
    legend.background = element_rect(fill = "transparent")
  )
p

解决方案

Do you want the full plot size, but with the space empty? If so, the override.aes parameter can help. Make the plotted points fully transparent (alpha=0), but the legend points fully opaque (alpha=1).

palette_color <- c("A"='chartreuse3', "B"='gainsboro', "C"='dodgerblue3', "D"='firebrick2', "E"='cornsilk4')

ggplot(df, aes(x = xc, y = yc, fill = Outcome)) +
  geom_point(shape=22, alpha=0) +
  # geom_blank() +
  scale_fill_manual(values=palette_color, drop=FALSE) +
  guides(fill = guide_legend(override.aes = list(alpha=1))) 

I like your theme modifications, but I left them off here so it's more clear which elements are drawn where.

If this isn't what you're asking for, is it something addressed by geom_blank()?

这篇关于在空的ggplot上绘制图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆