用渐变对散点图进行颜色编码 [英] Color code a scatter plot by group with a gradient

查看:111
本文介绍了用渐变对散点图进行颜色编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有XY个数据,我想使用scatter plotRplotly程序包来绘制图形.

I have XY data which I'd like to graph using a scatter plot, with R's plotly package.

set.seed(1)
df <- data.frame(x=c(rnorm(30,1,1),rnorm(30,5,1),rnorm(30,9,1)),
                 y=c(rnorm(30,1,1),rnorm(30,5,1),rnorm(30,9,1)),
                 group=c(rep("A",30),rep("B",30),rep("C",30)),score=runif(90,0,1))

每个点都分配给三个组(df$group)之一,并且得分在[0,1]范围内.

Each point is assigned to one of three groups (df$group) and has a score in the [0,1] range.

我正在寻找一种以图形方式显示数据的方法,以使每个组使用不同的颜色进行着色,但是该颜色(或强度)的阴影会反映得分.

I'm looking for a way to graph the data such that each group is colored with a different color but the shade of that color (or intensity) reflects the score.

所以我认为这会起作用:

So I thought this would work:

library(dplyr)
library(plotly)

    plot_ly(marker=list(size=10),type='scatter',mode="markers",x=~df$x,y=~df$y,color=~df$score,colors=c("#66C2A5","#FC8D62","#8DA0CB")) %>%
  layout(xaxis=list(title="X",zeroline=F,showticklabels=F),yaxis=list(title="Y",zeroline=F,showticklabels=F))

但是我得到:

如果我只是按group进行颜色编码:

If I just color code by group:

plot_ly(marker=list(size=10),type='scatter',mode="markers",x=~df$x,y=~df$y,color=~df$group,colors=c("#66C2A5","#FC8D62","#8DA0CB")) %>%
      layout(xaxis=list(title="X",zeroline=F,showticklabels=F),yaxis=list(title="Y",zeroline=F,showticklabels=F))

我得到:

所以看起来它正在混合group颜色和score渐变.

So it looks like it is mixing the group colors and the score gradient.

我要寻找的是将左下组的颜色涂成绿色(例如,从graydarkgreen),与score(从低到高)相对应,并且两者相同其他组分别为橙色和蓝色.

What I'm looking for is to have the bottom left group colored in shades of green (say from gray to darkgreen) that correspond to score (low to high), and the same for the two other groups in orange and blue, respectively.

推荐答案

使用scales::colour_ramp,您可以快速创建颜色.我不确定在每个组中还会如何获得不同的渐变.请注意,我在这里使用df$score = df$x + df$y来使映射更加明显.

Using scales::colour_ramp you can create the colours yourself with a quick function. I'm not sure how else to get different gradients happening within each group. Note I'm using df$score = df$x + df$y here to make the mapping more obvious.

make_colour_gradient = function(x, brewer_palette = "Greens") {
    min_x = min(x)
    max_x = max(x)
    range_x = max_x - min_x
    x_scaled = (x - min_x) / range_x

    # Chopping out first colour as it's too light to work well as a
    #   point colour
    colours = scales::brewer_pal("seq", brewer_palette)(5)[2:5]

    colour_vals = scales::colour_ramp(colours)(x_scaled)
    colour_vals
}

df$score = df$x + df$y

df = df %>%
    # Assign a different gradient to each group, these are the names
    #   of different palettes in scales::brewer_pal
    mutate(group_colour = case_when(
        group == "A" ~ "Greens",
        group == "B" ~ "Oranges",
        group == "C" ~ "Purples"
    )) %>%
    group_by(group) %>%
    mutate(point_colour = make_colour_gradient(score, first(group_colour)))


plot_ly(marker=list(size=10),type='scatter',mode="markers",
        x=~df$x,y=~df$y,color=~ I(df$point_colour)) %>%
    hide_colorbar() %>%
    layout(xaxis=list(title="X",zeroline=F,showticklabels=F),
           yaxis=list(title="Y",zeroline=F,showticklabels=F))

结果:

这确实会显示错误消息,但是它们似乎并不重要?在其中添加图例可能会很棘手.

This does bring up error messages but they don't seem to be important? Adding a legend to this would probably be tricky.

这篇关于用渐变对散点图进行颜色编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆