使用缺失值的資料框:
structure(list(id = c("id1", "test", "rew", "ewt"), total_frq_1 = c(54, 87, 10, 36), total_frq_2 = c(45, 24, 202, 43), total_frq_3 = c(24, NA, 25, 8), total_frq_4 = c(36, NA, 104, NA)), row.names = c(NA, 4L), class = "data.frame")
如何創建一個條形圖,其中包含每列的平均值,不包括 id 列,但不使用 0 填充缺失值,但省略具有缺失值的行示例total_frq_3 24 25 8 = 57/3 = 19
uj5u.com熱心網友回復:
您可以使用colMeans函式并將適當的引數傳遞給它以忽略 NA。
library(ggplot2)
xy <- structure(list(id = c("id1", "test", "rew", "ewt"),
total_frq_1 = c(54, 87, 10, 36), total_frq_2 = c(45, 24, 202, 43), total_frq_3 = c(24, NA, 25, 8),
total_frq_4 = c(36, NA, 104, NA)),
row.names = c(NA, 4L),
class = "data.frame")
xy.means <- colMeans(x = xy[, 2:ncol(xy)], na.rm = TRUE)
xy.means <- as.data.frame(xy.means)
xy.means$total <- rownames(xy.means)
ggplot(xy.means, aes(x = total, y = xy.means))
theme_bw()
geom_col()

或者只使用基本影像圖形
barplot(height = colMeans(x = xy[, 2:ncol(xy)], na.rm = TRUE))

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523475.html
標籤:rggplot2
上一篇:通過對行分組的資料線圖
