按照@eipi10的思路,可以将标题的名称添加为标签,带有white的值:
diamonds$cut = factor(diamonds$cut, levels=c("Title 1 ","Fair","Good"," ","Title 2","Very Good",
"Premium","Ideal"))
ggplot(diamonds, aes(color, fill=cut)) + geom_bar() +
scale_fill_manual(values=c("white",hcl(seq(15,325,length.out=5), 100, 65)[1:2],
"white","white",
hcl(seq(15,325,length.out=5), 100, 65)[3:5]),
drop=FALSE) +
guides(fill=guide_legend(ncol=2)) +
theme(legend.position="bottom",
legend.key = element_rect(fill=NA),
legend.title=element_blank())
我在"Title 1 " 之后引入了一些空格来分隔列并改进设计,但可能会有增加空间的选项。
唯一的问题是我不知道如何更改“标题”标签的格式(我尝试了bquote 或expression,但没有成功)。
_____________________________________________________________
根据您尝试的图表,图例的右对齐可能是更好的选择,而且这个技巧看起来更好(恕我直言)。它将图例一分为二,更好地利用了空间。您所要做的就是将ncol 改回1,并将"bottom" (legend.position) 改回"right":
diamonds$cut = factor(diamonds$cut, levels=c("Title 1","Fair","Good"," ","Title 2","Very Good","Premium","Ideal"))
ggplot(diamonds, aes(color, fill=cut)) + geom_bar() +
scale_fill_manual(values=c("white",hcl(seq(15,325,length.out=5), 100, 65)[1:2],
"white","white",
hcl(seq(15,325,length.out=5), 100, 65)[3:5]),
drop=FALSE) +
guides(fill=guide_legend(ncol=1)) +
theme(legend.position="bottom",
legend.key = element_rect(fill=NA),
legend.title=element_blank())
在这种情况下,通过删除 legend.title=element_blank() 将标题保留在此版本中可能是有意义的