地图ggplot中的两种比例颜色 [英] two scale colors in maps ggplot

查看:93
本文介绍了地图ggplot中的两种比例颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 ggplot 上绘制地图,并且在回顾了 几个 之后,想绘制两个不同的 scale_fill_gradient 堆栈溢出的帖子,我无法达到预期的结果.

I am ploting maps on ggplot and I would like to plot two different scale_fill_gradient, after reviewing several posts of stack overflow I couldnt reach the desired results.

shapefile 中,我将一个 spain 的映射读入数据框,其中有两列,其中每一行仅在一个区域中被通知,而在两个区域中都没有.关键是要用两个 scale_fill_gradient 表示同一图中的两个概念.

From a shapefile I read into a dataframe a map of spain there are two columns where every row is only informed in one of the regions, never in both. The point is to represent both concepts in the same plot with two scale_fill_gradient.

这是我的实际图片代码:

This is my the code for the actual picture:

df %>% 
  ggplot(aes(x=long, y= lat, group = group)) +
  geom_polygon(aes_string(color= "ratio_quan"), size = 0.2)+
  scale_color_gradient(low ="yellow", high ="blue", na.value="white")+
  geom_polygon(aes_string(fill= "ratio_qua"), size = 0.2)+
  scale_fill_gradient(low ="pink", high ="red", na.value="blank")

据我所知,只能提供一个填充选项,因此我使用了 scale_color_gradient scale_fill_gradient ,但是在一个比例尺和填充中存在边界区别另一个是我要完成的任务是:

As far as I understood only one fill option can be provided, for that reason I used scale_color_gradient and scale_fill_gradient, but there are border distinction in one scale and fill in the other one, in stead of that what I want to accomplish is the following:

如您所知,在这种情况下,两个概念都由多边形中的颜色填充.

As you realise, in this case both concepts are filled by colors in the polygons.

推荐答案

解决方案1 ​​

完成要求

@艾伦·卡梅隆(Allan Cameron)

Solution 1

Accomplish the requirements

By @Allan Cameron .

此问题已通过 ggnewscale 解决,此问题先前已在堆栈溢出时在很多线程中得到解决,并已通过其他方式解决.

This issue is solved by ggnewscale, this question has been addressed previously in lot of threads at stack-overflow previously and were solved in alternative ways.

通过使用函数 new_scale_fill()导入库 ggnewscale ,可以轻松地应用新的比例尺颜色.

By importing the library ggnewscale with the function new_scale_fill() a new scale color can be applied in an easy way.

library(ggnewscale)

df %>% 
  ggplot(aes(x=long, y= lat, group = group)) +
  geom_polygon(aes_string(fill= "ratio_quan"), size = 0.2)+
  scale_fill_gradient(low ="yellow", high ="blue", na.value="white")+
  new_scale_fill() +
  geom_polygon(aes_string(fill= "ratio_qua"), size = 0.2)+
  scale_fill_gradient(low ="pink", high ="red", na.value="blank")

解决方案2

替代

这种经典方法是使用带有 grid.arrange 的排列图,花式不太好,但可能更易于解释.

Solution 2

Alternative

This classic approach is by using to arranged plots with grid.arrange which is less fancy but could be more interpretable.

  p1 <- df %>% 
  ggplot(aes(x=long, y= lat, group = group)) +
  geom_polygon(aes_string(fill= "ratio_quan"), size = 0.2)+
  scale_fill_gradient(low ="yellow", high ="blue", na.value="white")
  p2 <- df %>% 
  ggplot(aes(x=long, y= lat, group = group)) +
  geom_polygon(aes_string(fill= "ratio_qua"), size = 0.2)+
  scale_fill_gradient(low ="yellow", high ="blue", na.value="white")
  grid.arrange(p1, p2, nrow = 1)

结果是,在同一图上使用了两个具有孤立比例的图.

As a result two plots with isolated scales are used on the same plots.

这篇关于地图ggplot中的两种比例颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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