r - R ggplot:如何将点与闪避的条对齐?
问题描述
我想知道如何将 geom_point 点与 geom_bar 躲避条的位置对齐。
根据 Year 参数对条形进行闪避,但无论其 Year 参数如何,点都绘制在闪避条的中间。
可重现的代码:
set.seed(42)
dat <- data.frame(Response = rep(paste0("Response",1:4),2),
Proportion = round(runif(8),2),
Year = c(rep(2017,4),rep(2018,4)))
industries <- data.frame(Response = rep(paste0("Response",1:4),6),
Proportion = round(runif(24),2),
Year = rep(c(rep(2017,4),rep(2018,4)),3),
Cat = rep(paste0("Cat",1:3),c(rep(8,3))))
ggplot(dat, aes(Response, Proportion, label = paste0(Proportion*100,"%"), fill = factor(Year))) +
geom_bar(stat = "identity", position = "dodge" ) +
geom_point(data = industries, aes(Response, Proportion, fill = factor(Year), col= Cat), size = 3) +
theme(axis.text.x = element_text(angle = 90)) +
scale_y_continuous(labels = scales::percent) +
geom_text(position = position_dodge(width = 1), angle = 90)
解决方案
然后,您需要group = factor(Year)
in (如@Tung 所建议的那样)。在for中重复也是多余的:aes()
position = position_dodge(1)
x, y
aes()
geom_point()
ggplot(dat, aes(Response, Proportion, label = paste0(Proportion*100,"%"),
fill = factor(Year))) +
geom_bar(stat = "identity", position = "dodge" ) +
geom_point(data = industries, aes(col= Cat, group = factor(Year)), size = 3,
position = position_dodge(1)) +
theme(axis.text.x = element_text(angle = 90)) +
scale_y_continuous(labels = scales::percent) +
geom_text(position = position_dodge(width = 1), angle = 90)
推荐阅读
- spring - 使用 spring-boot 的 REST API 和使用 3rd 方 REST API 的自定义身份验证
- powershell - 通过 PowerShell 将所有已安装的软件分组到一个单元中
- list - Excel 2007:如何从列表中提取所有可能的 2 对组合并将它们显示在 2 个单独的列中
- html - 如何正确定位和设计文本后的三角形
- api-platform.com - 在开发期间进行 React 和 docker,无需重启即可查看更改
- php - 谁能告诉我这个 PHP 代码有什么问题?
- php - 在 Laravel 的 index.blade 中使用多态关系
- html - 在我的 CSS 中,我在以“h1 {font-size: 2em;...}”开头的代码底部出现解析错误
- typescript - 在 TSX 中将 var 作为函数键入时出错
- laravel - 404 not found for laravel for delete form