geom_half_violin 情节右侧被切断

geom_half_violin on right side of plot cut off

我想在这个图的右侧绘制一些 geom_half_violin

parggsave 似乎都无法解决问题。我究竟做错了什么?提前致谢! (下面的复制示例)

## Install packages

#library("plyr")
#library("lattice")
library("ggplot2")
library("dplyr")
#library("readr")
#library("rmarkdown")
#library("Rmisc")
#library("devtools")
library("gghalves")
# width and height variables for saved plots
w = 6
h = 4
# Define limits of y-axis
y_lim_min = 4
y_lim_max = 7.5

before = iris$Sepal.Width[1:50]
during = iris$Sepal.Length[51:100]
after = iris$Sepal.Length[1:50]
follow = iris$Sepal.Length[51:100]
n <- length(before) 
d <- data.frame(y = c(before, during, after, follow),
                x = rep(c(1,2,3,4), each=n),
                z = rep(c(5,6,7,8), each=n),
                id = factor(rep(1:n,4)))
set.seed(321)
d$xj <- jitter(d$x, amount = .09) 
d$xj_2 <- jitter(d$z, amount = .09)
#d$xj_3 <- jitter(d$a, amount = .09)

#par(mar=c(7,7,6,2.1)) #doesnt work
ggplot(data=d, aes(y=y)) +
  
  #Add geom_() objects
  geom_point(data = d %>% filter(x=="1"), aes(x=xj), color = 'dodgerblue', size = 1.5,
             alpha = .6) +
  geom_point(data = d %>% filter(x=="2"), aes(x=xj), color = 'darkgreen', size = 1.5,
             alpha = .6) +
  geom_point(data = d %>% filter(x=="3"), aes(x=xj), color = 'darkorange', size = 1.5, 
             alpha = .6) +
  geom_point(data = d %>% filter(x=="4"), aes(x=xj), color = 'red', size = 1.5, 
             alpha = .6) +
  
  #Add geom_() objects
  geom_point(data = d %>% filter(z=="5"), aes(x=xj_2), color = 'green', size = 1.5,
             alpha = .6) +
  geom_point(data = d %>% filter(z=="6"), aes(x=xj_2), color = 'blue', size = 1.5,
             alpha = .6) +
  geom_point(data = d %>% filter(z=="7"), aes(x=xj_2), color = 'orange', size = 1.5, 
             alpha = .6) +
  geom_point(data = d %>% filter(z=="8"), aes(x=xj_2), color = 'red', size = 1.5, 
             alpha = .6) +
  
  geom_line(aes(x=xj, group=id), color = 'lightgray', alpha = .3) +
  geom_line(aes(x=xj_2, group=id), color = 'lightgray', alpha = .3) +
  
  geom_half_violin(
    data = d %>% filter(x=="1"),aes(x = x, y = y), position = position_nudge(x = -0.2), 
    side = "l", fill = 'dodgerblue', alpha = .5, color = "dodgerblue", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(x=="2"),aes(x = x, y = y), position = position_nudge(x = -1.2), 
    side = "l", fill = "darkgreen", alpha = .5, color = "darkgreen", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(x=="3"),aes(x = x, y = y), position = position_nudge(x = -2.2), 
    side = "l", fill = "darkorange", alpha = .5, color = "darkorange", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(x=="4"),aes(x = x, y = y), position = position_nudge(x = -3.2), 
    side = "l", fill = 'red', alpha = .5, color = "red", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(z=="5"),aes(x = x, y = y), position = position_nudge(x = 7), 
    side = "r", fill = "green", alpha = .5, color = "green", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(z=="6"),aes(x = x, y = y), position = position_nudge(x = 6), 
    side = "r", fill = "blue", alpha = .5, color = "blue", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(z=="7"),aes(x = x, y = y), position = position_nudge(x = 5), 
    side = "r", fill = "orange", alpha = .5, color = "orange", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(z=="8"),aes(x = x, y = y), position = position_nudge(x = 4), 
    side = "r", fill = "red", alpha = .5, color = "red", trim = TRUE) +
  
  #Define additional settings
  scale_x_continuous(breaks=c(1,2,3,4,5,6,7,8), labels=c("Before", "During", "After","Follow", "Before", "During", "After", "Follow"), limits=c(0, 8)) +
  xlab("Condition") + ylab("Value") +
  ggtitle('Figure 13: Repeated measures with jittered datapoints and connections') +
  theme_classic()+
  coord_cartesian(ylim=c(2, 7.5))

好吧,这很尴尬。发布问题后我自己解决了:

至少一种方法是输入一个额外的“人工”x-axis勾号:

scale_x_continuous(breaks=c(1,2,3,4,5,6,7,8), labels=c("Before", "During", "After","Follow", "Before", "During", "After", "Follow"), limits=c(0, 9)) +
## Install packages
#library("plyr")
#library("lattice")
library("ggplot2")
library("dplyr")
#library("readr")
#library("rmarkdown")
#library("Rmisc")
#library("devtools")
library("gghalves")
# width and height variables for saved plots
w = 6
h = 4
# Define limits of y-axis
y_lim_min = 4
y_lim_max = 7.5

before = iris$Sepal.Width[1:50]
during = iris$Sepal.Length[51:100]
after = iris$Sepal.Length[1:50]
follow = iris$Sepal.Length[51:100]
n <- length(before) 
d <- data.frame(y = c(before, during, after, follow),
                x = rep(c(1,2,3,4), each=n),
                z = rep(c(5,6,7,8), each=n),
                id = factor(rep(1:n,4)))
set.seed(321)
d$xj <- jitter(d$x, amount = .09) 
d$xj_2 <- jitter(d$z, amount = .09)
#d$xj_3 <- jitter(d$a, amount = .09)

par(mar=c(7,7,6,2.1))
ggplot(data=d, aes(y=y)) +
  
  #Add geom_() objects
  geom_point(data = d %>% filter(x=="1"), aes(x=xj), color = 'dodgerblue', size = 1.5,
             alpha = .6) +
  geom_point(data = d %>% filter(x=="2"), aes(x=xj), color = 'darkgreen', size = 1.5,
             alpha = .6) +
  geom_point(data = d %>% filter(x=="3"), aes(x=xj), color = 'darkorange', size = 1.5, 
             alpha = .6) +
  geom_point(data = d %>% filter(x=="4"), aes(x=xj), color = 'red', size = 1.5, 
             alpha = .6) +
  
  #Add geom_() objects
  geom_point(data = d %>% filter(z=="5"), aes(x=xj_2), color = 'green', size = 1.5,
             alpha = .6) +
  geom_point(data = d %>% filter(z=="6"), aes(x=xj_2), color = 'blue', size = 1.5,
             alpha = .6) +
  geom_point(data = d %>% filter(z=="7"), aes(x=xj_2), color = 'orange', size = 1.5, 
             alpha = .6) +
  geom_point(data = d %>% filter(z=="8"), aes(x=xj_2), color = 'red', size = 1.5, 
             alpha = .6) +
  
  geom_line(aes(x=xj, group=id), color = 'lightgray', alpha = .3) +
  geom_line(aes(x=xj_2, group=id), color = 'lightgray', alpha = .3) +
  
  geom_half_violin(
    data = d %>% filter(x=="1"),aes(x = x, y = y), position = position_nudge(x = -0.2), 
    side = "l", fill = 'dodgerblue', alpha = .5, color = "dodgerblue", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(x=="2"),aes(x = x, y = y), position = position_nudge(x = -1.2), 
    side = "l", fill = "darkgreen", alpha = .5, color = "darkgreen", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(x=="3"),aes(x = x, y = y), position = position_nudge(x = -2.2), 
    side = "l", fill = "darkorange", alpha = .5, color = "darkorange", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(x=="4"),aes(x = x, y = y), position = position_nudge(x = -3.2), 
    side = "l", fill = 'red', alpha = .5, color = "red", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(z=="5"),aes(x = x, y = y), position = position_nudge(x = 7.5), 
    side = "r", fill = "green", alpha = .5, color = "green", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(z=="6"),aes(x = x, y = y), position = position_nudge(x = 6.5), 
    side = "r", fill = "blue", alpha = .5, color = "blue", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(z=="7"),aes(x = x, y = y), position = position_nudge(x = 5.5), 
    side = "r", fill = "orange", alpha = .5, color = "orange", trim = TRUE) +
  
  geom_half_violin(
    data = d %>% filter(z=="8"),aes(x = x, y = y), position = position_nudge(x = 4.5), 
    side = "r", fill = "red", alpha = .5, color = "red", trim = TRUE) +
  
  #Define additional settings
  scale_x_continuous(breaks=c(1,2,3,4,5,6,7,8), labels=c("Before", "During", "After","Follow", "Before", "During", "After", "Follow"), limits=c(0, 9)) +
  xlab("Condition") + ylab("Value") +
  ggtitle('Figure 13: Repeated measures with jittered datapoints and connections') +
  theme_classic()+
  coord_cartesian(ylim=c(2, 7.5))