本文介绍了添加geom_line将gplot2中一个因子条件下的所有geom_point连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个像下面这样的患者数据。每个科目在4个不同的时间点进行测量。 df result = rnorm( time = rep(c('t1','t2','t3','t4'),12), subject = rep(c(1:12 ),each = 4), gender = rep(c('M','F'),6,each = 4)) 我制作了一个覆盖数据点的箱子图: pd = position_jitterdodge(闪避。 width = 0.75,jitter.width = 0.3) df%>% ggplot(aes(x = time,y = result,fill = gender))+ geom_boxplot(alpha = 0.2 )+ geom_point(aes(color = gender),position = pd)+ scale_fill_brewer(palette ='Set1')+ scale_color_brewer(palette ='Set1') 现在我需要添加行来链接所有患者(数据p沿着时间进程。 df%>% ggplot(aes(x = time,y = result,fill = gender)) + geom_boxplot(alpha = 0.2)+ geom_point(aes(颜色=性别,group =主题),position = pd)+ geom_line(aes(color = gender,group = subject) , position = pd,alpha = 0.3)+ scale_fill_brewer(palette ='Set1')+ scale_color_brewer(palette ='Set1') 所有行似乎'与数据点断开。我该如何解决这个问题?我花了几个小时却找不到解决方案。任何人都可以帮助我吗?非常感谢。 解决方案几天前 position_jitterdodge 在开发版本中获得了种子参数。因此, devtools :: install_github(tidyverse / ggplot2)库(ggplot2) 然后 pd = ggplot2 :: position_jitterdodge(dodge.width = 0.75,jitter.width = 0.3,seed = 1) df%>% ggplot(aes(x = time,y = result,fill =性别))+ geom_boxplot(alpha = 0.2)+ geom_point(aes(color = gender,group = subject),position = pd)+ geom_line(aes(color = gender,group =主题), position = pd,alpha = 0.3)+ scale_fill_brewer(palette ='Set1')+ scale_color_brewer(palette ='Set1') 给出这个图 I have a patient data something like below. Each subject are measured at 4 different time points.df <- data.frame( result = rnorm(48, 1,3), time = rep(c('t1', 't2', 't3', 't4'), 12 ), subject = rep(c(1:12), each=4), gender = rep (c('M', 'F'), 6, each=4) )I made a boxplot with overlaying datapoints:pd = position_jitterdodge(dodge.width = 0.75, jitter.width = 0.3)df %>% ggplot (aes(x= time, y=result, fill=gender))+ geom_boxplot(alpha=0.2)+ geom_point(aes(color = gender),position = pd)+ scale_fill_brewer(palette = 'Set1')+ scale_color_brewer(palette = 'Set1')Now I need to add lines to link all patients (data points) along the time course. df %>% ggplot (aes(x= time, y=result, fill=gender))+ geom_boxplot(alpha=0.2)+ geom_point(aes(color = gender, group=subject),position = pd)+ geom_line(aes(color=gender, group=subject), position=pd, alpha=0.3)+ scale_fill_brewer(palette = 'Set1')+ scale_color_brewer(palette = 'Set1')All the lines seems to 'disconnected from data points. How can I fix this problem? I have spent hours but could not find solution. Could anyone kindly help me with that? Thanks a lot. 解决方案 Some days ago position_jitterdodge gained a seed argument in the development version. So, devtools::install_github("tidyverse/ggplot2")library(ggplot2)and thenpd = ggplot2::position_jitterdodge(dodge.width = 0.75, jitter.width = 0.3, seed = 1)df %>% ggplot (aes(x= time, y=result, fill=gender))+ geom_boxplot(alpha=0.2)+ geom_point(aes(color = gender, group=subject),position = pd)+ geom_line(aes(color=gender, group=subject), position=pd, alpha=0.3)+ scale_fill_brewer(palette = 'Set1')+ scale_color_brewer(palette = 'Set1')gives this plot 这篇关于添加geom_line将gplot2中一个因子条件下的所有geom_point连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 09:01