2017-08-07 17 views
2

geom_area()の開始yレベルをゼロからずらしたいと思います。これは、次の例では第一の関数呼び出しは大丈夫ですが、どのように私は2回目の呼び出しのためのプロット是正することができますgeom_area()のゼロ以外の開始値を定義する

library(ggplot2) 

set.seed(123) 

myplot <- function (split.value) { 
    year <- (2000:2017) 
    index <- rnorm(18, split.value,5) 
    interp <- approx(year, index, n=1000) 
    df <- data.frame(year = interp$x, 
        index = interp$y, 
        valence = ifelse(interp$y <= split.value, "pos", "neg"), 
        stringsAsFactors = FALSE) 
    ggplot(df, aes(x = year, y = index)) + 
    geom_area(aes(fill = valence), alpha = 0.5) + 
    geom_line() + 
    geom_hline(yintercept = split.value) + 
    scale_fill_manual(values=c("green", "red"), guide=FALSE) + 
    scale_x_continuous(expand=c(0, 0)) 
} # cf. R Graphics Cookbook 

myplot(0) # o.k. (with more interpolation points) 
# but the following does not work: 

myplot(2) # .. would like to shift green-area-MIN-y-value to 2 
# and red-area-MAX-y to 2 

答えて

1

geom_areaをゼロにyminを修正geom_ribbonの特殊なケースです。したがって、あなたのgeom_areaの代わりに次のものが使用されます

geom_ribbon(aes(ymin=split.value, ymax=index, fill = valence), alpha = 0.5) + 
関連する問題