R:FUN(X [[i]],...)中的错误:仅在具有所有数字变量的数据帧上定义 [英] R: Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables

查看:116
本文介绍了R:FUN(X [[i]],...)中的错误:仅在具有所有数字变量的数据帧上定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言.我正在尝试绘制一些正在使用的分类数据和连续数据,但出现一个错误,告诉我只有使用仅数字变量"才能进行此类绘制.

 图书馆(生存)库(ggplot2)数据(肺)数据=肺data $ sex = as.factor(数据$ sex)data $ status = as.factor(数据$ status)data $ ph.ecog = as.factor(data $ ph.ecog)str(数据)#阴谋mycolours<-彩虹(length(unique(data $ sex)),end = 0.6)#png("gally.png",500、400,类型="cairo",点数= 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 =变量",ylab =标准值")轴(1,1:5,标签= colnames(数据)[1:6])abline(v = 1:5,col =#00000033",lwd = 2)abline(h = seq(-2.5,2.5,0.5),col =#00000022",lty = 2)对于(i在1:nrow(data))行中(as.numeric(data [i,1:6]),col = mycolours [as.numeric(data $ sex [i])]))图例("topright",c("Female","Male"),lwd = 2,col = mycolours,bty ="n")#dev.off() 

有人知道分类数据和连续数据是否都可以做到?

谢谢

来源:

 图书馆(生存)数据(肺)#数据准备肺部鳞状<-适用(肺部2,刻度)drop.column.index<--which(colnames(lung)=="sex")肺标度<-肺标度[,-drop.column.index]#删除split变量split.var<-肺[,drop.column.index]肺<-肺[,-drop.column.index]mycolours<-彩虹(length(unique(split.var)),end = 0.6,v = 0.9,alpha = 0.4)#png("gally.png",500、400,类型="cairo",点数= 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 =标准值")axis(1,1:ncol(肺部缩放),标签= colnames(肺部),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)对于(i in 1:nrow(lung.scaled))行(as.numeric(lung.scaled [i,]),col = mycolours [as.numeric(split.var [i])]))图例("topleft",c("Female","Male"),lwd = 3,col = mycolours,bty ="n")#一些带有白色光环的类别变量的标签,以提高可读性labels.with.halo<-function(varname,data.scaled,labels,nhalo = 32,col.halo =#FFFFFF44",hscale = 0.04,vscale = 0.04,...){偏移量<-cbind(cos(seq(0,2 * pi,length.out = nhalo + 1))* hscale,sin(seq(0,2 * pi,length.out = nhalo + 1))* vscale)[-(nhalo +1),]ind<-其中(colnames(data.scaled)== varname)yvals<-sort(unique(data.scaled [,ind]))对于(i in 1:nhalo)text(rep(ind,length(yvals))+ offsets [i,1],yvals + offsets [i,2],标签=标签,col = col.halo,...)文本(rep(ind,length(yvals)),yvals,标签=标签,...)}带有halo的标签(状态",经肺部缩放,c(经审查",死亡"),pos = 3)带有halo("ph.ecog",肺部标度,c(无症状","Symp.but ambul.",< 50%bed",> 50%bed")的标签.;),pos = 3,cex = 0.9)#dev.off() 

I am working with the R programming language. I am trying to plot some categorical and continuous data that I am working with, but I am getting an error that tells me that such plots are only possible with "only numeric variables".

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

Does anyone know if this is possible to do with both categorical and continuous data?

Thanks

Sources: R: Parallel Coordinates Plot without GGally

解决方案

Yup. You just have to be careful with the values. Remember how the factors are coded internally: they are just spicy integer variables with value labels (similar to names). You can losslessly cast it to character or to numeric. For the sake of plotting, you need numbers for line coordinates, so the factor-y nature of your variables will come at the end.

Remember that the quality of your visualisation and the information content depends on the order of your variables in you data set. For factors, labels are absolutely necessary. Help the reader by doing some completely custom improvements impossible in ggplot2 in small steps!

I wrote a custom function allowing anyone to add super-legible text on top of the values that are not so obvious to interpret. Give meaningful names, choose appropriate font size, pass all those extra parameters to the custom function as an ellipsis (...)!

Here you can see that most of the dead patients are female and most of the censored ones are males. Maybe adding some points with slight jitter will give the reader idea about the distributions of these variables.

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

这篇关于R:FUN(X [[i]],...)中的错误:仅在具有所有数字变量的数据帧上定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆