R scatter plot with hexadecimal colors

大憨熊 提交于 2020-01-04 04:13:08

问题


I've got a CSV file with 3 columns, the X values, Y values, and their corresponding hexadecimal (#RRGGBB) values. I've been trying to create a scatter/bubble plot with the insides of the bubbles colored according to the hex values.

symbols(year, logtrans, circles=size, inches=0.05, bg=intel2$HexLogClock)

intel2$HexLogClock contains the hex values.

Sorry again for the noob question, any help is appreciated.


回答1:


I think your trouble may lie in the hex values not being a character. Make sure they are first. See example below:

year <- 1:5
logtrans <- log(year)
size <- rep(15,5)
intel2 <- data.frame(HexLogClock=c("#330000", "#FFFFCC", "#660000", "#FF0000", "#00FF00"),stringsAsFactors=FALSE)
symbols(year, logtrans, circles=size, inches=0.05, bg=intel2$HexLogClock)

Notice the stringsAsFactors=FALSE code, which you can specify for read.csv and other import methods to ensure your character data isn't converted to a factor.

You can do this for your data using:

intel2$HexLogClock <- as.character(intel2$HexLogClock)



回答2:


I imagine intel2$HexLogClock is stored as a factor. Confirm this with class(intel2$HexLogClock). In this case, each level of the factor is represented by an integer (which is assigned based on the order of factor level's first occurrences), so your first HexLogClock color will be black (1=black), then red, then green, blue, cyan, and so on.

To correct this, you need to convert intel2$HexLogClock to a character vector, thusly:

intel2$HexLogClock <- as.character(intel2$HexLogClock)

after which your command should work as you had expected.

Alternatively:

symbols(year, logtrans, circles=size, inches=0.05, 
        bg=as.character(intel2$HexLogClock))



回答3:


I think I'm misunderstanding, if so let me know but you can just supply the hexadecimal values to col as in:

barplot(1:3, axes=FALSE, col=c("#330000", "#FFFFCC", "660000"))


来源:https://stackoverflow.com/questions/10238526/r-scatter-plot-with-hexadecimal-colors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!