R:使用矢量为绘图添加颜色

时间:2017-07-04 21:54:48

标签: r plot

根据enter image description here,我应该能够根据变量中的颜色值对图上的点进行着色,如下所示(直接引用):

ui-list

this website

我想用一维散点图来做这件事,称为(jDatColor <- merge(jDat, jColors)) ## continent country year pop lifeExp gdpPercap color ## 1 Africa Eritrea 2007 4906585 58.04 641.4 #1B9E77 ## 2 Africa Chad 2007 10238807 50.65 1704.1 #1B9E77 ## 3 Americas Jamaica 2007 2780132 72.57 7320.9 #D95F02 ## 4 Americas Cuba 2007 11416987 78.27 8948.1 #D95F02 ## 5 Americas Costa Rica 2007 4133884 78.78 9645.1 #D95F02 ## 6 Asia Nepal 2007 28901790 63.78 1091.4 #7570B3 ## 7 Europe Germany 2007 82400996 79.41 32170.4 #E7298A ## 8 Europe Norway 2007 4627926 80.20 49357.2 #E7298A #And now we can provide this new color variable as the value of the col = argument. plot(lifeExp ~ gdpPercap, jDatColor, log = 'x', xlim = jXlim, ylim = jYlim, col = color, main = 'custom color scheme based on Dark2', cex = 2) legend(x = 'bottomright', legend = as.character(jColors$continent), col = jColors$color, pch = par("pch"), bty = 'n', xjust = 1) 。我的(MWE)代码如下所示:

stripchart

产生这个:

enter image description here

根据我在total_data <- floor(runif(8000, 1,101)) class_val <- c(rep('one', 1000), rep('two', 1000), rep('three', 1000), rep('four', 1000), rep('five', 1000), rep('six', 1000), rep('seven', 1000), rep('eight', 1000)) colour_vals_pl <- rep(c('red', 'blue', 'yellow', 'dark blue', 'grey', 'pink', 'cornflowerblue', 'brown'), each=1000) colour_vals_pl <- factor(colour_vals_pl, levels=c('red', 'blue', 'yellow', 'dark blue', 'grey', 'pink', 'cornflowerblue', 'brown'), ordered=TRUE) class_val <- factor(class_val, levels=c('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'), ordered=TRUE) datatouse <- cbind.data.frame(total_data, class_val, colour_vals_pl) stripchart(total_data~class_val, main='Power law epidemic lengths', ylab="Epidemic duration (days)", xlab="Class", pch=20, vertical=TRUE, col=colour_vals_pl, data=datatouse) 中提供的定义,我希望每一行(一,二,三等)都是不同的颜色。我怎样才能做到这一点?

0 个答案:

没有答案