如何使用R中的ggplot2在下图的每个面板中添加标准偏差的文本注释(例如,sd = sd_value)?
library(datasets)
data(mtcars)
ggplot(data = mtcars, aes(x = hp)) +
geom_dotplot(binwidth = 1) +
geom_density() +
facet_grid(. ~ cyl) +
theme_bw()
我发布了情节图片,但我没有足够的代表。
我认为" geom_text"或"注释"可能有用,但我不确定如何。
答案 0 :(得分:3)
如果要更改每个方面的文本标签,则需要使用$state.go
。如果您希望在每个构面中显示相同的文字,可以使用done()
。
geom_text
现在,如果您真的希望在此示例中按annotate
标准偏差,我可能会先使用p <- ggplot(data = mtcars, aes(x = hp)) +
geom_dotplot(binwidth = 1) +
geom_density() +
facet_grid(. ~ cyl)
mylabels <- data.frame(cyl = c(4, 6, 8),
label = c("first label", "seond label different", "and another"))
p + geom_text(x = 200, y = 0.75, aes(label = label), data = my labels)
### compare that to this way with annotate
p + annotate("text", x = 200, y = 0.75, label = "same label everywhere")
进行计算,然后使用cyl
完成此操作,如下所示:< / p>
dplyr
答案 1 :(得分:1)
当统计信息出现在构面标签本身内时,我更喜欢图形的外观。我制作了以下脚本,允许选择显示标准偏差,平均值或计数。本质上,它会计算摘要统计信息,然后将其与名称合并,以便您具有格式 CATEGORY(SUMMARY STAT = VALUE)。
#' Function will update the name with the statistic of your choice
AddNameStat <- function(df, category, count_col, stat = c("sd","mean","count"), dp= 0){
# Create temporary data frame for analysis
temp <- data.frame(ref = df[[category]], comp = df[[count_col]])
# Aggregate the variables and calculate statistics
agg_stats <- plyr::ddply(temp, .(ref), summarize,
sd = sd(comp),
mean = mean(comp),
count = length(comp))
# Dictionary used to replace stat name with correct symbol for plot
labelName <- mapvalues(stat, from=c("sd","mean","count"), to=c("\u03C3", "x", "n"))
# Updates the name based on the selected variable
agg_stats$join <- paste0(agg_stats$ref, " \n (", labelName," = ",
round(agg_stats[[stat]], dp), ")")
# Map the names
name_map <- setNames(agg_stats$join, as.factor(agg_stats$ref))
return(name_map[as.character(df[[category]])])
}
将此脚本与原始问题一起使用:
library(datasets)
data(mtcars)
# Update the variable name
mtcars$cyl <- AddNameStat(mtcars, "cyl", "hp", stat = "sd")
ggplot(data = mtcars, aes(x = hp)) +
geom_dotplot(binwidth = 1) +
geom_density() +
facet_grid(. ~ cyl) +
theme_bw()
该脚本应易于更改以包含其他摘要统计信息。我也相信它可以部分重写,以使它更清洁!