[英]Placing a Legend Title Above a Horizontal Legend Already Positioned at the Top of a Barplot ggplot
Issue:问题:
I have done a lot of research and tried many different solutions and I could not find an answer to my problem, at least not one that worked for me.我做了很多研究并尝试了许多不同的解决方案,但我找不到问题的答案,至少没有一个对我有用。 I am attempting to place the legend title 'Species' above the horizontally placed legend positioned at the top of a barplot (see below) and for it to be centred.
我试图将图例标题“物种”放置在条形图顶部水平放置的图例上方(见下文)并使其居中。
For instance:例如:
In general, I believe and I am using the correct ggplot() commands and I have tried following the examples from other StackOverflow questions.总的来说,我相信并且我正在使用正确的 ggplot() 命令,并且我已经尝试按照其他 StackOverflow 问题中的示例进行操作。 Whatever code I use, nothing happens and the title continues to remain at the lefthand side of the horizontal legend.
无论我使用什么代码,都没有任何反应,标题继续保留在水平图例的左侧。
Would anyone be able to lend a hand?有人能伸出援手吗?
Many thanks in advance.提前谢谢了。
R-Code R代码
#Open Graphics Window
dev.new()
#Barplot
Whistle_Plot<-ggplot(Whistle_Subtype, aes(x = Whistle_Type_Sub, y = N, fill = Species)) +
geom_bar(stat="identity", position=position_dodge(), width = 0.6) +
scale_fill_grey(start = 0.25, end = 0.75) +
theme(axis.text.x = element_text(angle = 75, hjust = 1), text = element_text(size=9)) +
theme(panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()) +
theme(axis.line.x = element_line(color="black", size = 0.8),
axis.line.y = element_line(color="black", size = 0.8)) +
labs(size = 0.8, x = "Whistle Subtypes", y = "Counts of Whistle Subtypes") +
theme(legend.position = 'top',
legend.direction = "horizontal") +
guides(color = guide_legend(title.position = "top",
# hjust = 0.5 centres the title horizontally
title.hjust = 0.5,
label.position = "bottom"))
#Set the limits of the y-axis scale
Whistle_Plot + scale_y_continuous(breaks=seq(0, 80, by=10))
Barplot条形图
If you switch to guides(fill =...)
you should be able do it.如果你切换到
guides(fill =...)
你应该可以做到。
Try:尝试:
guides(fill = guide_legend(title.position = "top", title.hjust=0.5)
(thanks to @teunbrand, for minor correction) (感谢@teunbrand,进行小幅更正)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.