将传奇标题放在已经位于barplot Ggplot顶部的水平传说上方

发布于 2025-01-31 02:21:41 字数 2794 浏览 2 评论 0原文

问题:

我做了很多研究,并尝试了许多不同的解决方案,但我找不到解决我的问题的答案,至少没有一个对我有用的问题。我试图将传奇标题“物种”放置在位于小barphot顶部的水平放置的传说之上(见下文),并以此为中心。

例如:

解决方案1 ​​

解决方案3

我一般都相信,我相信我是使用正确的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:

Solution 1

Solution 2

Solution 3

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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌枕肩 2025-02-07 02:21:41

如果您切换到指南(fill = ...),则应该可以这样做。

尝试:(

guides(fill = guide_legend(title.position = "top", title.hjust=0.5)

感谢@teunbrand,进行较小的更正)

If you switch to guides(fill = ...) you should be able do it.

Try:

guides(fill = guide_legend(title.position = "top", title.hjust=0.5)

(thanks to @teunbrand, for minor correction)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文