簡體   English   中英

使用ggplot2熱圖可視化每列中的NA數量

[英]Visualize the number of NAs in each column using ggplot2 heatmap

我有一個向量,用於保存數據集每一列中的NA數量。

countNA <-  c(0, 0, 88, 88, 161, 84, 540, 0, 84, 84, 84, 86, 101, 77, 80, 80, 72, 119, 72, 0, 72, 86, 72, 70, 70, 70, 161, 86, 80, 101, 77, 497, 80, 161, 88, 84, 86, 497, 471, 81, 560, 88, 88, 497, 472, 84, 0)

我想將其繪制為ggplot2中的熱圖

countNA <-  c(0, 0, 88, 88, 161, 84, 540, 0, 84, 84, 84, 86, 101, 77, 80, 80, 72, 119, 72, 0, 72, 86, 72, 70, 70, 70, 161, 86, 80, 101, 77, 497, 80, 161, 88, 84, 86, 497, 471, 81, 560, 88, 88, 497, 472, 84, 0)
plotNA3 <- as.data.frame(countNA, row.names = NULL)
plotNA3$VariableNames <- as.factor(seq(1, length(countNA)))
plotNA3$team <- as.factor(rep(1, length(countNA)))

ggplot(data = plotNA3, aes(x=VariableNames, y=team, fill=countNA)) +
  geom_tile() +
  labs(fill='Number of NAs in each column')+
  geom_text(aes(label=paste(sprintf("%.1d", countNA))), size=2, color="white")

但是,您將看到,如果運行此命令,它不是一塊正方形的瓷磚,而是被拉伸的。 是否可以對其進行一些更改,以使y軸不會拉伸?

我猜測真正的問題是“如何創建僅包含1個變量的熱圖”

這是一個可能的解決方案:

ggplot(data = plotNA3, aes(x=VariableNames, y=team, fill=countNA)) +
  geom_tile() +
  labs(fill='Number of NAs in each column')+
  geom_text(aes(label=paste(sprintf("%.1d", countNA))), size=2, color="white")+
  theme(aspect.ratio=1/25)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM