相关疑难解决方法(0)

当值低于零时如何使线为红色,高于时为绿色?

我希望当图表上的线低于 0 时为红色且高于绿色。我试图弄清楚我自己,但是,当我只需要低于 y=0 的颜色时,该代码在周五至周六的整个部分为我提供了一种颜色(“红色”,因为它低于 0)。

a<-structure(list(group = c("Total", "Total", "Total", "Total", 
"Total", "Total", "Total"), measure = c("sales", "sales", "sales", 
"sales", "sales", "sales", "sales"), day = structure(1:7, .Label = c("MONDAY", 
"TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("ALL", "DE", "BE", "NL", "AUS", "ES", 
"IT", "FR", "PO"), class = "factor"), value = c(2400000, 3750000, 
3400000, 3100000, 2300000, 3600000, 3100000), per_mtoday = c(2400000, 
2400000, 2400000, 2400000, 2400000, 2400000, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

2
推荐指数
1
解决办法
154
查看次数

使用 geom_line() 按正数或负数着色

抱歉,如果这是一个重复的问题,但我尝试过的其他答案并未产生我想要的情节。

我想根据该值是正值还是负值(我称之为“阶段”)对该图进行颜色编码。

当 geom_line() 达到 0 的上方或下方时,我希望它的颜色为“正”或“负” - 我认为以下方法可行,但似乎不起作用。有人能告诉我哪里出了问题吗?谢谢。

原剧情] 1

数据

structure(list(Date = c(1876, 1877, 1878, 1879, 1880, 1881), 
    value = c(4.95, -10.0416666666667, 2.95, 12.85, 7.8, -4.76666666666667
    ), phase = structure(c(3L, 1L, 3L, 3L, 3L, 1L), .Label = c("negative", 
    "neutral", "positive"), class = "factor")), row.names = c(NA, 
-6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x7ffb06817ce0>)
Run Code Online (Sandbox Code Playgroud)

代码

## create a column to determine if the phase is negative, positive or neutral i.e. 0

dd_avg$phase<-factor(sign(dd_avg$value), (-1):1, c('negative', 'neutral', 'positive'))
dd_avg …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

0
推荐指数
1
解决办法
1073
查看次数

标签 统计

ggplot2 ×2

r ×2