英文:
Separate positions of boxplots and geom_path
问题
以下是代码的翻译部分:
对于这个示例数据:
set.seed(123)
df <- data.frame(
ID = 1:50,
Q = rnorm(50),
A = rnorm(50,1)
)
我想要连接`ID`上的成对点,可以借助以下答案实现:https://stackoverflow.com/questions/75927315/connect-jittered-points-by-group:
pj <- position_jitter(seed = 1, width = .1, height = 0)
df %>%
pivot_longer(-ID) %>%
ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
# 箱线图:
geom_boxplot(
width = 0.12,
outlier.color = NA,
alpha = 0.5
) +
# 数据点:
geom_point(
alpha = 0.5, col = "blue",
position = pj
) +
# 连接线:
geom_path(aes(group = ID),
alpha = 0.5,
position = pj
)
希望这对你有帮助。
英文:
For this illustrative data:
set.seed(123)
df <- data.frame(
ID = 1:50,
Q = rnorm(50),
A = rnorm(50,1)
)
I want to connect the paired points across ID
, which is possible with the help of this answer https://stackoverflow.com/questions/75927315/connect-jittered-points-by-group:
pj <- position_jitter(seed = 1, width = .1, height = 0)
df %>%
pivot_longer(-ID) %>%
ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
# boxplot:
geom_boxplot(
width = 0.12,
outlier.color = NA,
alpha = 0.5
) +
# data points:
geom_point(
alpha = 0.5, col = "blue",
position = pj
) +
# connecting lines:
geom_path(aes(group = ID),
alpha = 0.5,
position = pj
)
What bothers me is that the points overplot the boxplots. I would like them to be separated from the boxplots. Specifically they should be moved inside the space between the boxes, like in this plot:
How can this be achieved? Many thanks in advance.
答案1
得分: 2
以下是代码的翻译部分:
一种选择是移动或微调点和线的位置,需要将 name
转换为数字,并考虑箱线图和抖动宽度:
library(tidyr)
library(dplyr, warn = FALSE)
library(ggplot2)
box_width <- .12
jitter_width <- .1
pj <- position_jitter(seed = 1, width = jitter_width, height = 0)
df %>%
pivot_longer(-ID) %>%
mutate(
name_num = as.numeric(factor(name)),
name_num = name_num + (box_width + jitter_width / 2) * if_else(name == "A", 1, -1)
) %>%
ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
geom_boxplot(
width = box_width,
outlier.color = NA,
alpha = 0.5
) +
geom_point(
aes(x = name_num),
alpha = 0.5, col = "blue",
position = pj
) +
geom_path(aes(x = name_num, group = ID),
alpha = 0.5,
position = pj
)
编辑 要交换 name
类别的位置,请在转换为 factor
时设置所需顺序的级别,并在 ifelse
中考虑这一点。
library(tidyr)
library(dplyr, warn = FALSE)
library(ggplot2)
box_width <- .12
jitter_width <- .1
pj <- position_jitter(seed = 1, width = jitter_width, height = 0)
df %>%
pivot_longer(-ID) %>%
mutate(
name = factor(name, levels = c("Q", "A")),
name_num = as.numeric(factor(name)),
name_num = name_num + (box_width + jitter_width / 2) * if_else(name == "Q", 1, -1)
) %>%
ggplot(aes(x = name, y = value, fill = name)) +
geom_boxplot(
width = box_width,
outlier.color = NA,
alpha = 0.5
) +
geom_point(
aes(x = name_num),
alpha = 0.5, col = "blue",
position = pj
) +
geom_path(aes(x = name_num, group = ID),
alpha = 0.5,
position = pj
)
希望这有所帮助。
英文:
One option would be to shift or nudge the positions of the points and the lines which requires to convert name
to a numeric and to take account of the boxplot and the jitter width:
library(tidyr)
library(dplyr, warn = FALSE)
library(ggplot2)
box_width <- .12
jitter_width <- .1
pj <- position_jitter(seed = 1, width = jitter_width, height = 0)
df %>%
pivot_longer(-ID) %>%
mutate(
name_num = as.numeric(factor(name)),
name_num = name_num + (box_width + jitter_width / 2) * if_else(name == "A", 1, -1)
) |>
ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
geom_boxplot(
width = box_width,
outlier.color = NA,
alpha = 0.5
) +
geom_point(
aes(x = name_num),
alpha = 0.5, col = "blue",
position = pj
) +
geom_path(aes(x = name_num, group = ID),
alpha = 0.5,
position = pj
)
EDIT To switch the positions of the name
categories set the levels in your desired order when converting to a factor
. And of course do we have to take account of that in the ifelse
.
library(tidyr)
library(dplyr, warn = FALSE)
library(ggplot2)
box_width <- .12
jitter_width <- .1
pj <- position_jitter(seed = 1, width = jitter_width, height = 0)
df %>%
pivot_longer(-ID) %>%
mutate(
name = factor(name, levels = c("Q", "A")),
name_num = as.numeric(factor(name)),
name_num = name_num + (box_width + jitter_width / 2) * if_else(name == "Q", 1, -1)
) |>
ggplot(aes(x = name, y = value, fill = name)) +
geom_boxplot(
width = box_width,
outlier.color = NA,
alpha = 0.5
) +
geom_point(
aes(x = name_num),
alpha = 0.5, col = "blue",
position = pj
) +
geom_path(aes(x = name_num, group = ID),
alpha = 0.5,
position = pj
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论