我使用 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 标签和我的顶部瓷砖之间有间隙。关于如何消除这个差距的任何想法?
您的代码有两个问题。首先,您将
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")
)