gpt4 book ai didi

r - 在 R 中向哑铃图添加图例

转载 作者:行者123 更新时间:2023-12-02 01:20:08 26 4
gpt4 key购买 nike

使用以下代码,我创建了一个哑铃图。

f <- ggplot(Brewers_PA, aes(x=PA.2015, xend=PA.2016, y=Name))
f + geom_dumbbell(colour = "darkblue", point.colour.l = "darkred", point.colour.r = "darkBlue", point.size.l = 2.5, point.size.r = 2.5) +
theme(plot.background=element_rect(fill = "grey93", colour = "grey93")) +
theme(plot.title=element_text(size = 11, face = "bold", hjust = 0)) +
theme(axis.text.x=element_text(size = 8)) +
theme(axis.text.y=element_text(size = 8)) +
theme(axis.title.x=element_text(size = 9)) +
theme(axis.title.y=element_text(size=9)) + ylab("") + xlab("Plate Appearance") +
ggtitle("Brewers Change in Plate Appearance 2015-2016")

感谢本教程,我能够做到这一点。 https://www.r-bloggers.com/beating-lollipops-into-dumbbells/

唯一的问题是我想为此添加图例,但我不知道如何添加。有人知道吗?感谢所有帮助。

我基本上希望图例能够显示颜色的年份。因此,“深蓝色”= 2016 年(PA.2016),“深红色”= 2015 年(PA.2015)。我想添加图片,但由于某种原因它不起作用。

这是我创建的数据框:

               Name     PA.2015 PA.2016
1 Jonathan Villar 128 679
2 Chris Carter 460 644
3 Ryan Braun 568 564
4 Scooter Gennett 391 542
5 Hernan Perez 272 430
6 Kirk Nieuwenhuis 141 392
7 Jonathan Lucroy 415 544
8 Aaron Hill 353 429
9 Ramon Flores 289 289
10 Domingo Santana 187 281
11 Martin Maldonado 256 253
12 Keon Broxton 2 244
13 Orlando Arcia 0 216

最佳答案

Pkg 作者,在这里。

我建议不要为读者添加额外的解释周期,并使其成为图表中的“传奇”,并删除一些“图表垃圾”美学:

library(ggplot2)
library(ggalt)

read.table(text='Name PA.2015 PA.2016
"Jonathan Villar" 128 679
"Chris Carter" 460 644
"Ryan Braun" 568 564
"Scooter Gennett" 391 542
"Hernan Perez" 272 430
"Kirk Nieuwenhuis" 141 392
"Jonathan Lucroy" 415 544
"Aaron Hill" 353 429
"Ramon Flores" 289 289
"Domingo Santana" 187 281
"Martin Maldonado" 256 253
"Keon Broxton" 2 244
"Orlando Arcia" 0 216', header=TRUE, quote='"') -> Brewers_PA

f <- ggplot()
f <- f + geom_dumbbell(data=Brewers_PA,
aes(x=PA.2015, xend=PA.2016, y=Name),
colour = "darkblue",
point.colour.l = "darkred", point.colour.r = "darkBlue",
point.size.l = 2.5, point.size.r = 2.5)
f <- f + geom_text(data=data.frame(),
aes(x=391, y= "Scooter Gennett", label="2015"),
color="darkred", hjust=1, size=3, nudge_x=-10)
f <- f + geom_text(data=data.frame(),
aes(x=542, y= "Scooter Gennett", label="2016"),
color="darkBlue", hjust=0, size=3, nudge_x=10)
f <- f + scale_x_continuous(expand=c(0,10))
f <- f + labs(x=NULL, y=NULL,
title="Brewers Change in Plate Appearance 2015-2016")
f <- f + theme(plot.background=element_rect(fill = "grey93", colour = "grey93"))
f <- f + theme(plot.title=element_text(size = 11, face = "bold", hjust = 0))
f <- f + theme(axis.text.x=element_text(size = 8))
f <- f + theme(axis.text.y=element_text(size = 8))
f <- f + theme(axis.title.x=element_text(size = 9))
f <- f + theme(axis.title.y=element_text(size=9))
f <- f + theme(axis.ticks=element_blank())
f <- f + theme(panel.grid.major.y=element_blank())
f <- f + theme(plot.margin=margin(30,30,30,30))
f

enter image description here

关于r - 在 R 中向哑铃图添加图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40011005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com