ggplot 中的 Geom_tile- x 轴上的缩放问题

问题描述 投票:0回答:2

我使用 geom_tile 的混淆矩阵的结果图在我的顶部比例轴上关闭。

这是一个模拟代码

library(ggplot2)
library(dplyr)
library(stringr)  

pred<- c(1,1,1,2,2,4,3,4,4,3,3,3)
truth<- c(1,1,1,2,2,2,3,3,3,4,4,4)

predClass<- pred
predClass[pred==1] = "Hi my friend"
predClass[pred==2] = "Hello my friend"
predClass[pred==3] = "Hey my friend"
predClass[pred==4] = "Hola my friend"

truthClass<- truth
truthClass[truth==1] = "Hi my friend"
truthClass[truth==2] = "Hello my friend"
truthClass[truth==3] = "Hey my friend"
truthClass[truth==4] = "Hola my friend"

truthClass <- as.factor(truthClass)
predClass <- as.factor(predClass)

tab <- table(truthClass, predClass)
tab <- tab / rowSums(tab)
tab <- as.data.frame(tab, stringsAsFactors = TRUE)
tab$predClass <- factor(tab$predClass, rev(levels(tab$predClass)))

ggplot(tab, aes(predClass, truthClass, fill = Freq)) +
  geom_tile(colour = "#EBECEB", height=3, width=3) +
  geom_text(aes(label = ifelse(Freq == 0, "", scales::percent(Freq))),vjust = .5, 
            alpha = 1, size = 7) +
  scale_fill_gradient(low = "white", high = "red") +
  scale_x_discrete(labels = function(x) 
    stringr::str_wrap(x, width = 9), limits=truthClass,
    position = "top")+
  scale_y_discrete(labels = function(x) 
    stringr::str_wrap(x, width = 9),limits=rev(truthClass))+
  labs(x = "True Class", y = "Prediction",
       fill = "Frequency") +
  theme_classic()+
  theme(legend.title = element_text(colour = "black",
                                    size = 20, margin = margin(0, 20, 10, 0)),
        legend.text = element_text(size = 17),
        axis.title.x = element_text(colour = "black", size = 20,
                                    margin = margin(20, 20, 20, 20)),
        axis.title.y = element_text(colour = "black", size = 20,
                                    margin = margin(0, 20, 0, 10)),
        axis.text.x = element_text(colour = "black",angle = 30, vjust = 0.6,
                                   size = 15),
        axis.text.y = element_text(colour = "black", size = 15),
        axis.line = element_line(colour = "#EBECEB", 
                                 size = 1, linetype = "solid"),
        axis.ticks.length=unit(.25, "cm"))

结果图如下所示:

我的 x 标签和我的顶部瓷砖之间有间隙。关于如何消除这个差距的任何想法?

r ggplot2 geom-tile
2个回答
0
投票

这是你想要的输出吗?

似乎

limits = rev(truthClass)
部分与轴填充意外地相互作用,使得在顶部产生了一些额外的填充。使用一些负填充将其删除。

scale_y_discrete(labels = function(x) 
    stringr::str_wrap(x, width = 9),limits=rev(truthClass), expand = c(-0.1,0)) +


0
投票

您的代码有两个问题。首先,您将

width
height
设置为
3
geom_tile
中,我没有看到任何原因,实际上这没有多大意义,因为这样做时瓷砖会重叠(要看到这个添加例如
alpha=.5
geom_tile
)。其次,您将带有重复条目的向量
truthClass
传递给
limits
参数。解决这两个问题后,您可以通过
expand=c(0, 0)
:

删除默认的比例扩展来消除差距
limits <- c("Hi my friend", "Hello my friend", "Hey my friend", "Hola my friend")

ggplot(tab, aes(predClass, truthClass, fill = Freq)) +
  geom_tile(colour = "#EBECEB") +
  geom_text(aes(label = ifelse(Freq == 0, "", scales::percent(Freq))),
    vjust = .5,
    alpha = 1, size = 7
  ) +
  scale_fill_gradient(low = "white", high = "red") +
  scale_x_discrete(
    labels = function(x) {
      stringr::str_wrap(x, width = 9)
    }, limits = limits,
    position = "top", expand = c(0, 0)
  ) +
  scale_y_discrete(labels = function(x) {
    stringr::str_wrap(x, width = 9)
  }, limits = rev(limits), expand = c(0, 0)) +
  labs(
    x = "True Class", y = "Prediction",
    fill = "Frequency"
  ) +
  theme_classic() +
  theme(
    legend.title = element_text(
      colour = "black",
      size = 20, margin = margin(0, 20, 10, 0)
    ),
    legend.text = element_text(size = 17),
    axis.title.x = element_text(
      colour = "black", size = 20,
      margin = margin(20, 20, 20, 20)
    ),
    axis.title.y = element_text(
      colour = "black", size = 20,
      margin = margin(0, 20, 0, 10)
    ),
    axis.text.x = element_text(
      colour = "black", angle = 30, hjust = 0,
      size = 15
    ),
    axis.text.y = element_text(colour = "black", size = 15),
    axis.line = element_line(
      colour = "#EBECEB",
      size = 1, linetype = "solid"
    ),
    axis.ticks.length = unit(.25, "cm")
  )

© www.soinside.com 2019 - 2024. All rights reserved.