r - 具有 position_dodge 的不同因子水平数量的中心 stat_summary 平均值
问题描述
我对正确躲避 with 有stat_summary
疑问position_dodge
。我会用diamond
数据来说明。目标是可视化用于预测的变量(这里我们使用price
)如何分布在我要预测的类之间(carat > 1
和carat < 1
)
因此,我将连续carat
值分为两类carat > 1
和carat < 1
. 计算两个类别的平均价格。在另一个步骤中,在填充美学carat
的调用中再次分箱。ggplot2
第一个填充箱0-1
与 class 一起使用carat < 1
。在我的实际数据中,第一类是“缺席”,第二类是“存在”。因此,其他填充箱属于类carat > 1
,并允许更详细地显示分布。
我的问题:geom_point
无论填充箱的数量如何,是否可以将每个班级的平均值居中?在这种情况下,蓝色圆点应位于蓝色圆圈列的中心,红色圆点应位于当前填充箱数量(2 或 3)的中心。
如果一直在玩价值,stat_summary(position=position_dodge(width = 0.9)
但我只是没有完全到达那里。
library(ggplot2)
data("diamonds")
set.seed(10)
diamond_subset <- diamonds[sample(nrow(diamonds), 1500),]
diamond_subset$carat[diamond_subset$carat < 1] <- 0
# plot price of diamond by class label and carat intervals
# split carat values into 2 classes for classification
diamond_subset$classes <- cut(diamond_subset[["carat"]],
breaks = c(-Inf, 1, Inf), labels = c("carat_below_1", "carat_above_1"))
# set intervals for continuous carat values, all intervals > 1 belong to class "carat_above_1"
break_points <- c(0,1,2,3,4,5.1)
ggplot(diamond_subset, aes(x = cut, y = price, colour = classes)) +
geom_point(aes(fill = cut(carat, break_points, include.lowest = TRUE)), pch = 21, alpha = 0.4, size = 2,
position = position_jitterdodge(dodge.width = 0.7, jitter.width = 0.5)) +
stat_summary(position=position_dodge(width = 0.9), fill = "black",
fun.y = mean, geom = "point", shape = 21, size = 4, stroke = 1.5, alpha = 1) +
scale_fill_manual(values = c("white", "green", "yellow", "red", "black"),
name = "Carat") +
scale_colour_manual(
values = c("blue", "red"),
name = "Average price per class",
breaks = c("carat_below_1", "carat_above_1"),
labels = c("Carat < 1", "Carat > 1")
)
额外问题:可能的解决方案是否也适用于 2 个以上的课程?例如
diamond_subset$classes <- cut(diamond_subset[["carat"]],
breaks = c(-Inf, 1, 2, Inf), labels = c("carat_below_1", "carat_above_1", "carat_above_2"))
ggplot(diamond_subset, aes(x = cut, y = price, colour = classes)) +
geom_point(aes(fill = cut(carat, break_points, include.lowest = TRUE)), pch = 21, alpha = 0.4, size = 2,
position = position_jitterdodge(dodge.width = 0.7, jitter.width = 0.5)) +
stat_summary(position=position_dodge(width = 0.9), fill = "black",
fun.y = mean, geom = "point", shape = 21, size = 4, stroke = 1.5, alpha = 1) +
scale_fill_manual(values = c("white", "green", "yellow", "red", "black"),
name = "Carat") +
scale_colour_manual(
values = c("blue", "red", "black"),
name = "Average price per class",
breaks = c("carat_below_1", "carat_above_1", "carat_above_2"),
labels = c("Carat < 1", "Carat > 1", "Carat > 2")
)
编辑:
如果没有人对此有解决方案,我当然可以将图保存为 pdf 并手动将点居中?
解决方案
推荐阅读
- c++ - 为什么这个 C++ 程序比 Node.js 慢?
- git - 当我尝试将代码推送到 GitHub 时,它不会询问我的用户名和密码以进行身份验证
- powerbi - 从表中检测每日暴露量(每日)
- javascript - 有没有更好的编码方式?(初学者)React.js
- sql-server - 在多个表中定义公共字段
- java - 如何使用 ObjectMapper 读取具有地图内容映射的 JSON 文件?
- google-chrome - Chrome 扩展 UI 调整大小/在生产中使用较小的值
- go - switch case 处的正则表达式
- asp.net - 在 Ionic Angular 应用程序中使用用户身份验证向 ASP.net Web API 发出 HTTP 获取请求
- google-bigquery - Campaign Manager - 尝试访问活动中的数据以使用数据传输云存储桶查看 GCLID