【问题标题】:Color intensity proportional to a number颜色强度与数字成正比
【发布时间】:2019-10-12 18:45:38
【问题描述】:

我有以下数据框

> dataframe
 lx         rx   mc     
  6  58.340712    6
  6 -66.993792    0
 16  -7.163176    1
  4  43.801029    2

与:

>plot(dataframe$lx,dataframe$rx)

我获得了点图(6,58.340712)(6,-66.993792)(16,-7.163176)(4,43.801029)。我想根据mc 字段为这些点着色。例如,如果mc=0,则该点为黑色,否则为红色。另外,我希望颜色强度与mc 字段中的值成正比。

【问题讨论】:

  • 添加参数col=dataframe$mc

标签: r dataframe plot colors


【解决方案1】:

数据

d = structure(list(lx = c(6L, 6L, 16L, 4L),
                   rx = c(58.340712, -66.993792, -7.163176, 43.801029),
                   mc = c(6L, 0L, 1L, 2L)),
              class = "data.frame",
              row.names = c(NA, -4L))

基础

#Figure out how many colors you need. If the minimum is 0, we need to add 1
#because indexing in R begins at 1.
n = max(d$mc) + (min(d$mc) == 0)

#Generate n colors based on your preference
cols = colorRampPalette(c("black", "red"))(n)

#Subset colors from 'cols' using values in d$mc
plot(d$lx, d$rx, col = cols[d$mc + (min(d$mc) == 0)], pch = as.character(d$mc), cex = 3)

gglot

library(ggplot2)
ggplot(d, aes(lx, rx, col = mc)) +
    geom_point(size = 3) +
    scale_color_gradient(low = "black", high = "red")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 2013-05-26
    • 2011-10-19
    • 2012-07-17
    • 2020-02-26
    相关资源
    最近更新 更多