2015-02-18 11 views
6

Ich erzeuge die folgenden zwei Linien mit ggplot und möchte einen bestimmten Bereich zwischen den beiden Linien schattieren, dh wo y = x² ist größer als y = 2x, wo 2 < = x < = 3 .Schattenbereich zwischen zwei Linien mit ggplot

# create data # 

x<-as.data.frame(c(1,2,3,4)) 
colnames(x)<-"x" 
x$twox<-2*x$x 
x$x2<-x$x^2 

# Set colours # 

blue<-rgb(0.8, 0.8, 1, alpha=0.25) 
clear<-rgb(1, 0, 0, alpha=0.0001) 

# Define region to fill # 

x$fill <- "no fill" 
x$fill[(x$x2 > x$twox) & (x$x <= 3 & x$x >= 2)] <- "fill" 

# Plot # 

ggplot(x, aes(x=x, y=twox)) + 
    geom_line(aes(y = twox)) + 
    geom_line(aes(y = x2)) + 
    geom_area(aes(fill=fill)) + 
    scale_y_continuous(expand = c(0, 0), limits=c(0,20)) + 
    scale_x_continuous(expand = c(0, 0), limits=c(0,5)) + 
    scale_fill_manual(values=c(clear,blue)) 

das Ergebnis ist die folgende, die nur den Bereich unter der Linie y = 2x beschattet, und dies unabhängig von der x-Wert - warum?

enter image description here

+1

http://www.r-bloggers.com/shading-between-two-lines-ggplot/ – CMichael

+0

Mögliche Betrogene: http://stackoverflow.com/q/20260749/903061 – Gregor

Antwort

10

Wie wäre es mit geom_ribbon statt

ggplot(x, aes(x=x, y=twox)) + 
    geom_line(aes(y = twox)) + 
    geom_line(aes(y = x2)) + 
    geom_ribbon(data=subset(x, 2 <= x & x <= 3), 
      aes(ymin=twox,ymax=x2), fill="blue", alpha="0.5") + 
    scale_y_continuous(expand = c(0, 0), limits=c(0,20)) + 
    scale_x_continuous(expand = c(0, 0), limits=c(0,5)) + 
    scale_fill_manual(values=c(clear,blue)) 

plot

+0

Funktioniert geom_ribbon immer für diese Art von Aufgabe? http://www.r-bloggers.com/shading-between-two-lines-ggplot/ sagt es nicht. – CMichael

+1

Es gibt ein Problem, wenn Sie in Ihrem data.frame keinen Schnittpunkt haben. Beispiel: x <- seq (0,5, durch = 0,2); df <- Datenrahmen (x = x, l1 = 5-x, l2 = x); Bibliothek (ggplot2); ggplot (df, aes (x = x)) + geom_line (aes (y = l1)) + geom_line (aes (y = l2)) + geom_ribbon (aes (ymin = pmin (l1, l2), ymax = pmax (l1 , l2)), fill = "blau", alpha = 0,5); –

+0

Danke für diese Illustration! – CMichael

2

denke ich, dass geom_ribbon ist der Weg zu gehen. Es gibt 2 Schritte zu gehen:

  1. Datenmanipulation: Sie Daten manipulieren sollte ymin & ymax für Argumente in geom_ribbon
  2. Draw Grundstück mit geom_ribbon zu definieren.

sie mein Beispiel sehen:

#Data 
library(gcookbook) 
# Data Manipulation 
cb <-subset(climate,Source=="Berkeley") 
cb$valence[cb$Anomaly10y >= 0.3] <- "pos" 
cb$valence[cb$Anomaly10y < 0.3] <- "neg" 
cb$min <- ifelse(cb$Anomaly10y >= 0.3, 0.3, cb$Anomaly10y) 
cb$max <- ifelse(cb$Anomaly10y >= 0.3, cb$Anomaly10y, 0.3) 

#Drawing plot 
ggplot(cb,aes(x=Year,y=Anomaly10y)) + 
geom_ribbon(aes(ymin = min, ymax = max, fill = valence), alpha = 0.75) + 
scale_fill_manual(values = c("blue", "orange")) + 
geom_line(aes(col = valence), size = 1) + 
scale_color_manual(values = c("blue", "orange")) + 
geom_hline(yintercept=0.3, col = "blue") + 
theme_bw() 
Verwandte Themen