在GGR图中离散分离的两个连续颜色梯度

在GGR图中离散分离的两个连续颜色梯度,r,ggplot2,colors,R,Ggplot2,Colors,我有一个美国各州数据的热图(使用虚拟数据),我试图让它连续运行颜色渐变,从[0,50]的深红色到浅红色,从[50100]的浅绿色到深绿色 如果我试着用一个从红色到绿色的渐变,中间会变得相当棕色,如果我从红色到白色变成绿色,那么用中间颜色阅读就变得困难了。有人能帮我找到一种在同一张地图上执行两个连续颜色渐变的方法吗? 这是我的密码: library(ggplot2) library(fiftystater) library(colorplaner) library(RColorBrewer)

我有一个美国各州数据的热图(使用虚拟数据),我试图让它连续运行颜色渐变,从[0,50]的深红色到浅红色,从[50100]的浅绿色到深绿色

如果我试着用一个从红色到绿色的渐变,中间会变得相当棕色,如果我从红色到白色变成绿色,那么用中间颜色阅读就变得困难了。有人能帮我找到一种在同一张地图上执行两个连续颜色渐变的方法吗?

这是我的密码:

library(ggplot2)
library(fiftystater)
library(colorplaner)
library(RColorBrewer)



# prepare data frame
data("fifty_states") 


dft <- data.frame(state=tolower(rownames(USArrests)), USArrests)
names(dft)[names(dft)=='Murder'] <- 'Var1'
dft<-dft[,-c(3:5)]


# create data
dft$Var1 <- runif(50,0, 100)






# map to each state's data
p <- ggplot(dft, aes(map_id = state)) + 
# map points to the fifty_states shape data
  geom_map(aes(fill = Var1), map = fifty_states, color = 'gray') + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_x_continuous(breaks = NULL) + 
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") +
  theme(legend.position = "bottom", 
        panel.background = element_blank())

p+ fifty_states_inset_boxes() 
库(ggplot2)
图书馆(第五州)
图书馆(彩色刨床)
图书馆(RColorBrewer)
#准备数据帧
数据(“五十个州”)

dft以下是Andrew Gustar进一步解释的想法:

pc <- p + scale_fill_gradientn(colors = c("darkred", "tomato1","palegreen", "darkgreen"),
                             values = scales::rescale(c(min(dft$Var1),
                                                      (max(dft$Var1)-min(dft$Var1))/3,
                                                      (max(dft$Var1)-min(dft$Var1))*2/3,
                                                      max(dft$Var1))))

pc + fifty_states_inset_boxes() + theme(legend.position = "right")
定义值。示例:

pc <- p+scale_fill_gradientn(colors = c("darkred", "tomato1","palegreen", "darkgreen"),
                             values = scales::rescale(quantile(dft$Var1, seq(0, 1, length.out=4))))

pc + fifty_states_inset_boxes() + theme(legend.position = "right")

pc可能类似于
+scale\u fill\u gradientn(颜色=c(“暗红色”、“浅红色”、“浅绿色”、“暗绿色”),值=c(0,0.5,0.5001,1))
pc <- p + scale_fill_gradientn(colors = c("darkred", "tomato1","palegreen", "darkgreen"),
                             values = scales::rescale(c(min(dft$Var1),
                                                      (max(dft$Var1)-min(dft$Var1))/3,
                                                      (max(dft$Var1)-min(dft$Var1))*2/3,
                                                      max(dft$Var1))))

pc + fifty_states_inset_boxes() + theme(legend.position = "right")
quantile(dft$Var1, seq(0, 1, length.out = n)) #n depending on the number of colors
pc <- p+scale_fill_gradientn(colors = c("darkred", "tomato1","palegreen", "darkgreen"),
                             values = scales::rescale(quantile(dft$Var1, seq(0, 1, length.out=4))))

pc + fifty_states_inset_boxes() + theme(legend.position = "right")