简体   繁体   中英

Color interpolation in R when dataset has NA values

I am trying to interpolate a color palette based on the values of the variable "weight" of my dataset.

F2 <- colorRampPalette(c("#c2c6b3", "#353828"), bias = length(unique(E(gD)$weight)), space = "rgb", interpolate = "linear")

However, some values on my dataset have NA values, and this provoques a (0) value on the color generated for that edge on my network graph.

As a result, when I try to assign the colors generated from this function into the edges of my network graph in R,

colCodes <- F2(length(unique(E(gD)$weight2)))
edges_col <- sapply(E(gD)$weight2, function(x)
colCodes[which(sort(unique(E(gD)$weight2)) == x)])`
edges_col_df <- as.data.frame(t(col2rgb(edges_col, alpha = FALSE)))

I get the error: "invalide color name `character(0)'"

I saw on a different post how to extrapolate on the case of missing values by the median o mean of values around the missing observation Imputing missing values keeping circular trend in mind but in my case, I would like the observations with NA values to acquire the value of the lower limit of my range of colors.

One possible solution would be to recode the NA values to the corresponding lower limit value.

data$variable[is.na(data$variable)]<-min(data$variable)

Sometimes to make this vector a new variable for data integrity reasons.

data$variable2[is.na(data$variable)]<-min(data$variable)

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM