用ggplot2行在boxplot上连接数据点

用ggplot2行在boxplot上连接数据点

本文介绍了用ggplot2行在boxplot上连接数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在以下图形上可视化成对的数据点集:

  ggplot(grid.mag.ROIcontrols.allRuns,aes(Model,Grid_Magnitude))+geom_boxplot(aes(fill = Model),outlier.shape = NA,alpha = 0.6)+geom_point(aes(填充=模型),尺寸= 2,形状= 21,位置= position_jitterdodge(0.2))+geom_line(aes(group = Side))+facet_grid(〜Side,scales ="free")+scale_fill_brewer(palette ="GnBu")+实验室(标题=平均网格幅度,pm与al EC") 

线连接了 alLeft6 / pmLeft6 alRight6 / pmRight6 之间的点.

但是带有我需要的组变量的 geom_line 不起作用-当我需要为10对中的每对使用一条水平线时,它在数据点之间添加垂直线和一条水平线./p>

没有 geom_line :

使用 geom_line :

PS:对不起,我不知道如何共享原始数据...

解决方案

没有实际数据,很难为您提供深入帮助,请参考

I'm trying to visualize paired sets of data points on the following graphs:

ggplot(grid.mag.ROIcontrols.allRuns, aes(Model,Grid_Magnitude)) +
  geom_boxplot(aes(fill=Model),outlier.shape = NA,alpha=0.6) +
  geom_point(aes(fill=Model),size=2,shape=21,position=position_jitterdodge(0.2)) +
  geom_line(aes(group=Side)) +
  facet_grid(~Side,scales = "free") +
  scale_fill_brewer(palette="GnBu") +
  labs(title = "Average Grid Magnitude, pm vs al EC")

Lines are joining the points between alLeft6/pmLeft6 and between alRight6/pmRight6.

However geom_line with the group variable that I need doesn't work - it adds vertical lines and one horizontal line between data points, when I need one horizontal line for each of the 10 pairs.

Without geom_line:

With geom_line:

PS: Sorry, I don't know how to share the raw data...

解决方案

Without the actual data it is hard give you in depth help, please refer to this site for a guide for a great reproducible example, as mentioned in the comments.

I am assuming you want to compare one datapoint from alLeft6 to one from pmLeft6 (otherwise the horizontal line would make little sense). This indicates you have some column in your data linking these points together (Pairs in the example data).

With made up data this would be as easy as setting the geom_line() grouping variable to this column (Pairs). To align the geom_point() with the geom_line() with jitter an easy solution is to define the offset before the ggplot call (here called pd).

library(tidyverse)
grid.mag.ROIcontrols.allRuns = tibble(Model = c(rep("alLeft6", 10),rep("pmLeft6", 10),rep("alRight6", 10),rep("pmRight6", 10)),
                                      Grid_Magnitude = c(runif(10, -1, 1),runif(10, -0.5, 1.5), runif(10, -1, 1),runif(10, -1.5, 0.5)),
                                      Side = c(rep("Left", 20), rep("Right", 20)),
                                      Pair = c(rep(1:10, 2), rep(11:20, 2))
) %>%
  mutate(Pair = as.factor(Pair))

pd <- position_dodge(0.2)
ggplot(grid.mag.ROIcontrols.allRuns, aes(Model,Grid_Magnitude)) +
  geom_boxplot(aes(fill=Model),outlier.shape = NA,alpha=0.6) +
  geom_line(aes(group=Pair), position = pd) +
  geom_point(aes(fill=Model,group=Pair),size=2,shape=21, position = pd) +
  facet_grid(~Side,scales = "free") +
  scale_fill_brewer(palette="GnBu") +
  labs(title = "Average Grid Magnitude, pm vs al EC")

这篇关于用ggplot2行在boxplot上连接数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:08