合并调色板颜色colorRampPalette并使用ggplot进行绘图 [英] merging palette colors colorRampPalette and plotting with ggplot

查看:76
本文介绍了合并调色板颜色colorRampPalette并使用ggplot进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ggplot2 复制,这是我使用

但是传单图(正确的着色)

  leaflet()%>%addTiles()%&%;%addPolygons(data = nc,fillOpacity = 0.7,fillColor =〜mypal(nc $ category),弹出窗口= paste("PERIMETER:",nc $ PERIMETER))%&%addLegend(pal = mypal,值= nc $ category) 

给出正确的情节:

您会看到 ggplot 数字与我无法解决的数字不同.

另外,当我在 ggplot 中添加图例时,我得到了新调色板的因子图例,但我想要一个连续的(即使变量是一个因子):

  nc%>%ggplot()+geom_sf(aes(填充=因子(类别)))+scale_fill_manual(值= mypal(nc $ category)) 

有人会知道如何解决着色问题并以连续形式更改关联的图例吗?谢谢

解决方案

我认为是由于使用 factor()并使用以下代码引起的:

  nc%>%ggplot()+geom_sf(aes(填充=因子(类别)))+scale_fill_manual(values = mypal(levels(factor(nc $ category))),guide = FALSE) 

为您提供一幅与传单颜色相同的地图.

I'm trying to replicate using ggplot2, an earlier question i had using leaflet. I'm trying to merge two palettes where one is used if my variable is below a certain threshold and another if above a certain threshold. As I did with my leaflet question my final plot doesn't seem to be correct. I also want to convert the factor legend to a continuous one. Example code:

library(sf)  
library(leaflet)
library(RColorBrewer)

# preparing the shapefile
nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>%
  st_transform(st_crs(4326)) %>%
  st_cast('POLYGON')

# Create 108 categories

mutate(nc, category = ntile(PERIMETER, n = 108)) -> nc

x <- sum(nc$PERIMETER < 1.3)  
x # number of values below threshold = 21


### Create an asymmetric color range
## Make vector of colors for values smaller than 1.3 (21 colors)
rc1 <- colorRampPalette(colors = c("#006837", "#1A9850"), space = "Lab")(x)    #21

## Make vector of colors for values larger than 1.3
rc2 <- colorRampPalette(colors = c("#FDAE61", "#A50026"), space = "Lab")(length(nc$PERIMETER) - x)

## Combine the two color palettes
rampcols <- c(rc1, rc2)

mypal <- colorFactor(palette = rampcols, domain = nc$category)
previewColors(colorNumeric(palette = rampcols, domain = NULL), values = 1:length(nc$PERIMETER))


nc %>%
  ggplot() +
  geom_sf(aes(fill = factor(category))) +
  scale_fill_manual(values = mypal(nc$category), guide = FALSE) 

gives this plot:

but the leaflet plot (correct coloring)

leaflet() %>%
  addTiles() %>%
  addPolygons(data = nc,
              fillOpacity = 0.7,
              fillColor = ~mypal(nc$category),
              popup = paste("PERIMETER: ", nc$PERIMETER)) %>%
  addLegend( pal = mypal, values = nc$category)

gives this correct plot:

You can see the ggplot figure is not the same which I can't fix.

Additionally when I add a legend to the ggplot, I get a factor legend for the new palette but I want a continuous one (even if the variable is a factor):

nc %>%
  ggplot() +
  geom_sf(aes(fill = factor(category))) +
  scale_fill_manual(values = mypal(nc$category))

Would anyone know how to fix the coloring issue and alter the associated legend in continuous form? Thanks

解决方案

I think is caused by the use of factor(), using the following code:

nc %>%
  ggplot() +
  geom_sf(aes(fill = factor(category))) +
  scale_fill_manual(values = mypal(levels(factor(nc$category))), guide = FALSE) 

gives you a map with the same coloring as leaflet does.

这篇关于合并调色板颜色colorRampPalette并使用ggplot进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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