我试图在x轴上连接两种不同方法(* measure )测量值之间的抖动点,这些测量值通过先证者( a )相互关联,先证者可分为两个主要组,患者( pat )和对照( ctr *)。我的df如下:
set.seed(1)
df <- data.frame(a = rep(paste0("id", "_", 1:20), each = 2),
value = sample(1:10, 40, rep = TRUE),
measure = rep(c("a", "b"), 20), group = rep(c("pat", "ctr"), each = 2,10))
我试过了
library(ggplot2)
ggplot(df,aes(measure, value, fill = group)) +
geom_point(position = position_jitterdodge(jitter.width = 0.1, jitter.height = 0.1,
dodge.width = 0.75), shape = 1) +
geom_line(aes(group = a), position = position_dodge(0.75))
由reprex package(v0.3.0)于2020年1月13日创建
我使用fill
美学来分离两组抖动的点(* pat * 和 * ctr *),我意识到当我将group = a
美学放入ggplot
主调用时,它不会很好地分离,但似乎更好地链接到点。
我的问题:有没有一种方法可以更好地将线连接到(抖动)点,同时保持两个主要组 * ctr * 和 * pat * 的分离?
多谢了。
2条答案
按热度按时间kadbb4591#
你遇到的最大问题是你只避开了
group
的点,但是线也被a
避开了。要保持直线与坐标轴保持原样,一种选择是手动减淡数据,这利用了因子是整数的特性,将
group
的一个级别向右移动,另一个级别向左移动。然后,您可以使用
measure
作为x轴进行绘图,但随后使用“被回避”变量作为geom_point
和geom_line
中的x轴变量。如果你还想抖动,也可以手动添加到你的x和y变量。
yxyvkwin2#
事实证明,这是一个令人惊讶的常见问题,我想给自己加上一个答案/评论,并建议一个--我现在认为--更好的可视化:
我最初打算显示配对数据,并在两个比较之间直观地引导眼睛。这种可视化的问题是显而易见的:每个对象都被可视化了两次。这导致了一个相当拥挤的图形。此外,数据的两个维度(测量之前和之后)被强制为一个维度(y),ID的连接被笨拙地强制到x轴上。
散点图自然地表示ID,每个受试者只显示一个点,但在x和y上更自然地显示两个维度。唯一需要的步骤是使数据更宽(是的,这有时也是必要的,ggplot并不总是需要长数据)。