将传奇标题放在已经位于barplot Ggplot顶部的水平传说上方
问题:
我做了很多研究,并尝试了许多不同的解决方案,但我找不到解决我的问题的答案,至少没有一个对我有用的问题。我试图将传奇标题“物种”放置在位于小barphot顶部的水平放置的传说之上(见下文),并以此为中心。
例如:
我一般都相信,我相信我是使用正确的ggplot()命令,我尝试按照其他stackoverflow问题的示例进行了尝试。无论我使用哪种代码,都不会发生任何事情,标题继续保留在水平传奇的左侧。
有人可以伸出援手吗?
非常感谢。
r-code
#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
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. 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
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您切换到
指南(fill = ...)
,则应该可以这样做。尝试:(
感谢@teunbrand,进行较小的更正)
If you switch to
guides(fill = ...)
you should be able do it.Try:
(thanks to @teunbrand, for minor correction)