2

I am trying to use R ggplot2 package to make a boxplot.

However I can only get legend like this. Is there anyway I can change those legend key to just a solid square, instead of using those small box with central line?

The code I used is:

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())  )

Sorry I cannot image here to describe my question.

4

1 回答 1

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

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

于 2013-08-01T21:44:05.967 回答