2017-03-01 25 views
8

एक श्रेणी के भीतर वाई अक्ष रेखा को नियंत्रित करना यह आधार का उपयोग कर रहा है, जहां मैं एक्स और वाई धुरी रेंज को नियंत्रित कर सकता हूं, जहां वास्तव में रेखा खींची जानी चाहिए।इसे ggplot में साजिश करें।

plot(mtcars$mpg, mtcars$hp, ylim = c(0, 400), xlim = c(0, 50), axes = F, xlab = 'mpg', ylab = 'hp', pch = 16) 
axis(side = 2, at = seq(100, 400, 100)) 
axis(side = 1, at = seq(10, 30, 10)) 

enter image description here

ggplot(data = mtcars, aes(x = mpg, y = hp))+geom_point()+ 
theme(panel.background = element_blank())+ 
scale_x_continuous(breaks = seq(10, 30, 10), limits = c(0, 50))+ 
scale_y_continuous(breaks = seq(100, 400, 100), limits = c(0, 400)) 

enter image description here

कैसे मैं बिल्कुल आधार साजिश की तरह अक्ष लाइन जोड़ सकता हूँ? मैंने scale_y_continuous और scale_x_continuous की कोशिश की है लेकिन यह हमेशा साजिश के अंत तक खींचती है।

उत्तर

6

आप ggthemes पैकेज का उपयोग कर सकते हैं:

ggplot(data = mtcars, aes(x = mpg, y = hp))+geom_point()+ 
    geom_rangeframe(data = data.frame(mpg = c(10, 30), hp = c(100, 400))) + 
    theme_tufte() + 
    scale_x_continuous(breaks = seq(10, 30, 10), limits = c(0, 50))+ 
    scale_y_continuous(breaks = seq(100, 400, 100), limits = c(0, 400)) 

enter image description here

आप भी उन्हें मैन्युअल रूप से आकर्षित कर सकते हैं अगर आप चाहते हैं:

ggplot(data = mtcars, aes(x = mpg, y = hp))+geom_point()+ 
    geom_segment(aes_all(c('x', 'y', 'xend', 'yend')), 
       data = data.frame(x = c(0, 10), xend = c(0, 30), y = c(100, 0), yend = c(400, 0))) + 
    theme(panel.background = element_blank()) + 
    scale_x_continuous(breaks = seq(10, 30, 10), limits = c(0, 50), expand = c(0, 0))+ 
    scale_y_continuous(breaks = seq(100, 400, 100), limits = c(0, 400), expand = c(0, 0)) 

enter image description here

+0

धन्यवाद , मैं पहले कोशिश की है (theme_hc()) का उपयोग कर। लेकिन मैं ggthemes का उपयोग करके इसे बाहर करना चाहता हूं। क्या यह संभव है ? – PoisonAlien

+0

मुझे लगता है कि पैमाने का विस्तार करने के दौरान आप मैन्युअल रूप से सेगमेंट खींच सकते हैं। – Axeman

+0

हाँ, यही वह है जो मुझे दिमाग में था। मैं कोशिश करूँगा। यह आश्चर्य की बात है कि इस परिपक्व होने के लिए ggplot इस के लिए कोई विकल्प नहीं है। – PoisonAlien