【发布时间】:2020-08-05 21:21:45
【问题描述】:
我有两个相似的条形图,grid.arrange 在两个列中,并排。
由于它们共享 y 轴的公共标签,因此我想删除右侧子图 y 轴上的标签。
我用以下方法做到这一点:
myplot2 <- arrangeGrob(q+
theme(axis.title.y = element_blank()), #....
这会导致两个子图的扭曲/错位。在我删除 y 标签之前,两个子图的比例完全相同,并且 x 轴处于同一水平。
问题
如何在不改变两个子图位置的情况下删除右侧图的 y 轴标签。
MRE
library(ggplot2)
library(ggthemes)
library(dplyr)
algorithm <- c(rep("0_DT",2),rep("1_RF",2),rep("2_MLP",2))
target <- rep(c("Some Data","Some Other Data"),3)
value <- runif(6,85,95) # Simulated Accuracies
data <- data.frame(target,algorithm,value)
p <- ggplot(data, aes(fill=algorithm, y=value, x=target)) + theme_classic()+
geom_bar(position=position_dodge(0.75), stat="identity", width = 0.65,colour="black",size=0.1) +
scale_fill_manual("Algorithm",
values = alpha(c("0_DT" = "#20639B", "1_RF" = "#3CAEA3", "2_MLP" = "#F6D55C"),0.8),
labels=c("DT","RF","MLP"))+
scale_y_continuous("Accuracy in %",limits = c(0,100),oob = rescale_none,
# breaks= sort(c(seq(0,90,10),h)),
breaks= seq(0,100,10),
expand = c(0,0)) +
scale_x_discrete(expand=c(0.3,0.1))+
theme(aspect.ratio =10/6)
# duplicate the plot for MRE
q <- p
myplot1 <- arrangeGrob(p,
top = textGrob("1", x = unit(0.05, "npc")
, y = unit(-0.5, "npc"), just=c("left","top"),
gp=gpar(col="black", fontsize=18, fontfamily="Arial",fontface="bold")))
myplot2 <- arrangeGrob(q+
theme(axis.title.y =element_blank()),
top = textGrob("2", x = unit(0.05, "npc")
, y = unit(-0.5, "npc"), just=c("left","top"),
gp=gpar(col="black", fontsize=18, fontfamily="Arial",fontface="bold")))
grid.arrange(myplot1,myplot2,ncol=2)
【问题讨论】:
-
您能否验证您的代码,在尝试运行它时,我在生成数据帧(CI.Lower 不存在)、图形部分(oob = rescale_none)和使用
arrange.grob。另外,合并您的数据框并使用 facet_wrap 显示两个图形怎么样? -
抱歉刚刚剪掉了 CI 以使其更短,错过了再次验证它。我编辑了 mre 以正常工作。我不太确定
ob = rescale_non似乎有什么问题,但评论它似乎并没有改变行为
标签: r ggplot2 plot bar-chart subplot