如何在ggplot2 geom_boxplot中使用实心方块作为图例键

Tao*_*ing 2 r legend ggplot2 boxplot

我正在尝试使用 R ggplot2包来制作箱线图。

但是我只能得到这样的传说。无论如何我可以将那些图例键更改为一个实心方块,而不是使用那些带有中心线的小盒子?

我使用的代码是:

print(ggplot(mydata,aes(x=factor(sp),fill=factor(CommunityType),y=Abundance*100))+geom_boxplot(show_guide=FALSE)
      +theme(axis.text = element_text(colour = "black",size=10))
      +scale_y_continuous(" RA (%) ")+scale_x_discrete(limits=taxalist[1:5]," ")
      +scale_fill_manual(name = "MY type", values = mycol[1:nmc])
      +theme_bw() + guides(fill=guide_legend(title=NULL))+theme(legend.position=c(1,1),legend.justification=c(1,1)) 
      +theme(legend.key = element_blank(),legend.key.size = unit(1.5, "lines"))
      +theme( panel.grid.major = element_blank(),  panel.grid.minor = element_blank(),  panel.background = element_blank())  )
Run Code Online (Sandbox Code Playgroud)

抱歉,我无法在这里形象地描述我的问题。

Ram*_*han 6

这是显示为简单方块的图例,使用了我在这里看到的 Winston Change 使用的 hack 变体。(我正在使用钻石数据集。)

在此处输入图片说明

这个想法是绘制 geom_points (您可以控制其图例,并完全抑制箱线图的图例)

library(ggplot2)
p <- ggplot() + geom_point(data=diamonds, aes(x=cut, y=mean(depth), color=clarity), shape=15, size=5)
p <- p + guides(color=guide_legend(title=NULL)) 
p <- p + theme(legend.key = element_blank())
p <- p  + geom_boxplot(data=diamonds,aes(x=cut,fill=factor(clarity),y=depth)) + guides(fill=FALSE)
p
Run Code Online (Sandbox Code Playgroud)

还可以查看github 上Hadley 的Legend-Attributes 页面