R ggplot:统计变量x并显示bin上方变量y平均值的直方图 test\u data
没有任何内置到R ggplot:统计变量x并显示bin上方变量y平均值的直方图 test\u data,r,ggplot2,histogram,R,Ggplot2,Histogram,没有任何内置到stat\u bin()函数中(该函数geom\u histogram()调用)来执行您要求的操作,但执行您要求的操作并不太难(或不太简洁): test_data <- data.frame(x = runif(20, 0, 10), y = runif(20, 0, 10)) ggplot(test_data, aes(x)) + geom_histogram(binwidth = 1) test_data <- test_data %>% arrange
stat\u bin()
函数中(该函数geom\u histogram()
调用)来执行您要求的操作,但执行您要求的操作并不太难(或不太简洁):
test_data <- data.frame(x = runif(20, 0, 10), y = runif(20, 0, 10))
ggplot(test_data, aes(x)) + geom_histogram(binwidth = 1)
test_data <- test_data %>% arrange(x)
test_list <- list()
for(i in 1:10){
test_list[[i]] <- test_data %>% filter( x < i & x > i-1)
}
test_list
test_means <- c()
for(i in 1:10){test_means[i] <- mean(test_list[[i]]$y)}
test_means
库(ggplot2)
图书馆(dplyr)
种子(15)#可复制
测试数据%
do(平均值(%)%>%
解组()->bin_表示
gg没有任何内置的stat_bin()
函数(该函数调用geom_histogram()
函数)可以满足您的要求,但满足您的要求并不太难(或不够简洁):
test_data <- data.frame(x = runif(20, 0, 10), y = runif(20, 0, 10))
ggplot(test_data, aes(x)) + geom_histogram(binwidth = 1)
test_data <- test_data %>% arrange(x)
test_list <- list()
for(i in 1:10){
test_list[[i]] <- test_data %>% filter( x < i & x > i-1)
}
test_list
test_means <- c()
for(i in 1:10){test_means[i] <- mean(test_list[[i]]$y)}
test_means
库(ggplot2)
图书馆(dplyr)
种子(15)#可复制
测试数据%
do(平均值(%)%>%
解组()->bin_表示
gg你可以试试base R:
library(ggplot2)
library(dplyr)
set.seed(15) # reproducible
test_data <- data.frame(x = runif(20, 0, 10),
y = runif(20, 0, 10))
gg <- ggplot(test_data, aes(x)) +
geom_histogram(binwidth=1, fill="#2166ac", color="white")
mean_bin <- function(df) {
filter(test_data, x > df$xmin & x <= df$xmax) %>%
summarise(μ=mean(y), ct=df$count[1]) %>%
mutate(μ=ifelse(is.nan(μ), NA, μ))
}
group_by(ggplot_build(gg)$data[[1]], x) %>%
do(mean_bin(.)) %>%
ungroup() -> bin_means
gg <- gg + geom_text(data=bin_means,
aes(x, ct, label=sprintf("μ(y)=%3.2f", μ)),
vjust=0, nudge_y=0.1, size=2.5)
gg <- gg + scale_x_continuous(breaks=1:10)
gg <- gg + scale_y_continuous(expand=c(0,0), limits=c(0, 4.5))
gg <- gg + theme_bw()
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg
#数据
种子集(123)
d你可以试试基本R:
library(ggplot2)
library(dplyr)
set.seed(15) # reproducible
test_data <- data.frame(x = runif(20, 0, 10),
y = runif(20, 0, 10))
gg <- ggplot(test_data, aes(x)) +
geom_histogram(binwidth=1, fill="#2166ac", color="white")
mean_bin <- function(df) {
filter(test_data, x > df$xmin & x <= df$xmax) %>%
summarise(μ=mean(y), ct=df$count[1]) %>%
mutate(μ=ifelse(is.nan(μ), NA, μ))
}
group_by(ggplot_build(gg)$data[[1]], x) %>%
do(mean_bin(.)) %>%
ungroup() -> bin_means
gg <- gg + geom_text(data=bin_means,
aes(x, ct, label=sprintf("μ(y)=%3.2f", μ)),
vjust=0, nudge_y=0.1, size=2.5)
gg <- gg + scale_x_continuous(breaks=1:10)
gg <- gg + scale_y_continuous(expand=c(0,0), limits=c(0, 4.5))
gg <- gg + theme_bw()
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg
#数据
种子集(123)
你太棒了!这是难以置信的信息。一般来说,我对ggplot2
和r
都学到了很多。我对你的回答感激不尽!最后,我用它制作了一个闪亮的应用程序,并找出哪些变量引用了哪个函数,这是一个非常有用的练习。另外,你能在r
中使用“μ”吗?或者你只是为了说明的目的而使用它吗μ’在我的r
版本中似乎不是一个合适的字符。再说一次,你太棒了!至少在我的UTF-8 OS X环境中是完全有效的字符。非常高兴这有帮助。你太棒了!这是难以置信的信息。一般来说,我对ggplot2
和r
都学到了很多。我对你的回答感激不尽!最后,我用它制作了一个闪亮的应用程序,并找出哪些变量引用了哪个函数,这是一个非常有用的练习。另外,你能在r
中使用“μ”吗?或者你只是为了说明的目的而使用它吗μ’在我的r
版本中似乎不是一个合适的字符。再说一次,你太棒了!至少在我的UTF-8 OS X环境中是完全有效的字符。非常高兴这有帮助。哇,这是非常简单和直接的。这真的很有帮助。我学到了很多,也不知道baser
提供了这么多的控制。我真的很喜欢使用实际平均数来确定标签高度的想法。非常感谢你!哇,这是非常简单和直接的。这真的很有帮助。我学到了很多,也不知道baser
提供了这么多的控制。我真的很喜欢使用实际平均数来确定标签高度的想法。非常感谢你!