带连接各个daa点的线的箱线图

时间:2019-08-29 21:10:13

标签: ggplot2 boxplot

我想制作一个箱形图,其中将我正在调查的两个条件下的个人数据点连接到每个度量(即LN1__00,LN2__00等)中。但是,我很难做到这一点。

这是一些示例代码和图表...

LN1__00 <- c(5.5,2.5,4.5,3.0,5.5,11.5)
LN2__00 <- c(9.5,9.5,5.5,7.0,11.5,17.5)
LN3__00 <- c(26.5,42.5,40.5,18.0,27.5,32.5)
condition <- c("1","2","1","2","1","2")
PB_ID <- c("A","A","B","B","C","C")

Sleepstages_Lat <- data.frame(LN1__00,LN2__00,LN3__00,condition,PB_ID)

Sleepstages_Lat2 <- melt(Sleepstages_Lat, id.vars = c("PB_ID", "condition"))

# PLOT
plottitle = "Conditions"
subtitle = "Sleep (Stage) Latencies"

# define some stuff
colour_datapoints = "gray45" # gray45
shape_datapoints = 1
size_datapoints = 2
stroke_datapoints = 1 # thickness of circles

margins = unit(c(1, 8, 1, 1), 'lines')
p <- ggplot (Sleepstages_Lat2, aes(x = variable, 
                               y=value, 
                               fill = condition))
p <- p + geom_boxplot(outlier.shape = NA, 
                  alpha = 0.9, 
                  colour="black", 
                  notch = F)+ 
geom_point(shape = shape_datapoints, 
          size = size_datapoints, 
          colour = colour_datapoints, 
          stroke = stroke_datapoints,
         position = position_jitterdodge(jitter.width = 0.1, dodge.width = 0.7))+
geom_line(aes(group = interaction(PB_ID, variable)), alpha = 0.6, colour = "black", data = Sleepstages_Lat2,
        position = position_jitterdodge(dodge.width = 0.7))+
theme_bw()+
coord_flip()
p

我知道这有多个线程,但是无法找到正确的答案。

Boxplots

2 个答案:

答案 0 :(得分:0)

这并不是解决您描述的问题的真正方法,但是如果它适合您的情况,则可能是解决问题的一种方法。如果您明确地将变量和条件组合在一起,则可以避免必须使用任何位置规避。

LN1__00 <- c(5.5,2.5,4.5,3.0,5.5,11.5)
LN2__00 <- c(9.5,9.5,5.5,7.0,11.5,17.5)
LN3__00 <- c(26.5,42.5,40.5,18.0,27.5,32.5)
condition <- c("1","2","1","2","1","2")
PB_ID <- c("A","A","B","B","C","C")

Sleepstages_Lat <- data.frame(LN1__00,LN2__00,LN3__00,condition,PB_ID)

Sleepstages_Lat2 <- reshape2::melt(Sleepstages_Lat, id.vars = c("PB_ID", "condition"))

Sleepstages_Lat2 = Sleepstages_Lat2 %>%
  unite(test, c(variable,condition), sep = "_", remove = "FALSE")

p  <- ggplot (Sleepstages_Lat2, aes(x = test, y = value, fill = condition)) +
        geom_boxplot(outlier.shape = NA) + 
        geom_point() +
        geom_line(aes(group = interaction(PB_ID, variable)))+
        theme_bw() +
        coord_flip()
p

enter image description here

答案 1 :(得分:0)

这段代码满足了我的需要...

LN1__00 <- c(5.5,2.5,4.5,3.0,5.5,11.5)
LN2__00 <- c(9.5,9.5,5.5,7.0,11.5,17.5)
LN3__00 <- c(26.5,42.5,40.5,18.0,27.5,32.5)
condition <- c("1","2","1","2","1","2")
PB_ID <- c("A","A","B","B","C","C")

Sleepstages_Lat <- data.frame(LN1__00,LN2__00,LN3__00,condition,PB_ID)

Sleepstages_Lat2 <- melt(Sleepstages_Lat, id.vars = c("PB_ID", "condition"))

Sleepstages_Lat2$var.cond = paste(Sleepstages_Lat2$variable, Sleepstages_Lat2$condition, sep = "_")

#create jitter
b1 <- runif(nrow(Sleepstages_Lat2), -0.2, -0.1)
b2 <- runif(nrow(Sleepstages_Lat2), 0.1, 0.2)
Sleepstages_Lat2$b_corr <- NA
for (i in 1:nrow(Sleepstages_Lat2)){
  if (Sleepstages_Lat2$condition[i] == 1){
Sleepstages_Lat2$b_corr[i] <- as.numeric(Sleepstages_Lat2$variable[i])+b1[i]
  }else{
Sleepstages_Lat2$b_corr[i] <- as.numeric(Sleepstages_Lat2$variable[i])+b2[i]
  }
}

# PLOT
plottitle = "Conditions"
subtitle = "Sleep (Stage) Latencies"

# define some stuff
colour_datapoints = "gray45" # gray45
shape_datapoints = 1
size_datapoints = 2
stroke_datapoints = 1 # thickness of circles

margins = unit(c(1, 8, 1, 1), 'lines')
p <- ggplot (Sleepstages_Lat2, aes(x = variable, 
                               y=value, 
                               fill = condition))
p <- p + geom_boxplot(outlier.shape = NA, 
                  alpha = 0.9, 
                  colour="black", 
                  notch = F)+ 
  geom_point(shape = shape_datapoints, 
          size = size_datapoints, 
          colour = colour_datapoints, 
          stroke = stroke_datapoints,
          aes(x = b_corr,
          group = var.cond))+
  geom_line(aes(x = b_corr, y = value, group=interaction(PB_ID, variable)), colour =     "gray68", show.legend = FALSE, linetype="dashed")+  
  theme_bw()+
  coord_flip()
p