首页 文章

ggplot中的颜色,连续值提供给离散比例

提问于
浏览
1

我正在绘制ggplot2中产生的热图上的点 . delta 是包含要在热图上绘制的点的数据框 . 变量 plt 存储ggplot图像 .

热图由此链接的代码生成(由于文本限制,无法在此处发布) . 此外,所有所需数据帧的可重现代码都在链接中 .

https://justpaste.it/65iu7

现在要在热图上叠加点,我使用下面的代码:

plt0 <- plt + geom_point(data = delta, aes(x = dP/100, y = dT, z = NULL,  color = rcp, shape = future))
plt0

它给出了错误:

错误:提供给离散比例的连续值 .

如果我从上面的代码中删除 color = future ,它可以工作 . 但是我需要有这个代码产生的颜色编码点:

ggplot()+geom_point(data = delta, aes(x = dP/100, y = dT, z = NULL, color = rcp, shape = future))

产生此错误的原因是什么?如何解决?

2 回答

  • 1

    这样好吗?

    plt <- ggplot() + geom_tile(data=new.data, aes(x = hh/100, y = tt, fill=W)) +
      geom_contour(data=new.data, bins = 10, 
                   aes(x = hh/100, y = tt, #color = ..level.., 
                       z = floor(W)), 
                   show.legend = FALSE) +
      ylab("Change in temperature in degree Celsius") +
      xlab("percentage change in precipitation") +
      scale_fill_gradientn(name = "W (in m3/year)",
                           values = scales::rescale(quantile(new.data$W)),
                           limits = c(min(new.data$W),max(new.data$W)),
                           breaks = seq(round(min(new.data$W)/1000000)*1000000,
                                        round(max(new.data$W)/1000000)*1000000,
                                        (round(max(new.data$W)/1000000)*1000000-round(min(new.data$W)/1000000)*1000000)/3),
                           colors = rainbow(7), guide = "colorbar") +
      scale_x_continuous(breaks = seq(-0.3,0.3, 0.1), label = scales::percent) +
      scale_y_continuous(breaks = seq(-1, 6, 1)) +
      ggtitle("Variation of average annual sediment production with \n temperature and precipitation")+
      guides(fill = guide_colorbar(barwidth = 0.5, barheight = 10))
    
    plt
    
    plt + 
      geom_point(data = delta, aes(x = dP/100, y = dT,
                                   color = rcp, shape = future))
    

    enter image description here

  • 0

    除第一行外,我没有更改代码 . 代替 :

    plt<-ggplot(new.data, aes(x = hh/100, y = tt, z = floor(W))) + geom_tile(aes(fill = W)) + ...
    

    用过的

    plt<-ggplot()+ geom_tile(new.data, aes(x = hh/100, y = tt, fill = W)) + ...
    

    这是为了确定我们调用空 ggplot 然后使用 new.data 添加 geom_tile ,并且由于 ggplot 尚未分配任何默认数据,我们稍后可以为 geom_point 添加增量 . 输出如下:
    enter image description here

相关问题