[英]How to connect grouped points in ggplot within groups?
我有一個包含兩組的數據集 - 實驗組和控制組。 每個參與者每組貢獻兩個響應,代表不同的學習方式。 這些在下面帶有抖動的箱形圖中表示。 我想使用 ggplot 將每個參與者的兩個響應與線條連接在一起(因此對照組中的每條紅線都對應於對照組中的每條綠松石線),但是我不知道如何在條件下做到這一點。 有人可以幫忙嗎? 我是 R 新手,真的需要指導。
然后,如果增加 = TRUE,我需要將條件內線條的顏色更改為黑色,如果增加 = FALSE,我需要將線條的顏色更改為紅色。
理想情況下,我需要它看起來像 Jon 的示例,但使用基於 True 或 False 的黑色或紅色線條: Connecting grouped points with lines in ggplot
數據和 ggplot 如下所示:
d <- data.frame (
Subject = c("1", "2", "3", "4"),
Group = c("Exp", "Exp", "Control", "Control"),
Tr = c("14", "11", "4", "23"),
Sr = c("56", "78", "12", "10"),
Increase = c("TRUE", "TRUE", "TRUE", "FALSE")
)
# put the data in long format
d <- d %>%
gather(key = "Strategy", value = "raw", Tr, Sr)
d %>%
ggplot(aes(x = Group, y = raw, color = Strategy)) +
geom_boxplot(width = 0.5, lwd = 0.5) +
geom_jitter(width = 0.15) +
geom_line(aes(group = raw),
color = "grey",
arrow = arrow(type = "closed",
length = unit(0.075, "inches")))
靈感來自您鏈接到的答案- @Jon 的答案
有幾個關鍵的事情要理解解決方案
Group
繪制為一個因子,解釋為數字 1、2、3、.. 對應於因子級別。 因此,我們創建了一個數值向量 group_jit,其值在 1 和 2 左右,其偏移量基於着色變量Strategy
以在 1 和 2 左右略微左右移動。fill
,將線條表示為colour
,以避免單個圖例上有 4 個東西。這是代碼 -
library(tidyverse)
# Load data
d <- data.frame (
Subject = c("1", "2", "3", "4"),
Group = c("Exp", "Exp", "Control", "Control"),
Tr = c("14", "11", "4", "23"),
Sr = c("56", "78", "12", "10"),
Increase = c("TRUE", "TRUE", "TRUE", "FALSE")
)
width_jitter <- 0.2 # 1 means full width between points
# put the data in long format
d_jit <- d %>%
gather(key = "Strategy", value = "raw", Tr, Sr) %>%
# type conversions
mutate(across(c(Group, Strategy), as_factor)) %>% # convert to factors
mutate(raw = as.numeric(raw)) %>% # make raw as numbers
# position on x axis is based on combination of Group and jittered Strategy. Mix to taste.
mutate(group_jit = as.numeric(Group) + jitter(as.numeric(Strategy) - 1.5) * width_jitter * 2,
grouping = interaction(Subject, Strategy))
# plotting
d_jit %>%
ggplot(aes(x = Group, y = raw, fill = Strategy)) +
geom_boxplot(width = 0.5, lwd = 0.5, alpha = 0.05, show.legend = FALSE) +
geom_point(aes(x = group_jit), size = 3, shape = 21) +
geom_line(aes(x = group_jit,
group = Subject,
colour = Increase),
alpha = 0.5,
arrow = arrow(type = "closed",
length = unit(0.075, "inches"))
) +
scale_colour_manual(values = c('red', 'black'))
由reprex 包於 2022-05-14 創建 (v2.0.1)
為了完整起見,進行抖動的另一種更優雅的方法是為geom_point
和geom_line
命令提供position
參數。 這個參數是一個像這樣添加隨機抖動的函數(來源: @erocoar's answer )
position = ggplot2::position_jitterdodge(dodge.width = 0.75, jitter.width = 0.3, seed = 1)
這樣數據本身不會改變,並且繪圖會處理抖動細節
jitterdodge
進行閃避(x 軸變量的偏移)和抖動(彩色點的小噪聲)seed
參數是關鍵,因為它確保為獨立調用它的點和線函數返回相同的隨機值不是您的問題的直接答案,但我想建議一種替代的可視化。
您正在處理配對數據。 使用散點圖可以實現更令人信服的可視化。 您將使用論文的兩個維度,而不是將您的兩個維度僅映射到一個維度上。 您可以更好地將控制與受試者進行比較,並立即查看哪一個變得更好或更糟。
library(tidyverse)
d <- data.frame (
Subject = c("1", "2", "3", "4"),
Group = c("Exp", "Exp", "Control", "Control"),
Tr = c("14", "11", "4", "23"),
Sr = c("56", "78", "12", "10"),
Increase = c("TRUE", "TRUE", "TRUE", "FALSE")
) %>%
## convert to numeric first
mutate(across(c(Tr,Sr), as.integer))
## set coordinate limits
lims <- range(c(d$Tr, d$Sr))
ggplot(d) +
geom_point(aes(Tr, Sr, color = Group)) +
## adding a line of equality and setting limits equal helps guide the eye
geom_abline(intercept = 0, slope = 1, lty = "dashed") +
coord_equal(xlim = lims , ylim = lims )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.