簡體   English   中英

ggplot2 - 當點圖不是在相同坐標上着色點時,如何應用帶圖例的手動漸變

[英]ggplot2 - How to apply a manual gradient with a legend when dot plot isn't coloring points at the same coordinates

我知道我正在以一種奇怪的方式使用dotplot,但我已經得到了它生成我想要的圖形; 這顯示了每個英超聯賽足球俱樂部每個位置有多少名球員,每個球員都有一名球員。 我有多個類別 - 顯示玩家是小隊運動員還是青年運動員,這些是單獨繪制的,第二個是輕微的,因此它們不會重疊。

我想為它添加另一層信息,根據每個玩家玩了多少分鍾來點亮點。 我的數據框中有這些數據。

它完美地對點進行顏色編碼,除非數據是“分組”的,在這種情況下它會使其保持灰色。

我的情節的截圖

我已經閱讀了關於提出一個好問題的指導。 我已經減少數據以顯示問題,而不是很大,並刪除了所有代碼行,例如操縱數據到此點和圖表標題等。

這是一個由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坐標的兩個min值,這打破了着色機制。 例如,在“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))

輸出:

在此輸入圖像描述

這是geom_dotplot的創意測試。 並不是說你不能用你的方法做出你想要的東西,但是用這種方法得到你想要的效果會過於復雜。 相反,你可能會對geom_jitter有更多好運,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))

輸出:

在此輸入圖像描述

編輯:

如果您仍然想要使用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))

輸出:

在此輸入圖像描述

對於那些不太熟悉第三個圖的代碼中發生的事情的人:步驟1是使用colorRampPalette存儲漸變范圍; 步驟2根據行的df $ mins值小心地為每一行指定一個十六進制顏色值; 第3步使用顏色和填充參數設置繪制數據,以便顯示圖例,但其他灰色(或黃色)分組點由我們通過調用scale_fill_identity()設置的正確手動漸變顏色覆蓋。 使用此配置,您可以獲得正確的顏色和正確的圖例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM