更新时间:2023-11-26 12:47:46
我不知道是否有一种方法可以独立于文本来控制图例颜色框的宽度(除了破解图例杂项之外).但是,如果在主题语句中添加legend.key.width=unit(1.5, "cm")
,则所有颜色框都将扩展为相同的宽度(您可能需要向上或向下调整1.5来获得所需的框宽).
I don't know if there's a way to control the width of the legend color boxes separately from the text (other than hacking the legend grobs). However, if you add legend.key.width=unit(1.5, "cm")
in your theme statement, all of the color boxes will be expanded to the same width (you may have to tweak the 1.5 up or down a bit to get the desired box widths).
library(scales)
ggplot(dataFrame, aes(x=color, y = percent))+
geom_bar(aes(fill = cut), stat = "identity") +
geom_text(aes(label = pretty_label), position=position_fill(vjust=0.5),
colour="white", size=3)+
coord_flip()+
theme(legend.position="top",
legend.key.width=unit(1.5, "cm"))+
guides(fill = guide_legend(label.position = "bottom", reverse = TRUE)) +
scale_y_continuous(labels=percent)
您可以通过将Very Good
放在两行上来节省一些空间:
You can save a little space by putting Very Good
on two lines:
library(forcats)
ggplot(dataFrame, aes(x=color, y = percent, fill = fct_recode(cut, "Very\nGood"="Very Good")))+
geom_bar(stat = "identity") +
geom_text(aes(label = pretty_label), position=position_fill(vjust=0.5),
colour="white", size=3)+
coord_flip()+
theme(legend.position="top",
legend.key.width=unit(1.2, "cm"))+
guides(fill = guide_legend(label.position = "bottom", reverse = TRUE)) +
labs(fill="Cut") +
scale_y_continuous(labels=percent)