繁体   English   中英

如何在ggplot2中使用position_dodge和2个级别的分组

[英]How to use position_dodge with 2 levels of groupings in ggplot2

我想使用 ggplot 中的 group = interaction 按 2 个因素对一系列线条进行分组。 这是一些示例代码:

set.seed(123)
N <- 18
means <- rnorm(N,0,1)
ses <- rexp(N,2)
upper<- means+qnorm(0.975)*ses
lower<- means+qnorm(0.025)*ses
fruit <- rep(c("Apples","Bananas","Pears"), each=6)
size <- rep(rep(c("Small","Medium","Big"), each=2),3)
GMO <- rep(c("Yes","No"), 9)
d<- data.frame(means,upper,lower,fruit,size,GMO)


ggplot(data=d,
       aes(x = fruit,y = means, ymin = lower, ymax = upper, col=size,linetype=GMO,group=interaction(GMO, size)))+
  geom_hline(aes(fill=size),yintercept =1, linetype=2)+
  xlab('labels')+ ylab("Parameter estimates (95% Confidence Interval)")+
  geom_pointrange(position=position_dodge(width = 0.6)) +
  scale_x_discrete(name="Fruits")+
  coord_flip()-> fplot
dev.new()
fplot

这是结果图的链接: https ://i.stack.imgur.com/5YF4F.png

我想将三个组中的每一个组的相同颜色的线条靠得更近。 换句话说,我希望这些线条不仅可以通过“水果”变量进行聚类,还可以通过每个水果的“大小”变量进行聚类。 poisition_dodge 似乎只适用于其中一个交互组。

谢谢你的建议。

也许你想facet_wrap你的 size 变量:

set.seed(123)
N <- 18
means <- rnorm(N,0,1)
ses <- rexp(N,2)
upper<- means+qnorm(0.975)*ses
lower<- means+qnorm(0.025)*ses
fruit <- rep(c("Apples","Bananas","Pears"), each=6)
size <- rep(rep(c("Small","Medium","Big"), each=2),3)
GMO <- rep(c("Yes","No"), 9)
d<- data.frame(means,upper,lower,fruit,size,GMO)

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.2
ggplot(data=d,
       aes(x = fruit,y = means, ymin = lower, ymax = upper, col=size,linetype=GMO,group=interaction(GMO, size)))+
  geom_hline(aes(fill=size),yintercept =1, linetype=2)+
  xlab('labels')+ ylab("Parameter estimates (95% Confidence Interval)")+
  geom_pointrange(position=position_dodge(width = 0.6)) +
  scale_x_discrete(name="Fruits")+
  coord_flip() +
  facet_wrap(~size)-> fplot
#> Warning: geom_hline(): Ignoring `mapping` because `yintercept` was provided.
fplot

reprex 包(v2.0.1) 于 2022-07-13 创建

据我所知, position_dodge是不可能的,即它会根据group aes 的类别进行闪避。 无论您是在组 aes 上映射一个变量还是两个或更多变量的交互,都无关紧要。 这些组只是彼此等距放置。

实现所需结果的一种选择是使用“看起来不像刻面的刻面”技巧,这意味着按fruit刻面,在x上映射size ,然后使用theme选项摆脱刻面外观以及一些调整x比例:

set.seed(123)
N <- 18
means <- rnorm(N, 0, 1)
ses <- rexp(N, 2)
upper <- means + qnorm(0.975) * ses
lower <- means + qnorm(0.025) * ses
fruit <- rep(c("Apples", "Bananas", "Pears"), each = 6)
size <- rep(rep(c("Small", "Medium", "Big"), each = 2), 3)
GMO <- rep(c("Yes", "No"), 9)
d <- data.frame(means, upper, lower, fruit, size, GMO)


library(ggplot2)

ggplot(data = d, aes(x = size, y = means, ymin = lower, ymax = upper, col = size, linetype = GMO, group = GMO)) +
  geom_hline(yintercept = 1, linetype = 2) +
  xlab("labels") +
  ylab("Parameter estimates (95% Confidence Interval)") +
  geom_pointrange(position = position_dodge(width = 0.6)) +
  scale_x_discrete(name = "Fruits", breaks = "Medium", labels = NULL, expand = c(0, 1)) +
  coord_flip() +
  facet_grid(fruit ~ ., switch = "y") +
  theme(strip.placement = "outside", 
        strip.background.y = element_blank(),
        strip.text.y.left = element_text(angle = 0),
        panel.spacing.y = unit(0, "pt"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM