简体   繁体   English

如何使用ggplot以不同颜色指示负值?

[英]How can I indicate negative values in different colour in shiny with ggplot?

I would like to display plot of a data frame which includes negative values either.我想显示一个包含负值的数据框的图。 In this case, the negative values may be indicated with red line.在这种情况下,负值可以用红线表示。 However I couldn't succeed.然而我没能成功。 When try to draw the plot, the negatives values began to follow sequence because of cbind argument.尝试绘制绘图时,由于 cbind 参数,负值开始遵循顺序。 In addition, I don't know another way to combine the data frames.另外,我不知道另一种组合数据框的方法。

set.seed(123)
tmp <- data.frame(time = 1:10, quantity = round(runif(10, -15, 25)) )

neg <- tmp$quantity[tmp$quantity <0]
tmp.pred <-  cbind(tmp,neg)

y <- ggplot(tmp, aes(time, quantity)) + 
 geom_line() +   
 geom_line(data=tmp.pred, aes(y=neg),color="red") 
 y

在此处输入图片说明

A couple of methods I think get you close to what you're after.我认为有几种方法可以让您接近您所追求的目标。

library(ggplot2)

set.seed(123)
tmp <- data.frame(time = 1:10, quantity = round(runif(10, -15, 25)) )

method 1 - colour line by expression方法 1 - 按表达式着色线条

This will colour the line from the point the quantity is negative, until the time the quantity is non-negative again.这将从数量为负的点开始为线条着色,直到数量再次为非负的时间。

ggplot(tmp, aes(time, quantity, colour=(quantity <= 0))) + 
geom_line(aes(group=1))

在此处输入图片说明

Method 2 - adding an extra column to so we can colour two lines, one is the complete 'quantity' column, and the other is only those values that are negative, or either size of a negative.方法 2 - 添加一个额外的列,以便我们可以为两行着色,一个是完整的“数量”列,另一个是负值或负值的任一大小。 (otherwise they'll be NA) (否则他们将成为 NA)

library(dplyr)
tmp <- tmp %>%
    mutate(neg = ifelse(quantity <= 0, quantity, NA)) %>%
    mutate(neg = ifelse(is.na(neg) & lag(neg) <= 0, quantity, neg)) %>%
    mutate(neg = ifelse(is.na(neg) & lead(neg) <= 0, quantity, neg))

ggplot() +
    geom_line(data=tmp, aes(x=time, y=quantity) , colour="black") +
    geom_line(data=tmp, aes(x=time, y=neg), colour="red")

在此处输入图片说明

If you want it to only change colour when the line crosses the x==0 line you may have to manipulate the data a bit more如果您希望它仅在线穿过x==0线时改变颜色,您可能需要更多地操作数据

Does this do what you want?这是你想要的吗?

library(ggplot2)
set.seed(1)
tmp <- data.frame(time = 1:10, quantity = round(runif(10, -15, 25)) )
tmp$series <- ifelse(tmp$quantity < 0, "Negative", "Positive")
y <- ggplot(tmp, aes(x = time, y = quantity, colour = series)) + 
  geom_line() +
  scale_colour_manual(values=c("red", "blue"))
y

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

相关问题 如何在ggplot2中使用颜色条来指示不同的类别? - How do I colour bars in ggplot2 to indicate different categories? 如何将三角形添加到 R 中的 ggplot2 颜色条以指示超出范围的值? - How can I add triangles to a ggplot2 colorbar in R to indicate out of bound values? 如何在 ggplot 中指示箱线图异常值的频率? - How can I indicate the frequency of outliers of boxplots in ggplot? 如何在 ggplot2 中以不同颜色在箱线图上添加特定点? - How can I add specific point on boxplot in ggplot2 in different colour? 如何在 R 中使用 ggplot 生成散点图,其中每列都是不同的颜色? - How can I produce a scatterplot using ggplot in R where each column is a different colour? 使用r和ggplot如何指定特定的十六进制颜色/颜色值来表示不同的字符值 - Using r and ggplot How do I specify specific hexadecimal color/colour values to use to represent different character values ggplot 用于具有不同颜色的负值的直方图 - ggplot for a histogram with negative values in different color 如何使用 ggplot 在闪亮中添加反应性 y 轴标签 - How can I add the reactive y axis labels in shiny with ggplot 如何使用 ggplot2 在连续轴上指示组(例如染色体)? - How can I indicate groups (such as chromosomes) on a continuous axis using ggplot2? 如何在ggplot中对正值和负值进行排序 - How to order both positive and negative values in ggplot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM