ggplot

2012-11-20 17 views
5

पर पैराबॉलिक संदर्भ रेखाएं मैं अपने प्लॉट को ggplot में ले जा रहा हूं। लगभग हो गया है इस एक को छोड़कर (कोड इस previous question से मिल गया):ggplot

ggplot plot should look like this

#Set the bet sequence and the % lines 
betseq <- 0:700 #0 to 700 bets 
perlin <- 0.05 #Show the +/- 5% lines on the graph 

#Define a function that plots the upper and lower % limit lines 
dralim <- function(stax, endx, perlin) { 
    lines(stax:endx, qnorm(1-perlin)*sqrt((stax:endx)-stax)) 
    lines(stax:endx, qnorm(perlin)*sqrt((stax:endx)-stax)) 
} 

#Build the plot area and draw the vertical dashed lines 
plot(betseq, rep(0, length(betseq)), type="l", ylim=c(-50, 50), main="", xlab="Trial Number", ylab="Cumulative Hits") 
abline(h=0) 
abline(v=35, lty="dashed") #Seg 1 
abline(v=185, lty="dashed") #Seg 2 
abline(v=385, lty="dashed") #Seg 3 
abline(v=485, lty="dashed") #Seg 4 
abline(v=585, lty="dashed") #Seg 5 

#Draw the % limit lines that correspond to the vertical dashed lines by calling the 
#new function dralim. 
dralim(0, 35, perlin) #Seg 1 
dralim(36, 185, perlin) #Seg 2 
dralim(186, 385, perlin) #Seg 3 
dralim(386, 485, perlin) #Seg 4 
dralim(486, 585, perlin) #Seg 5 
dralim(586, 701, perlin) #Seg 6 

मैं दिखा सकता है कि जहाँ तक मुझे मिल गया है (दूर नहीं):

ggplot(a, aes(x=num,y=s, colour=ss)) +geom_line() +stat_smooth(method="lm", formula="y~poly(x,2)") 

my attempt

स्पष्ट होने के लिए। मैं संदर्भ डेटा (शीर्ष छवि) पर अपना डेटा प्लॉट कर रहा हूं। निचली छवि संदर्भ डेटा प्राप्त करने पर मेरा डेटा और मेरा खराब प्रयास दिखाती है (जो स्पष्ट रूप से काम नहीं किया है)।

उत्तर

3

आप जो कर रहे थे वह पहले से परिभाषित पैराबोला को चित्रित नहीं करते हुए आपके डेटा पर पैराबोला फिट कर रहा था। ggplot पर आपके अनुकूलन को अनुकूलित करना बहुत मुश्किल नहीं है।

ही शुरू आप था के रूप में (हालांकि betseq वास्तव में कहीं भी नहीं किया जाता है)

#Set the bet sequence and the % lines 
betseq <- 0:700 #0 to 700 bets 
perlin <- 0.05 #Show the +/- 5% lines on the graph 

नहीं एक समारोह जो लाइनों ड्रॉ के बजाय, एक समारोह जो रिटर्न (किसी सूची में) geom_line रों कि आप क्या कर रहे हैं चाहते हैं। एक अंतर्निहित aes(x=x, y=y) है जो बाद में ggplot घोषणा में दिया जाएगा, लेकिन यह पैराबोलस बनाने वाले डेटा बिंदुओं को परिभाषित करता है।

#Define a function that plots the upper and lower % limit lines 
dralim <- function(stax, endx, perlin) { 
    c(geom_line(data = data.frame(x=stax:endx, 
           y=qnorm(1-perlin)*sqrt((stax:endx)-stax))), 
    geom_line(data = data.frame(x=stax:endx, 
           y=qnorm(perlin)*sqrt((stax:endx)-stax)))) 
} 

पुनरावृत्ति बचाने के लिए, खड़ी लाइनों (edges) है, जो भी परवलय (ranges) के बाएँ और दाएँ अंतिम बिंदुओं को परिभाषित करने के लिए किया जा सकता की स्थिति को परिभाषित।

edges <- data.frame(x=c(0, 35, 185, 285, 485, 585, 700)) 
ranges <- data.frame(left = edges$x[-nrow(edges)], 
        right = edges$x[-1] + 1) 

अब ggplot बनाएं। सभी लंबवत रेखाओं को आकर्षित करने के लिए एक एकल geom_vline (चूंकि हमने एकल डेटासेट में स्थितियों को परिभाषित किया है)। असामान्य चरण ranges की पंक्ति (सूचकांक) पर लूपिंग है और संबंधित बाएं और दाएं मान (और perlin) के साथ dralim पर कॉल कर रहा है। यह geom_lines की सूचियों की एक सूची देता है, लेकिन इसे सामान्य रूप से एक साजिश में जोड़ा जा सकता है और सभी पंक्तियां जुड़ जाती हैं। अंतिम दो स्केल कॉल केवल वाई अक्ष के मामले में, लेबल सेट करने के लिए हैं।

ggplot(mapping=aes(x=x, y=y)) + 
    geom_vline(data=edges, aes(xintercept = x), linetype="dashed") + 
    lapply(seq_len(nrow(ranges)), 
     function(r) {dralim(ranges$left[r], ranges$right[r], perlin)}) + 
    scale_y_continuous("Cumulative Hits", lim=c(-50,50)) + 
    scale_x_continuous("Trial Number") 

enter image description here

+0

मैं कर यह गलत था मेरे रास्ते को समझा। कहीं शुरू करना था। आपका बहुत अच्छा लग रहा है। धन्यवाद। इसे जाने दो! –

+0

ठीक है कि यह बहुत अच्छा लग रहा है। लेकिन मैं अपने डेटा को शीर्ष पर प्लॉट करने के बारे में कैसे जा सकता हूं? –

+0

अपने डेटा के प्रारूप को नहीं जानते हैं, लेकिन जो आपने दिखाया है उससे अनुमान लगाते हुए, 'geom_line (डेटा = ए, एईएस (x = num, y = s, color = ss) जोड़ें'। –