[英]How to plot two density curves of two continuous variables on the same chart
[英]How to plot two curves on the same canvas in R
這是用於生成模擬數據的代碼:
eff = seq(.1, 1, .1)
method = c('method1', 'method2')
xxxd = expand.grid(eff=eff, method=method)
xxxd$power = c(pow1, pow2)
pow1 = seq(.2, .7, length.out=10)
pow2 = seq(.4, .8, length.out=10)
pow1 = pow1 + rnorm(10, .05, .01)
pow2 = pow2 + rnorm(10, .05, .01)
xxxd$power = c(pow1, pow2)
這是數據:
eff method power
1 0.1 method1 0.25942
2 0.2 method1 0.32162
3 0.3 method1 0.36329
4 0.4 method1 0.41286
5 0.5 method1 0.47904
6 0.6 method1 0.52165
7 0.7 method1 0.58191
8 0.8 method1 0.64884
9 0.9 method1 0.69488
10 1.0 method1 0.73656
11 0.1 method2 0.44882
12 0.2 method2 0.49010
13 0.3 method2 0.54465
14 0.4 method2 0.58675
15 0.5 method2 0.63173
16 0.6 method2 0.69120
17 0.7 method2 0.71456
18 0.8 method2 0.77440
19 0.9 method2 0.81033
20 1.0 method2 0.85103
我想產生的數字是這樣的:
您可能需要做lines()
函數。 首先將plot()
與您的“方法”一起使用,然后在第二個方法上使用lines()
。 參數與plot
相同。 只是將新曲線添加到現有繪圖中,而不是創建新窗口。 ?lines
應闡明更多。
由於您的數據開始時似乎是長格式,因此與ggplot2
一起使用很簡單:
library(ggplot2)
ggplot(xxxd, aes(eff, power, colour = method)) + geom_line()
ggplot2的幫助頁面確實很棒: http ://docs.ggplot2.org/current/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.