2016-03-14 21 views
0

我想做一個情節像這個圖像what I want,但我不知道如何。我編寫了下面的代碼,但我沒有找到獲取該圖的方法。這裏的要點是將密度線添加到我的原始繪圖(Relation Masa-SFR)中,密度應該是每0.3 x。我的意思是從7到7.3的一行,從7.3到7.6的下一行,等等。用下面的代碼(繼續下去,直到X = 12),我得到這個[劇情] [2]覆蓋許多地塊與x的不同範圍

plot(SFsl$MEDMASS, SFR_SalpToMPA,xlim= range(7:12), 
    ylim= range(-3:2.5),ylab="log(SFR(M(sun)/yr)", 
    xlab="log(M(star)/(M(sun)") 
title("Relacion Masa-SFR") 
par(new=TRUE) 
FCUTsfrsl1=(SFsl$MEDMASS >= 7 & SFsl$MEDMASS <=7.3 & 
    SFR_SalpToMPA < 2 & SFR_SalpToMPA > -3) 
x <- SFR_SalpToMPA[FCUTsfrsl1] 
y <- density(x) 
plot(y$y, y$x, type='l',ylim=range(-3:2.5), col="red", 
    ylab="", xlab="", axes=FALSE) 

我沒有你說什麼,但我得到這個plot,我不知道我做錯了什麼

+0

我覺得這兩個地塊您鏈接的都是同一個。那是你想要的嗎? –

+0

是的,我想要第一個陰謀,但我不知道我該怎麼做,我的意思是我的密度線是從x = 0開始的,我不想那個 – YazminRios

+0

所以你想要計算每個區間的密度0.3單位? –

回答

0

由於我沒有你的數據,我不得不做出一些。如果這樣做你想要的,我認爲你可以適應你的實際數據。

set.seed(7) 
x <- runif(1000, 7, 12) 
y <- runif(1000, -3, 3) 
DF <- data.frame(x = x, y = y) 
plot(DF$x, DF$y) 

# Cut the x axis into 0.3 unit segments, compute the density and plot 
br <- seq(7, 12, 0.333) 
intx <- cut(x, br) # intervals 
intx2 <- as.factor(cut(x, br, labels = FALSE)) # intervals by code 

intx3 <- split(x, intx) # x values 
inty <- split(y, intx2) # corresponding y values for density calc 

for (i in 1:length(intx3)) { 
    xx <- seq(min(intx3[[i]]), max(intx3[[i]]), length.out = 512) 
    lines(xx, density(inty[[i]])$y, col = "red") 
    } 

這將產生以下圖像。您需要仔細觀察,但每0.3單位間隔有一個單獨的密度圖。 enter image description here

編輯更改用於計算密度

set.seed(7) 
x <- runif(1000, 7, 12) 
y <- runif(1000, -3, 3) 
DF <- data.frame(x = x, y = y) 
plot(DF$x, DF$y, xlim = c(7, 15)) 

# Cut the x axis into 0.3 unit segments, compute the density and plot 
br <- seq(7, 12, 0.333) 
intx <- cut(x, br) # intervals 
intx2 <- as.factor(cut(x, br, labels = FALSE)) # intervals by code 

intx3 <- split(x, intx) # x values 
inty <- split(y, intx2) # corresponding y values 

# This gives the density values in the horizontal direction (desired) 
# This is the change, the above is unchanged. 
for (i in 1:length(intx3)) { 
    yy <- seq(min(inty[[i]]), max(inty[[i]]), length.out = 512) 
    offset <- min(intx3[[i]]) 
    lines(density(intx3[[i]])$y + offset, yy, col = "red") 
    } 

這給尺寸:

enter image description here

+0

非常感謝! :) 我剛剛意識到,我上傳了兩次相同的情節,我只是改變了它。你介意看看第一張圖片嗎?這就是我想要的,對不起,我犯了一個錯誤 – YazminRios

+0

看我的編輯。通過編寫數據很難做到這一點,但是,您應該能夠調整腳本。 –

+0

我仍然有問題。你能檢查我的答案嗎?因爲我不知道我怎麼能告訴你我獲得的陰謀 – YazminRios