【问题标题】:R: Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variablesR: FUN(X[[i]], ...) 中的错误:仅在具有所有数值变量的数据帧上定义
【发布时间】:2021-01-22 19:36:50
【问题描述】:

我正在使用 R 编程语言。我正在尝试绘制一些我正在使用的分类和连续数据,但我收到一个错误,告诉我这样的图只能使用“仅数字变量”。

library(survival)
library(ggplot2)

data(lung)
data = lung
data$sex = as.factor(data$sex)
data$status = as.factor(data$status)
data$ph.ecog = as.factor(data$ph.ecog)
str(data)

#plot
mycolours <- rainbow(length(unique(data$sex)), end = 0.6)
# png("gally.png", 500, 400, type = "cairo", pointsize = 14)
par(mar = c(4, 4, 0.5, 0.75))
plot(NULL, NULL, xlim = c(1, 5), ylim = range(data[, 1:6]) + c(-0.2, 0.2),
     bty = "n", xaxt = "n", xlab = "Variable", ylab = "Standardised value")
axis(1, 1:5, labels = colnames(data)[1:6])
abline(v = 1:5, col = "#00000033", lwd = 2)
abline(h = seq(-2.5, 2.5, 0.5), col = "#00000022", lty = 2)
for (i in 1:nrow(data)) lines(as.numeric(data[i, 1:6]), col = mycolours[as.numeric(data$sex[i])])
legend("topright", c("Female", "Male"), lwd = 2, col = mycolours, bty = "n")
# dev.off()

有谁知道这是否可以同时用于分类数据和连续数据?

谢谢

来源:R: Parallel Coordinates Plot without GGally

【问题讨论】:

    标签: r ggplot2 data-visualization data-manipulation


    【解决方案1】:

    是的。你只需要小心这些值。记住因子是如何在内部编码的:它们只是带有值标签的辛辣整数变量(类似于名称)。您可以无损地将其转换为字符或数字。为了绘图,您需要线坐标的数字,因此变量的因子 y 特性将出现在最后。

    请记住,可视化和信息内容的质量取决于数据集中变量的顺序。对于因素,标签是绝对必要的。通过小步骤在ggplot2 中进行一些不可能的完全自定义改进来帮助读者!

    我编写了一个自定义函数,允许任何人在不那么容易解释的值之上添加超级易读的文本。给出有意义的名称,选择适当的字体大小,将所有这些额外参数作为省略号 (...) 传递给自定义函数!

    在这里您可以看到大多数死亡患者是女性,而大多数被删减的患者是男性。也许添加一些带有轻微抖动的点会让读者了解这些变量的分布。

    library(survival)
    data(lung)
    # Data preparation
    lung.scaled <- apply(lung, 2, scale)
    drop.column.index <- which(colnames(lung) == "sex")
    lung.scaled <- lung.scaled[, -drop.column.index] # Dropping the split variable
    split.var <- lung[, drop.column.index]
    lung <- lung[, -drop.column.index]
    
    mycolours <- rainbow(length(unique(split.var)), end = 0.6, v = 0.9, alpha = 0.4)
    # png("gally.png", 500, 400, type = "cairo", pointsize = 14)
    par(mar = c(5.5, 4, 0.5, 0.75))
    plot(NULL, NULL, xlim = c(1, ncol(lung.scaled)), ylim = range(lung.scaled, na.rm = TRUE) + c(-0.2, 0.2),
         bty = "n", xaxt = "n", xlab = "", ylab = "Standardised value")
    axis(1, 1:ncol(lung.scaled), labels = colnames(lung), cex.axis = 0.95, las = 2)
    abline(v = 1:ncol(lung), col = "#00000033", lwd = 2)
    abline(h = seq(round(min(lung.scaled, na.rm = TRUE)), round(max(lung.scaled, na.rm = TRUE), 0.5)), col = "#00000022", lty = 2)
    for (i in 1:nrow(lung.scaled)) lines(as.numeric(lung.scaled[i, ]), col = mycolours[as.numeric(split.var[i])])
    legend("topleft", c("Female", "Male"), lwd = 3, col = mycolours, bty = "n")
    
    # Labels for some categorical variables with a white halo for readability
    labels.with.halo <- function(varname, data.scaled, labels, nhalo = 32, col.halo = "#FFFFFF44", hscale = 0.04, vscale = 0.04, ...) {
      offsets <- cbind(cos(seq(0, 2*pi, length.out = nhalo + 1)) * hscale, sin(seq(0, 2*pi, length.out = nhalo + 1)) * vscale)[-(nhalo + 1), ]
      ind <- which(colnames(data.scaled) == varname)
      yvals <- sort(unique(data.scaled[, ind]))
      for (i in 1:nhalo) text(rep(ind, length(yvals)) + offsets[i, 1], yvals + offsets[i, 2], labels = labels, col = col.halo, ...)
      text(rep(ind, length(yvals)), yvals, labels = labels, ...)
    }
    
    labels.with.halo("status", lung.scaled, c("Censored", "Dead"), pos = 3)
    labels.with.halo("ph.ecog", lung.scaled, c("Asymptomatic", "Symp. but ambul.", "< 50% bed", "> 50% bed"), pos = 3, cex = 0.9)
    
    # dev.off()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多