正负值不同颜色ggplot2 [英] Different colors for positive and negative values ggplot2

查看:64
本文介绍了正负值不同颜色ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制数据,突出显示正值和负值(分别为"firebrick"和"dodgerblue4"),并创建一个图例,其中"Upwelling"为正值,"Downwelling"为正值.负值.我的代码现在看起来像这样:

I would like to plot my data, highlighting the positive and negative values ("firebrick" and "dodgerblue4"respectively) and create a legend with 'Upwelling' being the positive values and "Downwelling" the negative values. My code now looks like this:

ggplot(dados19,aes(Date,UI))+
  geom_line()+
  ggtitle("Upwelling Index during the period 2012-2019")+
  theme(plot.title = element_text(hjust = 0.5))+
  xlab("Year") + ylab("Upwelling index")


A tibble: 10,225 x 2
     UI   Date               
   <dbl> <dttm>             
   37.9  2012-01-01 00:00:00
    9.18 2012-01-01 06:00:00
    1.18 2012-01-01 12:00:00
   27.0  2012-01-01 18:00:00
 -292.   2012-01-02 00:00:00
   98.2  2012-01-02 06:00:00
   95.9  2012-01-02 12:00:00
    6.19 2012-01-02 18:00:00
   -4.65 2012-01-03 00:00:00
   40.1  2012-01-03 06:00:00
# ... With 10,215 more rows

最好的表示方法是使用渐变

The best way to represented was using gradient

scale_color_gradient(low ="dodgerblue4",high ="firebrick")+

情节是这样的:

我需要相同的图,但只有两种颜色(红色代表正值,蓝色代表负值),而不是渐变.

I need the same plot but with only the 2 colors (red for positive values and blue for negative values), not a gradient.

推荐答案

此处是一种解决方案,它用不同的颜色绘制y轴变量的正值和负值的线段.如果使用辅助功能查找切点,即由具有不同符号的端点定义的线段的零.

Here is a solution that draws segments for positive and negative values of the y axis variable with different colors. If uses an auxiliary function to find the cut points, meaning, the zeros of the segments defined by end points with different signs.

lin_zeros <- function(data, x, y){
  xname <- deparse(substitute(x))
  yname <- deparse(substitute(y))
  x <- data[[xname]]
  y <- data[[yname]]
  ix <- which(c(FALSE, abs(diff(sign(y))) == 2))
  res <- lapply(ix, function(i) approx(x = y[c(i - 1, i)], y = x[c(i - 1, i)], xout = 0))
  res <- do.call(rbind.data.frame, res)
  res <- res[2:1]
  row.names(res) <- NULL
  names(res) <- c(xname, yname)
  res
}

现在是情节.

library(dplyr)
library(ggplot2)

dados19 %>%
  bind_rows(dados19 %>% 
              lin_zeros(Date, UI) %>%
              mutate(Date = as.POSIXct(Date, origin = "1970-01-01"))) %>%
  arrange(Date) %>%
  mutate(xend = lead(Date),
         yend = lead(UI, default = 0)) %>%
  mutate(line_color = (UI == 0 & yend < 0) | (UI < 0 & yend == 0)) %>%
  mutate(Index = ifelse(line_color, "Downwelling", "Upwelling"),
         Index = factor(Index)) %>%
  select(-line_color) %>% 
  ggplot(aes(Date, UI, color = Index)) +
  geom_segment(aes(xend = xend, yend = yend)) +
  scale_color_manual(breaks = c("Downwelling", "Upwelling"),
                     values = c("dodgerblue4", "firebrick")) +
  ggtitle("Upwelling Index during the period 2012-2019") +
  xlab("Year") + 
  ylab("Upwelling index") +
  theme(plot.title = element_text(hjust = 0.5))

这篇关于正负值不同颜色ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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