2014-04-03 25 views
1

我正嘗試在R中使用ggplot2創建一個具有多個線性線的繪圖,其中每行表示一個單獨的個人對環境變化的響應x軸上。使用下面提供的腳本,我可以使這個繪圖沒有問題,但是,我想每個人的每一行的長度代表每個人在下面採樣的數據範圍(有些行顯然比其他行更長)。線長等於數據範圍的多線圖 - GGPlot2

這個鏈接(http://www.intechopen.com/source/html/18476/media/image4.jpeg)會告訴你我是如何想要最終圖看起來像,除了我只需要1幀(不是9),並且線的長度會有所不同。此外,我的X軸上的數據是連續的,而不是離散的。

看來我可能需要使用geom_linerange,但調整ymin和ymax似乎沒有意義,因爲我實際上試圖限制x軸的範圍。

任何幫助,非常感謝!

此代碼將從下面的數據中產生圖。

p <- qplot(dBLevel,LowFreq, group=Male,data=test,geom="line") 

數據子集(「上部」和「下部」表示環境變量的(dBLevel)每個「男」的範圍內下取樣。「LowFreq」是響應變量「dBLevel」。)

test <- structure(list(Male = c(69L, 69L, 69L, 69L, 69L, 113L, 113L, 
113L, 113L, 113L, 126L, 126L, 126L, 126L, 126L, 143L, 143L, 143L, 
143L, 143L, 155L, 155L, 155L, 155L, 155L, 178L, 178L, 178L, 178L, 
178L, 186L, 186L, 186L, 186L, 186L, 193L, 193L, 193L, 193L, 193L 
), dBLevel = c(-20L, -10L, 0L, 10L, 20L, -20L, -10L, 0L, 10L, 
20L, -20L, -10L, 0L, 10L, 20L, -20L, -10L, 0L, 10L, 20L, -20L, 
-10L, 0L, 10L, 20L, -20L, -10L, 0L, 10L, 20L, -20L, -10L, 0L, 
10L, 20L, -20L, -10L, 0L, 10L, 20L), LowFreq = c(3093.5, 3142.7, 
3191.9, 3241.2, 3290.4, 3017.7, 3218.1, 3418.6, 3619, 3819.4, 
2986.1, 3251.1, 3516.2, 3781.2, 4046.3, 2776.5, 2793.1, 2809.8, 
2826.4, 2843.1, 3207.8, 3306.2, 3404.5, 3502.8, 3601.2, 2813.1, 
2834.5, 2855.9, 2877.2, 2898.6, 4468.3, 4461.2, 4454.2, 4447.1, 
4440.1, 2498.5, 2596.9, 2695.4, 2793.8, 2892.3), Upper = c(3.7, 
3.7, 3.7, 3.7, 3.7, 12.23, 12.23, 12.23, 12.23, 12.23, -3.96, 
-3.96, -3.96, -3.96, -3.96, -3.22, -3.22, -3.22, -3.22, -3.22, 
-11.34, -11.34, -11.34, -11.34, -11.34, 15.34, 15.34, 15.34, 
15.34, 15.34, -6.75, -6.75, -6.75, -6.75, -6.75, -0.67, -0.67, 
-0.67, -0.67, -0.67), Lower = c(-2.71, -2.71, -2.71, -2.71, -2.71, 
-1.31, -1.31, -1.31, -1.31, -1.31, -16.17, -16.17, -16.17, -16.17, 
-16.17, -15.28, -15.28, -15.28, -15.28, -15.28, -15.79, -15.79, 
-15.79, -15.79, -15.79, -3.79, -3.79, -3.79, -3.79, -3.79, -20.19, 
-20.19, -20.19, -20.19, -20.19, -8.24, -8.24, -8.24, -8.24, -8.24 
)), .Names = c("Male", "dBLevel", "LowFreq", "Upper", "Lower" 
), row.names = c(NA, 40L), class = "data.frame") 
+0

這將是真的很難幫你沒有一個重複的例子(即我們需要的數據幀'dbLevel')。 – joran

+0

您只需要列dBLevel中的數據?或者來自實際數據框'lowfreqblups'的數據?無論哪種方式,你需要整個數據框還是僅僅是兩個人的一個子集? – user3494534

+0

如果您提供一個**最小的,自包含的示例**,則更容易提供幫助。請檢查這些鏈接的一般想法,以及如何在R:[** here **](http://stackoverflow.com/help/mcve),[** here **](http:// www .sscce.org /)和[** here **](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)。 – Henrik

回答

0

這裏是一個可能的解決方案,繪製每個Male,其中所述斜率和截距由原始數據所確定的線,但在x範圍是由LowerUpper對於男值來確定。

我已經使用線性迴歸lm()獲得線係數,並且predict()獲得每個Lower和Upper值的y值。這些值被放入一個名爲line_dat的數據框中。

male_vec = unique(test$Male) 

predict_list = list() 

for (i in seq(length(male_vec))) { 
    male = male_vec[i] 
    subdat = test[test$Male == male, ] 
    x = subdat[, "dBLevel"] 
    y = subdat[, "LowFreq"] 
    model = lm(y ~ x) 
    new_x = data.frame(x=c(subdat[1, "Lower"], subdat[1, "Upper"])) 
    new_y = predict(model, new_x) 
    predict_list[[i]] = data.frame(Male=male, dBLevel=new_x$x, LowFreq=new_y) 
} 

line_dat = do.call(rbind, predict_list) 
line_dat 
# Male dBLevel LowFreq 
# 1 69 -2.71 3178.599 
# 2 69 3..155 
# 11 113 -1.31 3392.304 
# 21 113 12.23 3663.686 
# 12 126 -16.17 3087.594 
# 22 126 -3.96 3411.220 
# 13 143 -15.28 2784.339 
# 23 143 -3.22 2804.419 
# 14 155 -15.79 3249.221 
# 24 155 -11.34 3292.982 
# 15 178 -3.79 2847.761 
# 25 178 15.34 2888.642 
# 16 186 -20.19 4468.414 
# 26 186 -6.75 4458.939 
# 17 193 -8.24 2614.257 
# 27 193 -0.67 2688.784 

library(ggplot2) 

p = ggplot(data=line_dat, aes(x=dBLevel, y=LowFreq, group=Male)) + 
    geom_line(size=1.0) 

ggsave("lines.png", plot=p, height=4, width=6, dpi=150) 

enter image description here

+0

我認爲你所做的非常好!這正是我所描繪的。我不得不花一些時間坐下來理解你的劇本,因爲我不熟悉它。謝謝! – user3494534