R 删除ggplot刻面条标签周围的三个边框
我有以下图表: 我想做一个非常简单的更改:我想删除左刻面标签边界线的顶部、右侧和底部 我该如何删除这些线,或绘制与右手线等效的线?如果可能的话,我宁愿不去胡闹,但也不会拒绝任何可行的解决方案 图形代码:R 删除ggplot刻面条标签周围的三个边框,r,ggplot2,facet-grid,R,Ggplot2,Facet Grid,我有以下图表: 我想做一个非常简单的更改:我想删除左刻面标签边界线的顶部、右侧和底部 我该如何删除这些线,或绘制与右手线等效的线?如果可能的话,我宁愿不去胡闹,但也不会拒绝任何可行的解决方案 图形代码: library(ggplot2) library(dplyr) library(forcats) posthoc1 %>% mutate(ordering = -as.numeric(Dataset) + Test.stat, Species2 = fct_re
library(ggplot2)
library(dplyr)
library(forcats)
posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
strip.background.x=element_rect(color = NA, fill=NA),
strip.background.y=element_rect(color = "black", fill=NA)) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
数据:
该解决方案基于grob:找到“strip-l”(左strip)的位置,然后用line grob替换rect grob
p <- posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
#strip.background.x=element_rect(color = "white", fill=NULL),
strip.background.y=element_rect(color = NA)
) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
library(grid)
q <- ggplotGrob(p)
lg <- linesGrob(x=unit(c(0,0),"npc"), y=unit(c(0,1),"npc"),
gp=gpar(col="red", lwd=4))
for (k in grep("strip-l",q$layout$name)) {
q$grobs[[k]]$grobs[[1]]$children[[1]] <- lg
}
grid.draw(q)
p%
mutate(ordering=-as.numeric(Dataset)+Test.stat,
规格2=fct\U重新排序(规格2,排序,.desc=F))%>%
ggplot(aes(x=Coef,y=Species2,重新排序(Coef,分类群),group=Species2,Color=Taxa))+
geom_点(大小=posthoc1$Test.stat*.25,show.legend=FALSE)+
ylab(“”+
主题经典(基本尺寸=20)+
刻面网格(分类群~数据集,scales=“free\u y”,space=“free\u y”,switch=“y”)+
几何线(xintercept=0)+
主题(axis.text.x=element_text(color=“black”),
strip.placement=“外部”,
#strip.background.x=element_rect(color=“white”,fill=NULL),
strip.background.y=element_rect(color=NA)
) +
坐标笛卡尔(clip=“off”)+
比例x连续(限值=NULL)
图书馆(网格)
q我假设您知道(因为您在代码中使用了它),刻面标签边框由element\u rect()
控制,它同时控制所有四个面。如果你只想把它放在一边,那就可以对它进行黑客攻击,尽管我不确定这是否真的能改善这张图表的外观……是的,我知道。这就是为什么我觉得很难。这张图可能不是最好的例子,但是在右边有一条横线可以让我们更清楚地知道每一组物种属于哪一组。太好了,谢谢。实际上,我想在分类群名称下添加这些行,所以我将代码改为x=unit(c(1,1),“npc”),y=unit(c(1,0),“npc”),Marco Sandri的解决方案非常有效,是一种澄清图表的好方法。在切换轴等时,我仍然很难完全理解x和y输入,但一些尝试和错误通常会有所帮助。只是想知道,在此基础上,是否可以省略一个或两个标签+行,例如,假设我想显示所有标签,但等足动物+等足动物标签的行。。。?有什么想法吗?
p <- posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
#strip.background.x=element_rect(color = "white", fill=NULL),
strip.background.y=element_rect(color = NA)
) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
library(grid)
q <- ggplotGrob(p)
lg <- linesGrob(x=unit(c(0,0),"npc"), y=unit(c(0,1),"npc"),
gp=gpar(col="red", lwd=4))
for (k in grep("strip-l",q$layout$name)) {
q$grobs[[k]]$grobs[[1]]$children[[1]] <- lg
}
grid.draw(q)