R 是否可以使用geom_文本将文本放置在每个镶嵌面板下方,并将其与记号对齐?

R 是否可以使用geom_文本将文本放置在每个镶嵌面板下方,并将其与记号对齐?,r,ggplot2,facet-wrap,R,Ggplot2,Facet Wrap,我已经在bellow的facet_wrap图上添加了文本行(ind=305,ind=182…)。我正在寻找一种方法,使这些文本线正好位于每个刻面面板中的刻度线下方。我搜索并尝试了许多选项,其中包括短语“将文本置于绘图区域之外”,但我无法解决我的问题 这是我用来生成绘图的代码。有可行的选择吗 ggplot(df1, aes(Sex, value)) + geom_boxplot(aes(fill = Sex), alpha = 0.2) + theme_bw() +

我已经在bellow的facet_wrap图上添加了文本行(ind=305,ind=182…)。我正在寻找一种方法,使这些文本线正好位于每个刻面面板中的刻度线下方。我搜索并尝试了许多选项,其中包括短语“将文本置于绘图区域之外”,但我无法解决我的问题

这是我用来生成绘图的代码。有可行的选择吗

ggplot(df1, aes(Sex, value)) +    
  geom_boxplot(aes(fill = Sex), alpha = 0.2) + 
  theme_bw() +    
  facet_wrap(.~trait.code, nrow = 2, scales = "free") +    
  geom_text(data = df2[df2$Sex == "Female",], aes(label = ind), size = 4, fontface="bold") +    
  geom_text(data = df2[df2$Sex == "Male",], aes(label = ind), size = 4, fontface="bold") +  
  theme(axis.title.x= element_blank(), 
        axis.title.y=element_blank(),
        legend.position="bottom", 
        axis.text.x = element_blank(), 
        panel.spacing.y = unit(2, "lines")) + 
  ggtitle("Any tittle")
下面是数据的一个子集


df1这里的诀窍是使用
ind
变量作为x轴。为此,我建议将连接与元数据
df2
和包含观察结果的原始
df1
一起使用。为了加入他们,我“组合”了
trait.code
Sex
并使用了
left\u-join

#-- Add 'trait.comb' for join
df2 <- df2 %>%
    mutate(ind = str_remove(ind, "\n "),
           trait.comb = paste(trait.code, Sex, sep = "~")) %>%
    select(trait.comb, ind

#-- Add the information in df2 to df1 by joining using trait.comb
df1 <- df1 %>%
    mutate(trait.comb = paste(trait.code, Sex, sep = "~")) %>%
    left_join(df2, by = "trait.comb") %>%
    select(-trait.comb)

#-- Plot using the ind as your x-axis aesthetic
ggplot(df1, aes(ind, value)) +    
    facet_wrap(.~trait.code, nrow = 2, scales = "free") +    
    geom_boxplot(aes(fill = Sex), alpha = 0.2) + 
    theme_bw() +    
    theme(axis.title.y=element_blank(),
          legend.position="bottom", 
          panel.spacing.y = unit(2, "lines")) + 
    ggtitle("Any title")
#--为join添加'trait.comb'
df2%
突变(ind=str_remove(ind,“\n”),
trait.comb=粘贴(trait.code,sep=“~”)%>%
选择(trait.comb,ind
#--通过使用trait.comb连接,将df2中的信息添加到df1中
df1%
突变(trait.comb=paste(trait.code,sep=“~”)%%
左联合(df2,by=“trait.comb”)%>%
选择(-trait.comb)
#--使用ind作为x轴进行打印
ggplot(df1,aes(ind,value))+
刻面(trait.code,nrow=2,scales=“free”)+
geom_箱线图(aes(填充=性别),α=0.2)+
主题_bw()+
主题(axis.title.y=element_blank(),
legend.position=“bottom”,
面板间距y=单位(2,“线”)+
名称(“任何名称”)

这里的诀窍是使用
ind
变量作为你的x轴美学。为此,我建议使用元数据
df2
和原始
df1
,其中包含观察结果。为了加入它们,我“组合”了
特征.code
性别
,并使用
左键

#-- Add 'trait.comb' for join
df2 <- df2 %>%
    mutate(ind = str_remove(ind, "\n "),
           trait.comb = paste(trait.code, Sex, sep = "~")) %>%
    select(trait.comb, ind

#-- Add the information in df2 to df1 by joining using trait.comb
df1 <- df1 %>%
    mutate(trait.comb = paste(trait.code, Sex, sep = "~")) %>%
    left_join(df2, by = "trait.comb") %>%
    select(-trait.comb)

#-- Plot using the ind as your x-axis aesthetic
ggplot(df1, aes(ind, value)) +    
    facet_wrap(.~trait.code, nrow = 2, scales = "free") +    
    geom_boxplot(aes(fill = Sex), alpha = 0.2) + 
    theme_bw() +    
    theme(axis.title.y=element_blank(),
          legend.position="bottom", 
          panel.spacing.y = unit(2, "lines")) + 
    ggtitle("Any title")
#--为join添加'trait.comb'
df2%
突变(ind=str_remove(ind,“\n”),
trait.comb=粘贴(trait.code,sep=“~”)%>%
选择(trait.comb,ind
#--通过使用trait.comb连接,将df2中的信息添加到df1中
df1%
突变(trait.comb=paste(trait.code,sep=“~”)%%
左联合(df2,by=“trait.comb”)%>%
选择(-trait.comb)
#--使用ind作为x轴进行打印
ggplot(df1,aes(ind,value))+
刻面(trait.code,nrow=2,scales=“free”)+
geom_箱线图(aes(填充=性别),α=0.2)+
主题_bw()+
主题(axis.title.y=element_blank(),
legend.position=“bottom”,
面板间距y=单位(2,“线”)+
名称(“任何名称”)

回答原始问题:
geom_text()
如果使用
coord_cartesian(clip=“off”)
关闭剪裁,则可以在绘图面板外绘制

ggplot(df1,aes(性别,价值))+
geom_箱线图(aes(填充=性别),α=0.2)+
主题_bw()+
刻面(trait.code,nrow=2,scales=“free”)+
geom_文本(
数据=df2[df2$Sex==“女性”,],
aes(label=ind,y=-Inf),size=4,vjust=1.2,fontface=“bold”
) +    
geom_文本(
数据=df2[df2$Sex==“男性”,],
aes(label=ind,y=-Inf),size=4,vjust=1.2,fontface=“bold”
) +
坐标笛卡尔(clip=“off”)+
主题(axis.title.x=element_blank(),
axis.title.y=元素_blank(),
legend.position=“bottom”,
legend.box.spacing=单位(2,“线”),
axis.text.x=元素_blank(),
面板间距y=单位(2.5,“线”)+
标题(“任何标题”)


由(v0.3.0)于2019-11-16创建,以回答原始问题:
geom_text()
如果使用
coord_cartesian(clip=“off”)关闭剪裁,则可以在绘图面板外绘制

ggplot(df1,aes(性别,价值))+
geom_箱线图(aes(填充=性别),α=0.2)+
主题_bw()+
刻面(trait.code,nrow=2,scales=“free”)+
geom_文本(
数据=df2[df2$Sex==“女性”,],
aes(label=ind,y=-Inf),size=4,vjust=1.2,fontface=“bold”
) +    
geom_文本(
数据=df2[df2$Sex==“男性”,],
aes(label=ind,y=-Inf),size=4,vjust=1.2,fontface=“bold”
) +
坐标笛卡尔(clip=“off”)+
主题(axis.title.x=element_blank(),
axis.title.y=元素_blank(),
legend.position=“bottom”,
legend.box.spacing=单位(2,“线”),
axis.text.x=元素_blank(),
面板间距y=单位(2.5,“线”)+
标题(“任何标题”)


由(v0.3.0)于2019-11-16创建,而不是使用
geom_text
,您应该考虑修改x轴的标签。@csgroen关于如何替换原始标签(阴标签和阳标签)的任何建议通过使用geom_text而不是使用
geom_text
添加的文本行,您应该考虑修改x轴的标签。@csgroen如何用使用geom_text添加的文本行替换原始标签(女性和男性)的任何建议