r - Using comma delimited values from a dataframe in hsv() -
this question related 1 asked here. i'm trying color individual points in scatterplot color specified in column of dataframe. data tab delimited file , looks this:
> dput(ztesthsv) structure(list(y = c(0, -1, -1, -2), x = c(0, 2, 0, -2), group = c("m", "m", "m", "s"), colorhsv = c("0.02,0.83,0.89", "0.59,0.59,0.85", "0.25,0.45,0.8", "0.55,0.41,0.8"), colortext = c("red", "blue", "green", "turquoise")), .names = c("y", "x", "group", "colorhsv", "colortext"), class = "data.frame", row.names = c(na, -4l))
i.e.
y x group colorhsv colortext 1 0 0 m 0.02,0.83,0.89 red 2 -1 2 m 0.59,0.59,0.85 blue 3 -1 0 m 0.25,0.45,0.8 green 4 -2 -2 s 0.55,0.41,0.8 turquoise
the following code works predefined set of colors available in r:
ztesthsv=read.table(file="testhsv.txt",sep="\t",header=true, colclasses=c("numeric","numeric","character","character","character")) plot(ztesthsv[ztesthsv$group=='m',]$x,ztesthsv[ztesthsv$group=='m',]$y,type='n') points(ztesthsv[ztesthsv$group=='m',]$x,ztesthsv[ztesthsv$group=='m',]$y, pch=23,cex=1.5, bg=ztesthsv[ztesthsv$group=='m',]$colortext )
what use hsv()
specify colors. can't figure out right bg
command make work. i've tried several things , listed them below associated result.
1
bg=hsv(as.numeric(ztesthsv$colorhsv)) error in hsv(as.numeric(ztesthsv$colorhsv)) : bad hsv rgb color conversion in addition: warning message: in hsv(as.numeric(ztesthsv$colorhsv)) : nas introduced coercion
2
bg=hsv(as.numeric(strsplit(ztesthsv$colorhsv,","))) error in hsv(as.numeric(strsplit(ztesthsv$colorhsv, ","))) : (list) object cannot coerced type 'double'
3
bg=hsv(unlist(strsplit(ztesthsv$colorhsv,",")))
above runs uses wrong background colors
4
bg=hsv(as.numeric(unlist(strsplit(ztesthsv$colorhsv,","))))
above doesn't generate error doesn't fill background colors
it seems problem converting character string of colorhsv hsv()
can use, don't know how it. great.
it helps if provide reproducible example using dput()
show data using:
> dput(ztesthsv) structure(list(y = c(0l, -1l, -1l, -2l), x = c(0l, 2l, 0l, -2l ), group = structure(c(1l, 1l, 1l, 2l), .label = c("m", "s"), class = "factor"), colorhsv = c("0.02,0.83,0.89", "0.59,0.59,0.85", "0.25,0.45,0.8", "0.55,0.41,0.8"), colortext = structure(c(3l, 1l, 2l, 4l), .label = c("blue", "green", "red", "turquoise"), class = "factor")), .names = c("y", "x", "group", "colorhsv", "colortext"), row.names = c(na, -4l ), class = "data.frame")
you can convert vector of character strings list of numerics, , use do.call()
call hsv()
on each one, this:
> bg <- sapply(strsplit(ztesthsv$colorhsv, ","), function(x) do.call(hsv, as.list(as.numeric(x)))) > dput(bg) c("#e33d27", "#5994d9", "#9ecc70", "#78b3cc") > plot(1:4, pch = 21, cex = 20, bg = bg)
Comments
Post a Comment