You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggplot2的facet_wrap中将第二列strip label移至右侧?

问题需求

我希望将分面图(facet graph)第二列的strip label移至右侧,如图中标注所示。以下是我当前使用的代码及示例数据集:

当前代码

library(reshape2)
library(tidyverse)
library(ggh4x)
library(scales)
library(ggplot2)
#step 1: 处理数据
df <- melt(df, id=c("Specie","Treatment"))
head(df)
#绘图部分
left  <- scale_y_continuous(labels = number_format(accuracy = 0.01))
right <- scale_y_continuous(labels = number_format(accuracy = 0.01),position = "right")

ggplot(df1, aes(x = Specie, y = mean, fill = Treatment, colour=Treatment)) +
  geom_col(position = position_dodge(), width = 0.8)+
  facet_wrap(~ variable, scales = "free_y", ncol = 2, 
             labeller = label_parsed, strip.position = "left")+
  geom_errorbar(aes(ymin = mean, ymax = mean + sd), width = 0.2,
                position = position_dodge(0.9)) +theme_bw()+
  geom_text(aes(y = mean+sd,label = label),position = position_dodge(0.9),
            size = 3.5, vjust=-0.2, family="serif", colour="black")+
  geom_text(aes(y = Q1/2,label = letter),size = 3.5, family="serif", colour="#000000")+
  scale_fill_manual(values=c('#DEB887','#CD853F'))+
  scale_color_manual(values=c('#DEB887','#CD853F'))+
  facetted_pos_scales(y = list(left, right, left, right, left, right))+
  #coord_flip()+
  theme(axis.text.x = element_text(face = "italic"),
        strip.placement = "outside",
        strip.background = element_blank())

示例数据集

TreatmentSpeciePnGsEWUE
CTS. commersonii4.81000.07361.11004.3333
CTS. commersonii11.50000.46605.44002.1140
CTS. tuberosum 2x16.40000.49305.52002.9710
CTS. tuberosum 2x4.24000.08621.81002.3425
CTS. chacoense3.92000.04260.71605.4749
CTS. chacoense7.90000.15001.94004.0722
HSS. commersonii10.10000.35206.03001.6750
HSS. commersonii6.85000.37005.46001.2546
HSS. tuberosum 2x4.71000.02050.84005.6071
HSS. tuberosum 2x4.34000.03141.24503.4859
HSS. chacoense13.80000.24506.48002.1296
HSS. chacoense14.40000.17005.51002.6134

内容的提问来源于stack exchange,提问作者Ikram Bashir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 10:10:58