在R中的不同图中具有相同颜色比例的颜色渐变

时间:2016-04-17 00:54:27

标签: r colors maps raster color-scheme

我需要创建一些光栅图形,但保持颜色渐变比例相同,但我还希望颜色渐变为平滑渐变。是否可以保留大量的颜色(约100,以获得平滑的颜色渐变),但同时具有合理数量的中断,以便它可读?

SELECT r.*, r1.timestampstart - r.timestampend AS interval_til_start_of_next_row
FROM   routes r
LEFT   JOIN LATERAL (
   SELECT timestampstart
   FROM   routes
   WHERE  timestampstart > r.timestampend
   ORDER  BY timestampstart  -- or BY timestampstart, id - to break ties if not unique
   LIMIT  1
   ) r1 ON true;

在这种情况下,我希望有一个颜色渐变,但是你无法读取中断标签 enter image description here

当我减少休息次数时,颜色渐变变为全彩色

def izracunaj_dohvatljiva_stanja(funkcije_prijelaza):
    dohvatljiva = []
    dohvatljiva.extend(pocetno_stanje)
    pomocna = collections.OrderedDict
    for i in xrange(len(dohvatljiva)):
        for temp in pomocna.keys(): <-----------------------------------this line
            if temp.split(',')[0] == dohvatljiva[i]:
                if funkcije_prijelaza.get(temp) not in dohvatljiva:
                    dohvatljiva.extend(funkcije_prijelaza.get(temp))

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以使用at来定义范围(最小值,最大值,间隔)。较小的间隔将产生更平滑的颜色范围。请在此处查看相关问题 - How to setup step color region in r?或此帖Locking color key in levelplot in r

答案 1 :(得分:1)

ggplot2您可能会发现这更容易。在下面的代码中,对于每个绘图,关键是为lowmidhigh以及limits中的相同scale_fill_gradient2设置相同的颜色值}。这保证了相同的数据值被映射到每个图中的相同颜色。例如:

library(rasterVis)
library(ggplot2)

# Reproducible rasters
set.seed(4598)
r1<- raster(ncol=56, nrow=26)
r1[] <- runif(n=56*26,min=-20,max=15)

r2<- raster(ncol=56, nrow=26)
r2[] <- runif(n=56*26,min=-14,max=68)

# Get range of data values across the two rasters
rng = range(c(getValues(r1), getValues(r2)))

gplot(r1) + 
  geom_tile(aes(fill=value)) +
  ggtitle("r1") +
  scale_fill_gradient2(low="red", mid="green", high="blue", 
                       midpoint=mean(rng),    # Value that gets the middle color (default is zero)
                       breaks=seq(-100,100,10),  # Set whatever breaks you want
                       limits=c(floor(rng[1]), ceiling(rng[2])))  # Set the same limits for each plot

gplot(r2) + 
  geom_tile(aes(fill=value)) +
  ggtitle("r2") +
  scale_fill_gradient2(low="red", mid="green", high="blue", 
                       midpoint=mean(rng),    # Value that gets the middle color (default is zero)
                       breaks=seq(-100,100,10),  # Set whatever breaks you want
                       limits=c(floor(rng[1]), ceiling(rng[2])))  # Set the same limits for each plot

enter image description here