为什么ggplot2无法使用facet_grid分割我的绘图?

为什么ggplot2无法使用facet_grid分割我的绘图?,r,ggplot2,R,Ggplot2,这是我的数据和代码: a <- data.frame(Original = c("Full", "Full", "T0", "T0"), Classified = c("Full", "T0", "Full", "T0"), Value = c(0.69, 0.31, 0.32, 0.68), Text = c("69%", "31%", "32%", "68%")) ggplot(data = a, aes(x=

这是我的数据和代码:

a <- data.frame(Original = c("Full", "Full", "T0", "T0"),
            Classified = c("Full", "T0", "Full", "T0"),
            Value = c(0.69, 0.31, 0.32, 0.68),
            Text = c("69%", "31%", "32%", "68%"))


ggplot(data = a, aes(x=Classified, y=Original, fill=Value)) + 
  geom_tile()+
  geom_text (aes (label=Text), size = 15)+
  scale_fill_gradient(low="light blue", high="purple")+
  facet_grid (Original~Classified, space = "free", switch = "y")+
  theme_bw()+
  ggtitle ("ADS")+
  theme (panel.background = element_blank())+
  theme (plot.background = element_blank())+
  theme (panel.grid.major = element_line(colour = "white"))+
  ylab ("Percentage of Tokens")+
  theme (strip.text = element_text(size = 23))+
  theme (axis.title = element_text (size = 25))+
  theme(plot.title = element_text(size=25))+
  theme (legend.position = "none")+
  theme (axis.title.x = element_blank())

a问题源于轴和面上的
已分类
原始
。有两种方法可以解决这个问题

  • 瓦尔特·比科维奇(Valter Biković)的评论中提出了解决这一问题的一种方法 和sandipan,这就是将
    scales=“free”
    添加到
    facet\u网格
    。轴最终是多余的,因为它们只是 提供与刻面标签相同的信息,因此我们可以删除 通过添加更多的
    主题
    调用来增强它们

  • 另一种选择是调用
    分类
    原始

    作为
    x
    y
    ,而是调用一个常量(例如
    “1”
    )。由于轴现在将毫无意义,我们不妨
    再次移除它们

这为第一个选项提供了以下代码:

ggplot(data = a, aes(x=Classified, y=Original, fill=Value)) + 
  geom_tile()+
  geom_text (aes (label=Text), size = 15)+
  scale_fill_gradient(low="light blue", high="purple")+
  facet_grid (Original~Classified, space = "free", switch = "y", scales = "free")+
  theme_bw()+
  ggtitle ("ADS")+
  theme (panel.background = element_blank())+
  theme (plot.background = element_blank())+
  theme (panel.grid.major = element_line(colour = "white"))+
  ylab ("Percentage of Tokens")+
  theme (strip.text = element_text(size = 23))+
  theme (axis.title = element_text (size = 25))+
  theme(plot.title = element_text(size=25))+
  theme (legend.position = "none")+
  theme (axis.title.x = element_blank()) +
  theme (axis.text = element_blank()) +
  theme (axis.ticks = element_blank())
以及第二个选项的以下代码:

ggplot(data = a, aes(x="1", y="1", fill=Value)) + 
  geom_tile()+
  geom_text (aes (label=Text), size = 15)+
  scale_fill_gradient(low="light blue", high="purple")+
  facet_grid (Original~Classified, space = "free", switch = "y")+
  theme_bw()+
  ggtitle ("ADS")+
  theme (panel.background = element_blank())+
  theme (plot.background = element_blank())+
  theme (panel.grid.major = element_line(colour = "white"))+
  ylab ("Percentage of Tokens")+
  theme (strip.text = element_text(size = 23))+
  theme (axis.title = element_text (size = 25))+
  theme(plot.title = element_text(size=25))+
  theme (legend.position = "none")+
  theme (axis.title.x = element_blank()) +
  theme (axis.text = element_blank()) +
  theme (axis.ticks = element_blank())
两种方法都会产生相同的绘图:


试着用它代替代码
刻面网格(原始~Classified,space=“free”,switch=“y”,scales=“free”)
。刻面网格在所有绘图上使用相同的比例,这就是您获得显示的绘图的原因。确切地说,您需要将scales='free'添加到刻面网格。