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).
Data:
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)
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")
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...
here what I tried:
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.
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
could not find function "getSymbols"
. Please make your example reproducible. – Axemanquantmod
package. I've added the required packages in the example to make it reproducible. – shadow