2013-11-26 12 views
6

मैं ggplot2 में एक साजिश बना रहा हूं जिसमें geom_pointrange और geom_line शामिल है। मैं देखता हूं कि जब मैं भूगर्भों का क्रम बदलता हूं, तो या तो रेखा रेखा के शीर्ष पर या इसके विपरीत बिंदुओं को प्लॉट किया जाता है। पौराणिक कथाओं में भी परिवर्तन होता है जो भूगर्भ के उसी क्रम के आधार पर दूसरे के शीर्ष पर प्लॉट किया जाता है। हालांकि, मैं पहले प्लॉट करने के लिए लाइन चाहता हूं, फिर पौराणिक कथाओं के विपरीत, साजिश में शीर्ष पर पॉइंट्रेंज। क्या यह संभव है? मैं किसी भी इनपुट की सराहना करता हूं।ggplot2 - मैं केवल किंवदंती में भूगोल प्लॉटिंग ऑर्डर कैसे बदलूं?

यहां कोड है जिसे मैं आंकड़ा बनाने के लिए उपयोग करता हूं।

md.figd2 <- structure(list(date = c("2013-05-28", "2013-07-11", "2013-09-22", 
"2013-05-28", "2013-07-11", "2013-09-22", "2013-05-28", "2013-07-11", 
"2013-09-22"), trt = structure(c(3L, 3L, 3L, 1L, 1L, 1L, 2L, 
2L, 2L), .Label = c("- Fescue", "- Random", "Control"), class = "factor"), 
means = c(1, 0.921865257043089, 0.793438250521971, 1, 0.878305313846414, 
0.85698797555687, 1, 0.840679145697309, 0.798547331410388 
), mins = c(1, 0.87709562979756, 0.72278951032918, 1, 0.816185624483356, 
0.763720265496049, 1, 0.780804129401513, 0.717089626439849 
), maxes = c(1, 0.966634884288619, 0.864086990714762, 1, 
0.940425003209472, 0.950255685617691, 1, 0.900554161993105, 
0.880005036380927)), .Names = c("date", "trt", "means", "mins", 
"maxes"), row.names = c(NA, 9L), class = "data.frame") 

library(ggplot2) 
dplot1.ysc <- scale_y_continuous(limits=c(0,1), breaks=seq(0,1,.2), name='Proportion mass lost') 
dplot1.xsc <- scale_x_date(limits=as.Date(c('2013-05-23', '2013-10-03')), labels=c('May 28', 'July 11', 'Sep 22'), breaks=md.figdata$date, name='Date') 
dplot1.csc <- scale_color_manual(values=c('grey20','grey50','grey80')) 
dplot1.lsc <- scale_linetype_manual(values=c('solid','dotted','dashed')) 
djitter <- rep(c(0,-1,1), each=3) 

# This one produces the plot with the legend I want. 
dplot1b <- ggplot(md.figd2, aes(x=date + djitter, y=means, group=trt)) + geom_pointrange(aes(ymin=mins, ymax=maxes, color=trt), size=2) + geom_line(aes(linetype=trt), size=1) 
# This one produces the plot with the points on the main plot that I want. 
dplot1b <- ggplot(md.figd2, aes(x=date + djitter, y=means, group=trt)) + geom_line(aes(linetype=trt), size=1) + geom_pointrange(aes(ymin=mins, ymax=maxes, color=trt), size=2) 

dplot1b + dplot1.xsc + dplot1.ysc + dplot1.csc + dplot1.lsc 

उत्तर

3

त्वरित और गंदे निकाला है: एक और geom_line whi जोड़ने ch ylim के बाहर डेटा का उपयोग करता है, और इसे साजिश आदेश में geom_pointrange के बाद रखता है।

# some data for the 'fake' line 
df <- data.frame(date = rep(as.Date(c('2013-05-23', '2013-10-03')), 3), 
       means = 1.5, trt = c("- Fescue", "- Random", "Control")) 

# insert fake line after geom_pointrange 
ggplot(md.figd2, aes(x = date, y = means, group = trt)) + 
    geom_line(aes(linetype = trt), size = 1) + 
    geom_pointrange(aes(ymin = mins, ymax = maxes, color = trt), 
        position = position_dodge(width = 5), size = 2) + 
    geom_line(data = df, aes(linetype = trt), size = 1) + 
    scale_y_continuous(limits = c(0,1), breaks = seq(0,1, 0.2), name = 'Proportion mass lost') + 
    scale_x_date(limits = as.Date(c('2013-05-23', '2013-10-03')), 
       name = 'Date') + 
    scale_color_manual(values = c('grey20','grey50','grey80')) + 
    scale_linetype_manual(values = c('solid','dotted','dashed')) 

enter image description here

कृपया क्योंकि आंशिक रूप से object 'md.figdata' not found ध्यान दें, कि मैं अपने उदाहरण में साजिश के अपने अनुरूपण में से कुछ को हटा दिया।

4

आप gtable::gtable_filter उपयोग कर सकते हैं साजिश आप चाहते हैं से कथा को निकालने के लिए, और फिर gridExtra::grid.arrange साजिश आप चाहते हैं

# the legend I want 
plot1a <- ggplot(md.figd2, aes(x=date , y=means, group=trt)) + 
    geom_pointrange(aes(ymin=mins, ymax=maxes, color=trt), size=2, 
         position = position_dodge(width=1)) + 
    geom_line(aes(linetype=trt), size=1) 
# This one produces the plot with the points on the main plot that I want. 
dplot1b <- ggplot(md.figd2, aes(x=date, y=means, group=trt)) + 
    geom_line(aes(linetype=trt), size=1) + 
    geom_pointrange(aes(ymin=mins, ymax=maxes, color=trt), size=2) 

w <- dplot1b + dplot1.xsc + dplot1.ysc + dplot1.csc + dplot1.lsc 
# legend 
l <- dplot1a + dplot1.xsc + dplot1.ysc + dplot1.csc + dplot1.lsc 

library(gtable) 
library(gridExtra) 
# extract legend ("guide-box" element) 
leg <- gtable_filter(ggplot_gtable(ggplot_build(l)), 'guide-box') 
# plot the two components, adjusting the widths as you see fit. 
grid.arrange(w + theme(legend.position='none'),leg,ncol=2, widths = c(3,1)) 

एक वैकल्पिक बस भूखंड आप चाहते हैं में कथा को बदलने के लिए है पुनः बनाना कथा के साथ आप चाहते हैं कि आप (gtable_filter का उपयोग कर)

# create ggplotGrob of plot you want 
wGrob <- ggplotGrob(w) 
# replace the legend 
wGrob$grobs[wGrob$layout$name == "guide-box"][[1]] <- leg 
grid.draw(wGrob) 
संबंधित मुद्दे