gpt4 book ai didi

r - ggplot2 - 当点图不在同一坐标处着色点时,如何使用图例应用手动渐变

转载 作者:行者123 更新时间:2023-12-04 20:31:06 24 4
gpt4 key购买 nike

我知道我正在以一种有点奇怪的方式使用点图,但我已经用它生成了我想要的图形;它显示了每个英超足球俱乐部在每个位置上有多少球员,每个点显示一名球员。我有多个类别 - 显示球员是小队球员还是青年球员,这些是分开绘制的,第二个向下轻推,这样它们就不会重叠。

我想给它添加另一层信息,它根据每个玩家玩了多少分钟来对点进行着色。我的数据框中有这些数据。

它完美地对点进行颜色编码,除非数据被“分组”,在这种情况下它会保留灰色。

screenshot of my plot

我已经阅读了关于产生一个好的 r 问题的指南。我已经减少了数据以显示问题,但不是很大,并删除了所有代码行,例如将数据操作到这一点和图形标题等。

这是一个由 20 个玩家组成的样本,它产生了 16 个颜色漂亮的点和 2 对灰色、未着色的点。

structure(list(team = structure(c(2L, 3L, 4L, 4L, 5L, 6L, 8L, 9L, 11L, 12L, 5L, 6L, 7L, 10L, 12L, 12L, 1L, 4L, 5L, 7L), .Label = c("AFC Bournemouth", "Arsenal", "Brighton & Hove Albion", "Chelsea", "Crystal Palace", "Everton", "Huddersfield Town", "Leicester City", "Liverpool", "Swansea City", "Tottenham Hotspur", "West Bromwich Albion"), class = "factor"), 
role = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "U21", class = "factor"),
name = structure(c(10L, 2L, 1L, 15L, 13L, 19L, 4L, 7L, 20L,
8L, 17L, 9L, 18L, 11L, 3L, 6L, 14L, 5L, 12L, 16L), .Label = c("Boga",
"Brown", "Burke", "Chilwell", "Christensen", "Field", "Grujic",
"Harper", "Holgate", "Iwobi", "Junior Luz Sanches", "Loftus Cheek",
"Lumeka", "Mousset", "Musonda", "Palmer", "Riedwald", "Sabiri",
"Vlasic", "Walker-Peters"), class = "factor"), pos = structure(c(6L,
7L, 6L, 6L, 6L, 5L, 2L, 4L, 3L, 6L, 1L, 1L, 5L, 4L, 6L, 4L,
7L, 1L, 4L, 5L), .Label = c("2. CB", "3. LB", "3. RB", "4. CM",
"5. AM", "5. WM", "6. CF"), class = "factor"), mins = c(11,
24, 18, 1, 25, 10, 90, 6, 90, 20, 99, 180, 97, 127, 35, 156,
32, 162, 258, 124)), .Names = c("team", "role", "name", "pos", "mins"), row.names = 471:490, class = "data.frame")

这是我正在使用的代码:
library(ggplot2)
ggplot()+
geom_dotplot(data=u21, aes(x=team, y=pos, fill=mins), binaxis='y', stackdir="center", stackratio = 1, dotsize = 0.1, binwidth=0.75, position=position_nudge(y=-0.1)) +
scale_fill_gradient(low="pink",high='red')

在我的实际代码中,我再次运行 ggplot 行,但调用不同的数据框,具有不同的颜色渐变和不同的微调,因此点不重叠。

最佳答案

基本上发生的事情是那些“分组”点被视为 NA 值,因为 ggplot 正在接收相同 x,y 坐标的两个最小值,这破坏了着色机制。例如,在“team=Chelsea”和“pos=5.WM”的交点处,有两分钟:18 和 1。以下代码/图表将 NA 值从默认的灰色更改为黄色以显示正在发生的情况:

ggplot()+ 
geom_dotplot(data=df, aes(x=team, y=pos, fill=mins),
binaxis='y', stackdir="center",
stackratio = 1, dotsize = 0.2, binwidth=0.75,
position=position_nudge(y=-0.1)) +
scale_fill_gradient(low="pink",high='red',na.value="yellow") +
theme(axis.text.x = element_text(angle=90, vjust=0.2, hjust=1, size=8))

输出:

enter image description here

这是 geom_dotplot 的创造性测试。并不是说你不能用那种方法做你所要求的,但是用这种方法得到你想要的效果会过于复杂。相反,您可能更喜欢 geom_jitter,它旨在处理绘制此类数据。
ggplot(df)+ 
geom_jitter(aes(x=team, y=pos, col=mins),width = 0.2, height = 0) +
scale_color_gradient(low="pink",high='red',na.value="yellow") +
theme(axis.text.x = element_text(angle=90, vjust=0.2, hjust=1, size=8))

输出:

enter image description here

编辑:

如果你仍然想要带有 dotplot 的复杂版本,避免抖动,那么这里也是:
cols <- colorRampPalette(c("pink","red"))

df$cols <- cols(
max(df$mins,na.rm=T))[findInterval(df$mins,sort(1:max(df$mins,na.rm=T)))]

ggplot()+
geom_dotplot(data=df, aes(x=team, y=pos, col=mins, fill=cols),
binaxis='y',stackdir="centerwhole",stackgroups=TRUE,
binpositions="all",stackratio=1,dotsize=0.2,binwidth=0.75,
position=position_nudge(y=-0.1)) +
scale_color_gradient(low="pink",high='red',na.value="yellow") +
scale_fill_identity() +
theme(axis.text.x = element_text(angle=90, vjust=0.2, hjust=1, size=8))

输出:

enter image description here

对于那些不太熟悉第三张图代码中发生的事情的人:第 1 步是使用 colorRampPalette 存储渐变范围; step 2 根据行的 df$mins 值仔细地为每一行分配一个十六进制颜色值;第 3 步使用设置的颜色和填充参数绘制数据,以便显示图例,但其他灰色(或黄色)分组点由我们通过调用 scale_fill_identity() 设置的正确手动渐变颜色覆盖。通过这种配置,您可以获得正确的颜色和正确的图例。

关于r - ggplot2 - 当点图不在同一坐标处着色点时,如何使用图例应用手动渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46269133/

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