Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot:在x轴上绘制箱子,在y轴上绘制平均值_R_Ggplot2_Binning - Fatal编程技术网

R ggplot:在x轴上绘制箱子,在y轴上绘制平均值

R ggplot:在x轴上绘制箱子,在y轴上绘制平均值,r,ggplot2,binning,R,Ggplot2,Binning,假设我有一个如下所示的数据帧: data <- data.frame(y = rnorm(10,0,1), x = runif(10,0,1)) data您可以使用stat\u summary()函数 library(ggplot2) data <- data.frame(y = rnorm(10,0,1), x = runif(10,0,1)) data$bins <- cut(data$x,breaks = 4) # Points: ggplot(data, aes(x

假设我有一个如下所示的数据帧:

data <- data.frame(y = rnorm(10,0,1), x = runif(10,0,1))

data您可以使用
stat\u summary()
函数

library(ggplot2)
data <- data.frame(y = rnorm(10,0,1), x = runif(10,0,1))
data$bins <- cut(data$x,breaks = 4)
# Points:
ggplot(data, aes(x = bins, y = y)) +
  stat_summary(fun.y = "mean", geom = "point")

# Histogram bars:
ggplot(data, aes(x = bins, y = y)) +
  stat_summary(fun.y = "mean", geom = "histogram")
库(ggplot2)

数据由于y值的平均值可能小于0,我建议使用点图而不是条形图。点代表方法。您可以使用qplot或常规ggplot函数。后者更易于定制。在本例中,两者产生相同的输出

library(ggplot2)

set.seed(7)
data <- data.frame(y = rnorm(10,0,1), x = runif(10,0,1))
data$bins <- cut(data$x,breaks = 4, dig.lab = 2)

qplot(bins, y, data = data, stat="summary", fun.y = "mean")

ggplot(data, aes(x = factor(bins), y = y)) + 
  stat_summary(fun.y = mean, geom = "point")
库(ggplot2)
种子(7)

data此线程有点旧,但现在请使用stat\u summary\u bin(它可能在较新版本中)


我看到您的问题要求切割值,然后绘制平均值。在
hist
函数(如
>tmp)中使用标准算法查找垃圾箱的更具统计意义的方法
library(ggplot2)

set.seed(7)
data <- data.frame(y = rnorm(10,0,1), x = runif(10,0,1))
data$bins <- cut(data$x,breaks = 4, dig.lab = 2)

qplot(bins, y, data = data, stat="summary", fun.y = "mean")

ggplot(data, aes(x = factor(bins), y = y)) + 
  stat_summary(fun.y = mean, geom = "point")
m <- tapply(data$y, data$bins, mean)
sd <- tapply(data$y, data$bins, sd)
df <- data.frame(mean.y = m, sd = sd, bin = names(m))

ggplot(df, aes(x = bin, y = mean.y, 
               ymin = mean.y - 1.96*sd, 
               ymax = mean.y + 1.96*sd)) + 
  geom_errorbar() + geom_point(size = 3)
ggplot(data, mapping=aes(x, y)) +
stat_summary_bin(fun.y = "mean", geom="bar", bins=4 - 1) +
ylab("mean")