简体   繁体   English

R - 平滑颜色并向散点图添加图例

[英]R - Smoothing color and adding a legend to a scatterplot

I have a scatterplot in R. Each (x,y) point is colored according to its z value. 我在R中有一个散点图。每个(x,y)点都根据其z值着色。 So you can think of each point as (x,y,z) , where (x,y) determines its position and z determines its color along a color gradient. 因此,您可以将每个点视为(x,y,z) ,其中(x,y)确定其位置, z沿颜色渐变确定其颜色。 I would like to add two things 我想补充两件事

  1. A legend on the right side showing the color gradient and what z values correspond to what colors 右侧的图例显示颜色渐变, z值对应于什么颜色
  2. I would like to smooth all the color using some type of interpolation, I assume. 我想,我想使用某种类型的插值来平滑所有颜色。 In other words, the entire plotting region (or at least most of it) should become colored so that it looks like a huge heatmap instead of a scatterplot. 换句话说,整个绘图区域(或至少大部分绘图区域)应该变为彩色,以使其看起来像一个巨大的热图而不是散点图。 So, in the example below, there would be lots of orange/yellow around and then some patches of purple throughout. 所以,在下面的例子中,周围会有很多橙色/黄色,然后是一些紫色斑点。 I'm happy to further clarify what I'm trying to explain here, if need be. 如果需要的话,我很高兴进一步澄清我在这里要解释的内容。

Here is the code I have currently, and the image it makes. 这是我目前的代码,以及它制作的图像。

x <- seq(1,150)
y <- runif(150)
z <- c(rnorm(mean=1,100),rnorm(mean=20,50))
colorFunction <- colorRamp(rainbow(100))
zScaled <- (z - min(z)) / (max(z) - min(z))
zMatrix <- colorFunction(zScaled)
zColors <- rgb(zMatrix[,1], zMatrix[,2], zMatrix[,3], maxColorValue=255)
df <- data.frame(x,y)
x <- densCols(x,y, colramp=colorRampPalette(c("black", "white")))
df$dens <- col2rgb(x)[1,] + 1L
plot(y~x, data=df[order(df$dens),],pch=20, col=zColors, cex=1)

在此输入图像描述

Here are some solutions using the ggplot2 package. 以下是使用ggplot2包的一些解决方案。

# Load library
library(ggplot2)

# Recreate the scatterplot from the example with default colours
ggplot(df) +
  geom_point(aes(x=x, y=y, col=dens))

# Recreate the scatterplot with a custom set of colours. I use rainbow(100)
ggplot(df) +
  geom_point(aes(x=x, y=y, col=dens)) +
  scale_color_gradientn(colours=rainbow(100))

# A 2d density plot, using default colours
ggplot(df) +
  stat_density2d(aes(x=x, y=y, z=dens, fill = ..level..), geom="polygon") +
  ylim(-0.2, 1.2) + xlim(-30, 180) # I had to twiddle with the ranges to get a nicer plot

# A better density plot, in my opinion. Tiles across your range of data
ggplot(df) +
  stat_density2d(aes(x=x, y=y, z=dens, fill = ..density..), geom="tile", 
                 contour = FALSE)

# Using custom colours. I use rainbow(100) again.
ggplot(df) +
  stat_density2d(aes(x=x, y=y, z=dens, fill = ..density..), geom="tile", 
                 contour = FALSE) +
  scale_fill_gradientn(colours=rainbow(100))

# You can also plot the points on top, if you want
ggplot(df) +
  stat_density2d(aes(x=x, y=y, z=dens, fill = ..density..), geom="tile", 
                 contour = FALSE) +
  geom_point(aes(x=x, y=y, col=dens)) +
  scale_colour_continuous(guide=FALSE) # This removes the extra legend

I attach the plots as well: 我也附上了这些情节:

情节

Also, using ggplot2, you can use color and size together, as in: 此外,使用ggplot2,您可以一起使用颜色和大小,如下所示:

ggplot(df, aes(x=x, y=y, size=dens, color=dens)) + geom_point() + 
scale_color_gradientn(name="Density", colours=rev(rainbow(100))) +
scale_size_continuous(range=c(1,15), guide="none")

which might make it a little clearer. 这可能会让它更清晰一些。

Notes: 笔记:

  1. The expression rev(rainbow(100)) reverses the rainbow color scale, so that red goes with the larger values of dens . 表达式rev(rainbow(100))反转彩虹色标,因此红色与较大的dens值一致。

  2. Unfortunately, you cannot combine a continuous legend (color) and a discrete legend (size), so you would normally get two legends. 不幸的是,你不能组合一个连续的图例(颜色)和一个离散的图例(大小),所以你通常会得到两个传说。 The expression guide="none" hides the size legend. 表达式guide="none"隐藏了大小图例。

Here's the plot: 这是情节:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM