2013-03-15 65 views
7

使用R,我繪製了類似於this的陰影線。我想要做以下4件事R有關圖的一般問題

  1. 添加圖例如鏈接所示。
  2. 用德爾塔的希臘符號替換x軸名稱
  3. 提示圖上的各個交點。例如,在x = 0.75時,有幾條曲線符合x軸,我想將0.75的值放在該點附近。
  4. 如果看到曲線,它們不光滑。如何讓他們順利?即使excel繪製更平滑的曲線。

如何實現這一目標?

這是情節。

plot

下面的代碼是用於繪製曲線。

plot(NA,xlim=c(0,1),ylim=c(0,1),xlab="delta",ylab="K", xaxs="i",yaxs="i") # Empty plot 
a1 <- curve((x+x^7-x^2-x^4)/(1+x-x^3-x^4), from=0, n=450000, add = TRUE) # First curve 
a2 <- curve((x^2+x^3-x-x^5)/(x+x^2), to=0.9, n=45000, add = TRUE) 
a3 <- curve((x+x^7-x^2-x^4)/(1+x-x^2-x^3-x^4+x^7),from=0, n=45000, add = TRUE) 
a4 <- curve((x+x^8-x^3-x^5)/(x+x^8-x^3-x^5+1-x^4),from=0, to=0.9, n=45000, add = TRUE) 
a5 <- curve((x+x^8-x^3-x^5)/(1+x-x^5-x^4),from=0, n=45000, add = TRUE) 
a6 <- curve((x+x^2-x^4-1)/(x-x^4), to=0.84, n=45000, add = TRUE) 
a7 <- curve((x+x^6-x^3-x^4)/(1+x-x^3-x^4), from=0.83 ,to=1, n=45000, add = TRUE) 
a8 <- curve((1+x^7-x^2-x^4)/(1+x^3-x-x^4), from=0.819, n=45000, add = TRUE) 
a9 <- curve((x)/(1+x), n=45000,from=0.819, to =1, add = TRUE) 


names(a1) <- c('xA1','yA1') 
names(a2) <- c('xA2','yA2') 
names(a3) <- c('xA3','yA3') 
names(a4) <- c('xA4','yA4') 
names(a5) <- c('xA5','yA5') 
names(a6) <- c('xA6','yA6') 
names(a7) <- c('xA7','yA7') 
names(a8) <- c('xA8','yA8') 
names(a9) <- c('xA9','yA9') 


with(as.list(c(a1,a2,a3,a4,a5,a6,a7,a8,a9)),{ 

idA <- yA3 >=0 
idB <- yA2 >=0 & yA2 <= yA4 
idC <- yA4 >= yA2 

idD <- yA5 >=0 

idE <- yA6 >=0 & yA6 <= yA7 
idF <- yA7 <= yA6 

idG <- yA8 >=0 & yA8 <= yA9 
idH <- xA9 >= xA8 & xA9 >0.8 

idI <- xA1 >=0 & xA1 <= 0.755 
idJ <- xA3 >=0 & xA3 <= 0.755 



polygon(x = c(xA3[idA],xA2[idB],rev(xA4[idC])), 
     y = c(yA3[idA],yA2[idB],rev(yA4[idC])), 
     density=20, angle=90, border=NULL) 

polygon(x = c(xA5[idD],1,1,0), 
     y = c(yA5[idD],0,1,1), 
     density=20, angle=0, border=NULL) 

polygon(x = c(xA6,xA7), 
     y = c(yA6,yA7), 
     density=20, angle=45, border=NULL) 

polygon(x = c(rev(xA8[idG]),xA9[idH],1), 
     y = c(rev(yA8[idG]),yA9[idH],0), 
     density=20, angle=135, border=NULL) 

polygon(x = c(xA1[idI],rev(xA3[idJ])), 
     y = c(yA1[idI],rev(yA3[idJ])), 
     col="black", border=NULL) 


}) 
+3

你不能真的希望答案從發佈這個?添加一些如何繪製圖形的細節。如何添加圖例將取決於你用什麼來繪製你的圖(網格,網格,底座等) – 2013-03-15 12:03:02

+0

你爲什麼不把它添加到這個問題? http://stackoverflow.com/questions/15385063/easiest-way-to-plot-inequalities-with-hatched-fill – JT85 2013-03-15 12:19:31

+3

偉大的編輯和不錯的圖形。 +1,以獲得清晰,可重現的問題。今後,請考慮以這種方式構思您的所有問題。 – 2013-03-15 12:37:41

回答

10
layout(matrix(c(1,2),nrow=1), 
     width=c(4,1)) #Divide your plotting region in two inequal part 
par(mar=c(5,4,4,0)) #Get rid of the margin on the right side 
plot(NA,xlim=c(0,1),ylim=c(0,1), 
    xlab=expression(delta),ylab="K", xaxs="i",yaxs="i") # Here's your delta 
a1 <- curve((x+x^7-x^2-x^4)/(1+x-x^3-x^4), from=0, n=450000, add = TRUE) 

... 

par(mar=c(5,0,4,2)) #No margin on the left side 
plot(c(0,1),type="n", axes=F, xlab="", ylab="") #Empty plot 
legend("top",legend=c("1","2","3","4","5"), 
     density=c(20,20,20,20,NA), angle=c(90,0,45,135,NA), 
     col=c(NA,NA,NA,NA,"black"), bty="n", cex=1.5) 

enter image description here

至於要標記的點,或者使用功能text(或mtext)做 「programmaticaly」 或locator以交互方式做到這一點。

編輯:或者(正如我在評論中說的),這將工作,以及把你的傳奇繪圖區之外,可能是簡單的:

par(mar=c(5,4,4,8)) 
plot(NA,xlim=c(0,1),ylim=c(0,1), 
    xlab=expression(delta),ylab="K", xaxs="i",yaxs="i") # Here's your delta 
    a1 <- curve((x+x^7-x^2-x^4)/(1+x-x^3-x^4), from=0, n=450000, add = TRUE) 

... 

legend(1,1,legend=c("1","2","3","4","5"), 
    density=c(20,20,20,20,NA), angle=c(90,0,45,135,NA), 
    col=c(NA,NA,NA,NA,"black"), bty="n", cex=1.5, xpd=TRUE) 
+2

+1!你真棒!我忘了佈局! – agstudy 2013-03-15 12:41:55

+0

謝謝!在這裏使用'layout'的替代方法可能是增加右邊距(例如'par(mar = c(5,4,4,8))')並使用參數'xpd = TRUE'繪製圖例外: legend(1,1,legend = ...,xpd = TRUE)' – plannapus 2013-03-15 12:50:36

+0

其實我想我會改變我的答案,因爲它是一種更簡單的解決方案。 – plannapus 2013-03-15 13:02:53

8

傳統的圖形系統提供了圖例()函數用於添加 圖例或鍵的曲線圖。該圖例通常繪製在繪圖區域 中,並且相對於用戶座標而定位。

的功能有很多爭論,在這裏我們需要使用角度密度參數來區分散列地區。

legend(0.5, 0.8, paste("region", 1:5), 
     density=c(20,20,20,20,0), 
     angle=c(90,0,45,135,0)) 

enter image description here

+0

很快。如何將黑色區域添加到圖例中?還有,如何將傳奇放在劇情區域之外? – 2013-03-15 12:31:07

+0

超出陰謀區域?你的意思是左邊還是右邊? – agstudy 2013-03-15 12:32:24

+0

最好在底部,否則是正確的。也可以將圖例從「從上到下」從左到右寫成「? – 2013-03-15 12:38:12