简体   繁体   English

geom_contour_filled 色标不对应中断

[英]geom_contour_filled color scale does not correspond to breaks

I would like to plot contour fills with colour based on a range of breaks that is larger than the range of the data, so that different plots have the same scale.我想 plot 轮廓基于大于数据范围的中断范围填充颜色,以便不同的图具有相同的比例。 In the following example, blue and red are expected to correspond to more negative or positive values, respectively.在下面的示例中,蓝色和红色预计分别对应更多的负值或正值。 However, when the plot does not contain the full range, geom_contour_filled recognizes the breaks but does not match the colour scale.然而,当 plot 不包含完整范围时, geom_contour_filled识别中断但不匹配色标。 So, positive values are all blue.所以,正值都是蓝色的。

library(ggplot2)

grid <- expand.grid(x=0:10,y=0:10)
grid$z <- with(grid, x*y) # 0 to 100 does not work as expected
# grid$z <- with(grid, 2*x*y-100) # -100 to 100 works as expected

ggplot(grid,aes(x=x,y=y,z=z)) +
  scale_colour_manual( aesthetics = 'fill',
                       values = colorRampPalette(c('blue','white','red'))(20) ) +
  geom_contour_filled( breaks=floor(seq(-100,100, length.out=20)) )

You need to add drop = FALSE to your color scale:您需要将drop = FALSE添加到色标中:

ggplot(grid,aes(x=x,y=y,z=z)) +
  scale_colour_manual( aesthetics = 'fill', drop = FALSE,
                       values = colorRampPalette(c('blue','white','red'))(20) ) +
  geom_contour_filled( breaks=floor(seq(-100,100, length.out=20)) )

在此处输入图像描述

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

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