2011-08-30 32 views
10

में आंशिक धराशायी रेखा बनाएं मैं आर में एक साजिश बना रहा हूं और एक लाइन बनाने की आवश्यकता है जहां कुछ मूल्य अनुमान हैं। अनुमानों को एक धराशायी रेखा के रूप में दर्शाया जाता है। कोड यह रहा:ggplot2

df = data.frame(date=c(rep(2008:2013, by=1)), 
       value=c(303,407,538,696,881,1094)) 


ggplot(df, aes(date, value, width=0.64)) + 
     geom_bar(stat = "identity", fill="#336699", colour="black") + 
     ylim(c(0,1400)) + opts(title="U.S. Smartphone Users") + 
     opts(axis.text.y=theme_text(family="sans", face="bold")) + 
     opts(axis.text.x=theme_text(family="sans", face="bold")) + 
     opts(plot.title = theme_text(size=14, face="bold")) + 
     xlab("Year") + ylab("Users (in millions)") +   
     opts(axis.title.x=theme_text(family="sans")) + 
     opts(axis.title.y=theme_text(family="sans", angle=90)) + 
     geom_segment(aes(x=2007.6, xend=2013, y=550, yend=1350), arrow=arrow(length=unit(0.4,"cm"))) 

तो मैं एक लाइन जो 2008 से करने के लिए 2013 हालांकि, मैं 2008 से 2011 तक एक ठोस लाइन चाहते फैली हुई है, और अंत करने के लिए 2011 से एक चित्तीदार लाइन बना लिया है। क्या मैं सिर्फ दो अलग-अलग रेखा खंड करता हूं, या वांछित परिणाम प्राप्त करने के लिए मैं एक अलग आदेश का उपयोग कर सकता हूं।

उत्तर

20

ggplot दार्शनिक सरल है। एक साजिश के प्रत्येक तत्व को एक अलग परत पर होना चाहिए। इस प्रकार विभिन्न लाइन प्रकारों में दो लाइन सेगमेंट प्राप्त करने के लिए, आपको दो geom_segment कथन की आवश्यकता है।

मैं आपकी अलग-अलग अवधि के लिए विभिन्न रंगों में geom_bar के साथ एक ही सिद्धांत को चित्रित करता हूं।

ggplot(df[df$date<=2011, ], aes(date, value, width=0.64)) + 
    geom_bar(stat = "identity", fill="#336699", colour="black") + 
    geom_bar(data=df[df$date>2011, ], aes(date, value), 
     stat = "identity", fill="#336699", colour="black", alpha=0.5) + 
    ylim(c(0,1400)) + opts(title="U.S. Smartphone Users") + 
    opts(
     axis.text.y=theme_text(family="sans", face="bold"), 
     axis.text.x=theme_text(family="sans", face="bold"), 
     plot.title = theme_text(size=14, face="bold"), 
     axis.title.x=theme_text(family="sans"), 
     axis.title.y=theme_text(family="sans", angle=90) 
    ) + 
    xlab("Year") + ylab("Users (in millions)") +   
    geom_segment(aes(x=2007.6, xend=2011, y=550, yend=1050), linetype=1) + 
    geom_segment(aes(x=2011, xend=2013, y=1050, yend=1350), 
     arrow=arrow(length=unit(0.4,"cm")), linetype=2) 

enter image description here