如何绘制均值的偏差 [英] How to plot deviation from mean

查看:169
本文介绍了如何绘制均值的偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,我创建了一列的简单矩阵,产生一个具有集合均值和给定标准差的数字列表。

  rnorm2 < -  function(n,mean,sd){mean + sd * scale(rnorm(n))} 
r< - rnorm2(100,4,1)

我现在想绘制这些数字与平均值不同。我可以在Excel中做到这一点,如下所示:





但是我想用 ggplot2 在Excel中创建R.图我使用线图进行了欺骗,但是如果我可以将其作为列进行操作,它会更好。我曾尝试过使用散点图,但我无法弄清楚如何将其转化为偏离平均值的偏差。

解决方案

也许你想要:

  rnorm2 < -  function(n,mean,sd){mean + sd * scale(rnorm(n))} 
set.seed(101)
r < - rnorm2(100,4 ,1)
x < - seq_along(r)##设定从1到长度(r)
par(las = 1,bty =l)的向量##美观偏好
plot(x,r,col =green,pch = 16)##绘制点
##如果您根本不需要点,则使用
## plot(x,r $ x $ = x,y1 = r,col =green),
##设置轴, )##将它们连接到平均线
abline(h = 4)

如果你在0附近绘制,你可以自动使用 type =h

  plot(x,r-4,type =h,col =green)

ggplot2 中做到这一点:
$ b

  library(ggplot2 )
theme_set(theme_bw())##我的美容偏好
ggplot(数据(x,r))+
geom_segment(aes(x = x,xend = x,y = mean(r),yend = r),color =green)+
geom_hline yintercept = mean(r))


In R I have created a simple matrix of one column yielding a list of numbers with a set mean and a given standard deviation.

rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) }
r <- rnorm2(100,4,1)

I now would like to plot how these numbers differ from the mean. I can do this in Excel as shown below:

But I would like to use ggplot2 to create a graph in R. in the Excel graph I have cheated by using a line graph but if I could do this as columns it would be better. I have tried using a scatter plot but I cant work out how to turn this into deviations from the mean.

解决方案

Perhaps you want:

rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) }
set.seed(101)
r <- rnorm2(100,4,1)
x <- seq_along(r)  ## sets up a vector from 1 to length(r)
par(las=1,bty="l") ## cosmetic preferences
plot(x, r, col = "green", pch=16) ## draws the points
## if you don't want points at all, use 
##    plot(x, r, type="n")  
## to set up the axes without drawing anything inside them
segments(x0=x, y0=4, x1=x, y1=r, col="green") ## connects them to the mean line
abline(h=4)

If you were plotting around 0 you could do this automatically with type="h":

plot(x,r-4,type="h", col="green")

To do this in ggplot2:

library("ggplot2")
theme_set(theme_bw()) ## my cosmetic preferences
ggplot(data.frame(x,r))+
    geom_segment(aes(x=x,xend=x,y=mean(r),yend=r),colour="green")+
    geom_hline(yintercept=mean(r))

这篇关于如何绘制均值的偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆