我正在努力复制使用R
/ggplot2
的this个优秀演示的各个方面.
对于第一个点(绘制不同的bin计数),我处理得很好,但是当对第二个点(不同的最大值)复制这种方法时,它奇怪地失败了,我不知道为什么.
以下是我在上面的2.篇帖子中指出的健壮性不足的证明:
library(dplyr)
library(ggplot2)
library(magrittr)
requireNamespace("kmed")
# Define the new maxima to be used
maxima <- c(197, 202, 213, 224)
# Replicate the UCI heart data set
m_df <- bind_rows(
replicate(length(maxima), kmed::heart, simplify = FALSE)) %>%
dplyr::mutate(
sm = rep(maxima, each = nrow(kmed::heart)),
fsm = paste("Maximum Edited to:", rep(maxima, each = nrow(kmed::heart))))
# Modify the maxima
m_df[which(m_df$thalach == max(m_df$thalach)), "thalach"] <- maxima
# Generate individual geom_histogram layers, forcing identical number of bins
m_lp_hist <- plyr::llply(maxima, function(b) {
geom_histogram(
data = m_df %>% filter(sm == b),
mapping = aes(x = thalach),
bins = 20)
})
# Combine the layers
m_p_hist <- Reduce("+", m_lp_hist, init = ggplot2::ggplot())
m_p_hist +
ggplot2::facet_wrap(. ~ fsm, scales = "free_y") +
ggplot2::theme_bw() +
ggplot2::labs(x = "Maximum Heart Rate Achieved", y = "Count")
当判断结果图时,直方图非常相似(!),而单步执行使用ggplot() + m_p_list[[1]]
等生成的各个层很好地显示了层之间的差异,从而显示了我想要演示的效果(直方图缺乏健壮性).这是怎么回事!?
谢谢你的指点