2014-09-11 7 views
8

मैं ggplot2 के लिए नया हूं, और मैं ggplot2 के साथ filled.contour का उपयोग करके बनाए गए ग्राफ को दोहराने की कोशिश कर रहा हूं।full.contour बनाम ggplot2 + stat_contour

नीचे

मेरी कोड है:

require(ggplot2) 
require(reshape2) 

#data prep 
scale <- 10 

xs <- scale * c(0, 0.5, 0.8, 0.9, 0.95, 0.99, 1) 
ys <- scale * c(0, 0.01, 0.05, 0.1, 0.2, 0.5, 1) 

df <- data.frame(expand.grid(xs,ys)) 
colnames(df) <- c('x','y') 
df$z <- ((scale-df$x) * df$y)/((scale-df$x) * df$y + 1) 

#filled contour looks good 
filled.contour(xs, ys, acast(df, x~y, value.var='z')) 

#ggplot contour looks bad 
p <- ggplot(df, aes(x=x, y=y, z=z)) 

p + stat_contour(geom='polygon', aes(fill=..level..)) 

मैं समझ नहीं कैसे ggplot समोच्च ऊपरी बाएं हाथ की ओर करने के लिए सभी तरह से बहुभुज को भरने के लिए प्राप्त करने के लिए (वहाँ पर एक बात है (0,10) के साथ z = 0.99) ... सब मैं इन अजीब त्रिकोण हैं

उत्तर

3

filled.contour प्लॉट के एक ggplot संस्करण बनाने के लिए आप अपने उदाहरण में df वस्तु तुलना में एक बड़ा data.frame की आवश्यकता होगी और geom_tile का उपयोग कर उत्पादन करेगा वह साजिश जिसे आप ढूंढ रहे हैं। निम्नलिखित पर विचार करें:

# a larger data set 
scl <- 10 
dat <- expand.grid(x = scl * seq(0, 1, by = 0.01), 
        y = scl * seq(0, 1, by = 0.01)) 
dat$z <- ((scl - dat$x) * dat$y)/((scl - dat$x) * dat$y + 1) 

# create the plot, the geom_contour may not be needed, but I find it helpful 
ggplot(dat) + 
aes(x = x, y = y, z = z, fill = z) + 
geom_tile() + 
geom_contour(color = "white", alpha = 0.5) + 
scale_fill_gradient(low = "lightblue", high = "magenta") + 
theme_bw()