在 R 中绘制重叠的横向图 [英] Drawing overlayed sideways plots in R
问题描述
我在 R 中有以下代码.
I have the following code, in R.
x = c(rep(2,10),rep(4,10))
y1 = c(5.1,3,4.2,4.1,4.8,4.0,5,4.15,3,4.5)
y2 = c(9.1,8,9.2,8.2,7,9.5,8.8,9.3,10,10.4)
y = c(y1,y2)
plot(x,y,pch=16,cex=0.9,xlim=c(0,6),ylim=c(0,13))
此代码生成一个带有两条点带的图.我使用powerpoint在这些频段上横向叠加了正常曲线.如何使用实际均值和 sd 值在 R(绘制横向正态曲线)中执行此操作?注意:我再说一遍,正常曲线不是绘图的一部分.上面的代码只生成原始图.
This code produces a plot with two bands of dots. I've overlayed normal curves sideways on those bands using powerpoint. How can I do this in R (drawing the sideways normal curves), using the actual means and sd values? NOTE: I repeat, the normal curves are not part of the plot. The code above just produces the raw plot.
推荐答案
首先,计算 y1
和 y2
的均值和标准差.
First, calculate mean and standard deviation for y1
and y2
.
m1<-mean(y1)
s1<-sd(y1)
m2<-mean(y2)
s2<-sd(y2)
然后制作了两个数据框(为方便起见),其中包含 y 值作为数字序列(比实际的 y1
和 y2
值更宽).然后使用 dnorm()
计算 x
的密度值并计算平均值和标准偏差值.然后添加 2
或 4
以将值移动到所需位置.
Then made two data frame (for convenience) that contains y values as sequence of numbers (wider than actual y1
and y2
values). Then calculated density values for x
using dnorm()
and calculated mean and standard deviation values. Then added 2
or 4
to shift values to desired position.
df1<-data.frame(yval=seq(1,7,0.1),xval=(dnorm(seq(1,7,0.1),m1,s1)+2))
df2<-data.frame(yval=seq(6,12,0.1),xval=(dnorm(seq(6,12,0.1),m2,s2)+4))
使用函数 lines()
添加密度线.
Added density lines with function lines()
.
plot(x,y,pch=16,cex=0.9,xlim=c(0,6),ylim=c(0,13))
with(df1,lines(xval,yval))
with(df2,lines(xval,yval))
这篇关于在 R 中绘制重叠的横向图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!