简体   繁体   English

ggplot2调色板图例不显示

[英]ggplot2 palette legend does not show

I just started using ggplot2 R package and the following question should be very trivial, however I spent 2h on it without success. 我刚刚开始使用ggplot2 R软件包,以下问题应该很简单,但是我花了2h却没有成功。

I just need to show the scale_fill_distiller legend of the RdBu palette from -1 to 1 (red to blue) on my ggplot . 我只需要在scale_fill_distiller上显示RdBu调色板的scale_fill_distiller图例从-1到1(红色到蓝色) ggplot

Here's a code example: 这是一个代码示例:

## Load ggplot2 package
require(ggplot2)

## Create data.frame for 4 countries
shape = map_data("world") %>%
                 filter(region == "Germany" | region == 'Italy' | region == 'France' | region == 'UK')

## Order data.frame by country name
shape = shape[with(shape, order(region)), ]
rownames(shape) = NULL # remove rownames

##### Assign 4 different values (between -1 and 1) to each country by creating a new column 'id'
## These will be the values to be plotted with ggplot2 palette
shape[c(1:605),'id'] = 0.2
shape[c(606:1173),'id'] = -0.4
shape[c(1174:1774),'id'] = -0.9
shape[c(1775:2764),'id'] = 0.7

##### Make plot
ggplot() +
      ## plot countries borders
      geom_polygon(data = shape, aes(x = long, y = lat, group = group, fill = id), colour = 'black') +
      ## adjust coordinates
      coord_map() + 
      ## remove any background
      ggthemes::theme_map() + 
      ## add colour palette
      scale_fill_distiller(palette = 'RdBu', limits = c(1, -1), breaks = 50) 

The legend of the RdBu palette should pop out automatically but here it doesn't. RdBu调色板的图例应自动弹出,但此处不会弹出。 Is there any layer that is masking it? 有没有遮盖它的图层?

OR 要么

Is there any way to create a new legend from scratch and add it to the above plot? 有什么方法可以从头开始创建新图例并将其添加到上述图解中?

I need something like the picture below, but from -1 to 1 (Red to Blue) and vertical: 我需要类似下面的图片,但是从-1到1(红色到蓝色)和垂直:

在此处输入图片说明

Thanks 谢谢

The range specified in limits should be c(min, max) and not c(max, min) : limits指定的limits应为c(min, max)而不是c(max, min)

this works as expected: 这按预期工作:

library(ggmap)
library(ggplot2)
library(ggthemes)

ggplot() +
  geom_polygon(data = shape,
               aes(x = long, y = lat, group = group, fill = id), colour = 'black') +
  coord_map() +
  ggthemes::theme_map() +
  scale_fill_distiller(palette = 'RdBu', limits = c(-1,1))

在此处输入图片说明

while limits = c(1, -1) produces a plot without a colorbar: limits = c(1, -1)会产生没有颜色条的图:

ggplot() +
  geom_polygon(data = shape,
               aes(x = long, y = lat, group = group, fill = id), colour = 'black') +
  coord_map() +
  ggthemes::theme_map() +
  scale_fill_distiller(palette = 'RdBu', limits = c(1, -1))

在此处输入图片说明

If you would like to map the values in reverse order, you can use the direction argument: 如果要以相反的顺序映射值,则可以使用direction参数:

ggplot() +
  geom_polygon(data = shape,
               aes(x = long, y = lat, group = group, fill = id), colour = 'black') +
  coord_map() +
  ggthemes::theme_map() +
  scale_fill_distiller(palette = "RdBu",
                       limits = c(-1, 1),
                       breaks = c(-1, 0, 1),
                       direction = 1)

在此处输入图片说明

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

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