2012-07-11 11 views
7

मैं आर में ggplot2 का उपयोग कर रहा चाहते हैं पता करने के लिए में टिक के निशान दिखाने के लिए कि कैसे एक बार श्रृंखला बनाने के लिए कर रहा हूँ खड़ी लाइनों रखने के लिए आर के ggplot2 बताने के लिए X- अक्ष केवल महीनों कि नाम से पुकारा जाता के लिए (उदाहरण के लिए मार्च 07, मार्च 08, आदि) हर एक महीने के लिए खड़ी ग्रे लाइनों रखते हुए।कैसे x- अक्ष के कुछ मूल्यों के लिए टिक के निशान डाल दिया और अभी भी अन्य मूल्यों

मुख्य कारण है, क्योंकि हर महीने के लिए एक टिक मार्क होने यह मुश्किल पता करने के लिए जो एक लेबल के अनुरूप बना देता है।

यहाँ एक साजिश का एक उदाहरण है:

See how ticks on the x-axis make it hard to know where is each month

यहाँ के पीछे आर के लाइन है:

ggplot(timeseries_plot_data_mean,aes(as.numeric(project_date)))+ 
    geom_line(aes(y=num_views))+geom_point(aes(y=num_views))+ 
    stat_smooth(aes(y=num_views),method="lm")+ 
    scale_x_continuous(breaks = xscale$breaks, labels = xscale$labels)+ 
    opts(title="Monthly average num views")+xlab("months")+ylab("num views") 

यह उत्पन्न करने के लिए चाहते हैं क्या है। कैसे टिक सही महीने के लेबल से ऊपर स्थान दिया जाता है और ऊर्ध्वाधर लाइनों अभी भी वहाँ दिखा रहे हैं हर महीने मिलते हैं।

This is what would like to generate (Inkscape, image editor,strangely replaced the dots for q's

मैं मैन्युअल इंकस्केप का उपयोग करके उपरोक्त साजिश, (उपेक्षा क्ष के, इंकस्केप अजीब क्ष के लिए डॉट्स की जगह)

उत्तर

8

यहाँ एक समाधान scale_x_date() की minor_breaks पैरामीटर का उपयोग है का संपादन किया। इस का उपयोग करने के लिए, अपने एक्स-मूल्यों वर्ग Date बजाय numeric का होना चाहिए।

library(ggplot2) 
set.seed(123) 

x <- seq(as.Date("2007/3/1"), as.Date("2012/4/1"), by = "1 month") 
y <- ((exp(-10 * seq(from=0, to=1, length.out=length(x))) * 120) + 
     runif(length(x), min=-10, max=10)) 

dat <- data.frame(Months=x, Views=y) 

x_breaks <- seq(as.Date("2007/3/1"), as.Date("2012/4/1"), by="1 year") 
x_labels <- as.character(x_breaks, format="%h-%y") 

plot_1 <- ggplot(dat, aes(x=Months, y=Views)) + 
      theme_bw() + 
      geom_line() + 
      geom_point() + 
      scale_x_date(breaks=x_breaks, labels=x_labels, minor_breaks=dat$Months) 

png("plot_1.png", width=600, height=240) 
print(plot_1) 
dev.off() 

plot_1.png

संबंधित मुद्दे