“Geom_tile in ggplot – x轴上的缩放问题”

huangapple go评论56阅读模式
英文:

Geom_tile in ggplot- scaling problem on the x axis

问题

生成的混淆矩阵图中,我的顶部刻度轴的图块位置不正确。

这是一个模拟代码

库(ggplot2)
库(dplyr)
库(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] = "你好,我的朋友"
predClass[pred==2] = "你好,我的朋友"
predClass[pred==3] = "你好,我的朋友"
predClass[pred==4] = "你好,我的朋友"

truthClass<- truth
truthClass[truth==1] = "你好,我的朋友"
truthClass[truth==2] = "你好,我的朋友"
truthClass[truth==3] = "你好,我的朋友"
truthClass[truth==4] = "你好,我的朋友"

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 = "真实分类", y = "预测",
       fill = "频率") +
  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"))

生成的图表如下:
“Geom_tile in ggplot – x轴上的缩放问题”

我的x标签和顶部的图块之间有间隙。有什么办法可以去掉这个间隙吗?

英文:

The resultant plot for my confusion matrix using geom_tile is off on my top scale axis.

Here's a mock code

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"))

The resultant plot looks like this:
“Geom_tile in ggplot – x轴上的缩放问题”

There is a gap between my x labels and my top tiles. Any ideas on how I can remove this gap?

答案1

得分: 2

你的代码有两个问题。首先,在 geom_tile 中将 widthheight 设置为 3,我没有看到任何理由,实际上这样做没有太多意义,因为这样会导致瓦片重叠(要查看这一点,请将 alpha=.5 添加到 geom_tile 中)。其次,您正在将带有重复条目的向量 truthClass 传递给 limits 参数。修复这两个问题后,可以通过删除比例的默认扩展 expand=c(0, 0) 来去除间隙:

英文:

There are two issues with your code. First you have set the width and height to 3 in geom_tile for which I don't see any reason and which actually does not make that much sense as the tiles will overlap when doing so (To see this add e.g. alpha=.5 to geom_tile). Second, you are passing a vector truthClass with duplicated entries to the limits argument. After fixing both issues you could get rid of the gap(s) by removing the default expansion of the scale via 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")
  )

“Geom_tile in ggplot – x轴上的缩放问题”

答案2

得分: 1

这是您想要的输出吗?

似乎 limits = rev(truthClass) 部分与轴填充产生意外的交互作用,导致顶部产生了一些额外的填充。使用一些负填充可以去除它。

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

“Geom_tile in ggplot – x轴上的缩放问题”

英文:

Is this the output you want?

It seems that the limits = rev(truthClass) part interacts unexpectedly with the axis padding, such that some extra padding is generated at the top. Using some negative padding removes it.

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

“Geom_tile in ggplot – x轴上的缩放问题”

huangapple
  • 本文由 发表于 2023年3月31日 22:37:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定