去掉y标签后,子图错位了

我有两个类似的barchart plotet与 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)

enter image description here

解决方案:

如果你的数据集共享一个类似的结构,就像你的例子一样,一个可能的解决方案是将它们绑定在一起,如下所示。

data <-  data.frame(target,algorithm,value)
data2 <- data.frame(target,algorithm,value)
data$dataset <- "Dataset1"
data2$dataset <- "Dataset2"

DF <- rbind(data, data2)

            target algorithm    value  dataset
1        Some Data      0_DT 87.33034 Dataset1
2  Some Other Data      0_DT 89.65962 Dataset1
3        Some Data      1_RF 87.65973 Dataset1
4  Some Other Data      1_RF 93.57828 Dataset1
5        Some Data     2_MLP 85.45831 Dataset1
6  Some Other Data     2_MLP 89.42200 Dataset1
7        Some Data      0_DT 87.33034 Dataset2
8  Some Other Data      0_DT 89.65962 Dataset2
9        Some Data      1_RF 87.65973 Dataset2
10 Some Other Data      1_RF 93.57828 Dataset2
11       Some Data     2_MLP 85.45831 Dataset2
12 Some Other Data     2_MLP 89.42200 Dataset2

然后不使用 grid.arrange,您可以简单地使用 facet_wrap 以显示两个图形的同一y轴。

labels = c(Dataset1 = "1",Dataset2 ="2")

library(ggplot2)
ggplot(DF, aes(x = target, y = value, fill = algorithm))+
  geom_col(position = position_dodge2())+
  facet_wrap(~dataset, scales = "free", labeller = labeller(dataset = labels))+ 
  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),
                     breaks= seq(0,100,10),
                     expand = c(0,0)) +
  scale_x_discrete(expand=c(0.3,0.1))+
  theme_classic()+
  theme(aspect.ratio =10/6,
        strip.background = element_blank(),
        strip.text.x = element_text(size = 15, hjust = 0, face = "bold"))

enter image description here


编辑:改变两个面中的一个面的X顺序。

根据你的评论,你希望只对两个数据集中的一个进行顺序操作。要做到这一点,一个可能的解决方案是创建4个x值,设置正确的顺序,显示它,并使用参数修改它们的标签。labelsscale_x_discrete.

首先,生成新的x值,都是差值。在这里,我给第二个数据集的x值添加了一个后缀,方法是:。

library(dplyr)
library(ggplot2)

DF %>% rowwise() %>% mutate(Suffix = ifelse(dataset == "Dataset2",".22","")) %>%
  mutate(Target = paste(target, Suffix, sep = "")) %>%
  mutate(Target = factor(Target, 
                         levels = c("Some Data","Some Other Data","Some Other Data.22","Some Data.22")))

Source: local data frame [12 x 6]
Groups: <by row>

# A tibble: 12 x 6
   target          algorithm value dataset  Suffix Target            
   <fct>           <fct>     <dbl> <chr>    <chr>  <fct>             
 1 Some Data       0_DT       87.3 Dataset1 ""     Some Data         
 2 Some Other Data 0_DT       89.7 Dataset1 ""     Some Other Data   
 3 Some Data       1_RF       87.7 Dataset1 ""     Some Data         
 4 Some Other Data 1_RF       93.6 Dataset1 ""     Some Other Data   
 5 Some Data       2_MLP      85.5 Dataset1 ""     Some Data         
 6 Some Other Data 2_MLP      89.4 Dataset1 ""     Some Other Data   
 7 Some Data       0_DT       87.3 Dataset2 ".22"  Some Data.22      
 8 Some Other Data 0_DT       89.7 Dataset2 ".22"  Some Other Data.22
 9 Some Data       1_RF       87.7 Dataset2 ".22"  Some Data.22      
10 Some Other Data 1_RF       93.6 Dataset2 ".22"  Some Other Data.22
11 Some Data       2_MLP      85.5 Dataset2 ".22"  Some Data.22      
12 Some Other Data 2_MLP      89.4 Dataset2 ".22"  Some Other Data.22

现在,你可以把这个新的变量作为x值传递到 ggplot2 并在标签中使用以下方法去除后缀 labels 的论点 scale_x_discrete 诸如:

library(dplyr)
library(ggplot2)

DF %>% rowwise() %>% mutate(Suffix = ifelse(dataset == "Dataset2",".22","")) %>%
  mutate(Target = paste(target, Suffix, sep = "")) %>%
  mutate(Target = factor(Target, 
                         levels = c("Some Data","Some Other Data","Some Other Data.22","Some Data.22"))) %>%
  ggplot(aes(x = Target, y = value, fill = algorithm))+
  geom_col(position = position_dodge2())+
  facet_wrap(~dataset, scales = "free", labeller = labeller(dataset = labels))+ 
  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),
                     breaks= seq(0,100,10),
                     expand = c(0,0)) +
  scale_x_discrete(expand=c(0.3,0.1), labels = function(x) sub("\\..*$","",x))+
  theme_classic()+
  theme(aspect.ratio =10/6,
        strip.background = element_blank(),
        strip.text.x = element_text(size = 15, hjust = 0, face = "bold"))

enter image description here

它能回答你的问题吗?

给TA打赏
共{{data.count}}人
人已打赏
解决方案

Facebook按钮我正在使用firebaseUI多重登录,除了Facebook Builder().build()外,所有按钮都在生成,如何使该按钮膨胀?

2022-4-22 17:09:10

解决方案

Redshift。用一个共同的列来查询所有的表格

2022-4-22 17:09:12

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索