如何使用ggplot2填充values(geom_line)和拦截之间的空间?截距上下截取的颜色不同 [英] How can I fill the space between values(geom_line) and an intercept with ggplot2? Different Colors for values over and under intercept

查看:52
本文介绍了如何使用ggplot2填充values(geom_line)和拦截之间的空间?截距上下截取的颜色不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot2绘制图形,其中我需要将截距(= 1)和值(我通过geom_line连接的值)之间的空间/面积设置为红色(如果值小于1)或绿色(如果值大于1).数据来自微软(自1999年以来的价格表现).

I want to do a graph with ggplot2, where I need the space/area between the intercept (=1) and the values (which I connected through geom_line) to be red (if the values are lower than 1) or green (if the values are bigger than 1). The data is from microsoft (price performance since 1999).

数据:

require(quantmod)
require(dplyr)
require(ggplot2)

getSymbols("MSFT", from ="1999-01-01")
microsoft <- data.frame(time(MSFT), MSFT[,6]) 
microsoft$time <- as.Date(microsoft$time.MSFT., "%Y-%m-%d")

microsoft <- microsoft %>%
mutate(change = MSFT.Adjusted - first(MSFT.Adjusted),
     change.pc = change/first(MSFT.Adjusted)+1) 

这是我到目前为止拥有的ggplot:

that is the ggplot I have so far:

ggplot(microsoft, aes(x = time, y = change.pc)) +
  geom_line(stat = "identity") + 
  geom_hline(aes(yintercept=1), color="black") + 
  theme_bw() + 
  xlab("Jahr") + ylab("") + 
  ggtitle("Microsoft Kursentwicklung seit Januar 1999")

我想用绿色填充y = 1和上面的值之间的空间,用红色填充y = 1和下面的值之间的空间.我尝试了geom_ribbon,geom_area,geom_polynom,但没有任何效果.最大的问题是,它填充了绿色空间,但不在y = 1上方但也在其下方在线.甚至看不到的红色...

I want to fill the space between y = 1 and the values above in green, and the space between y = 1 and the values under in red. I tried geom_ribbon, geom_area, geom_polynom, but nothing worked. The biggest problem is, that it fills the space green, but not online above y = 1 but also under. and the red you can't even see...

这是我尝试过的:

geom_area(data = subset(microsoft, change.pc > 1), fill = "green", alpha =0.5)
geom_area(data = subset(microsoft, change.pc < 1), fill = "red", alpha = 0.5) 

我将它们放在绘图中的线上,然后出现了我上面描述的问题.

I put these to lines in my plot, and then the problem I described above appeared.

我还尝试了其他方法(可在stackoverflow.com上找到):

Among other things I also tried this (found here on stackoverflow.com):

microsoft$grp <- "orig"
microsoft <- microsoft[order(microsoft$time),]
microsoft_new <- do.call("rbind",
             sapply(1:(nrow(microsoft) -1), function(i){
               f <- lm(time ~ change.pc, microsoft[i:(i+1), ])
               if (f$qr$rank < 2) return(NULL)
               r <- predict(f, newdata = data.frame(change.pc = 0))
               if(microsoft[i, ]$time < r & r < microsoft[i+1, ]$time)
                 return(data.frame(time = r, change.pc = 0))
               else return(NULL)
             })
)
microsoft_2 <- rbind(microsoft, microsoft_new)  
ggplot(microsoft_2, aes(x = time, y = change.pc)) +
  geom_area(data = subset(microsoft_2, change.pc <= 1), fill = "red") +
  geom_area(data = subset(microsoft_2, change.pc >= 1), fill = "blue") +
  scale_x_continuous("", expand = c(0,0), breaks = seq(1999, 2017, 3)) +
  theme_bw()

那也不起作用.有谁知道我如何实现我所需要的?这就是它的外观

That didn't work either. Does anyone has an idea how I could achieve what I need? This is how it should look

推荐答案

我无法使您的数据正常工作,但是使用一些组合数据,以下方法看起来像您的示例:

I couldn't get your data to work, but using some made up data, the following approach looks like you're example:

library(ggplot2)
set.seed(0)
microsoft <- data.frame(date=1:1000, y=cumsum(runif(1000)-0.5))

ggplot(microsoft, aes(x=date,y=y)) +
geom_ribbon(aes(ymin=pmin(microsoft$y,0), ymax=0), fill="red", col="red", alpha=0.5) +
geom_ribbon(aes(ymin=0, ymax=pmax(microsoft$y,0)), fill="green", col="green", alpha=0.5) +
geom_line(aes(y=0))

这篇关于如何使用ggplot2填充values(geom_line)和拦截之间的空间?截距上下截取的颜色不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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