r - 第二个传说ggplot
问题描述
我有以下数据框和ggplot:
dat1 <- read.table(text = "row variable value percents
1 Base 0.0029 2.61%
2 Base 0.0022 58.59%
3 Base 0.0056 66.41%
4 Base 0.0106 73.68%
1 ATE 0.00007555184 2.61%
2 ATE 0.00128906474 58.59%
3 ATE 0.00371889655 66.41%
4 ATE 0.00780957462 73.68%", header = TRUE)
ggplot(dat1, aes(x = row, y = value, fill = variable)) +
geom_bar(stat = "identity") +
xlab("\nNtile") +
ylab("Estimate\n") +
theme_bw() +
ggtitle("Base line + ATE by Ntile (as defined by CATE)") +
labs(fill = "Value", caption = "Note: ATE as % of baseline on grid in black")
理想情况下,我想获得第二个图例,该图例仅将percents
列列出到其相关栏。每个百分比都是 ATE,表示为 Base 的百分比。我有一种将它们放在实际图上的方法,但是当某些百分比为负数时看起来很奇怪(我用不同的数据制作了很多这些)。所以像这样的传说:
百分比:
1 - 2.61%
2 - 58.59%
3 - 66.41%
4 - 73.68%
但在实际情节上没有任何点或条。我试图利用这一点geom_point
,aes
但这会将实际点放在情节上。
编辑:添加示例图例的草图
解决方案
label
您可以放置不可见的文本,并为您的图例显示美观的比例。不过,您需要覆盖一些美学。
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.3
dat1 <- read.table(text = "row variable value percents
1 Base 0.0029 2.61%
2 Base 0.0022 58.59%
3 Base 0.0056 66.41%
4 Base 0.0106 73.68%
1 ATE 0.00007555184 2.61%
2 ATE 0.00128906474 58.59%
3 ATE 0.00371889655 66.41%
4 ATE 0.00780957462 73.68%", header = TRUE)
ggplot(dat1, aes(x = row, y = value, fill = variable)) +
geom_bar(stat = "identity") +
geom_text(aes(label = percents), alpha = 0) +
scale_discrete_identity(
"label",
guide = guide_legend(override.aes = list(
alpha = 1, label = 1:4
))) +
xlab("\nNtile") +
ylab("Estimate\n") +
theme_bw() +
ggtitle("Base line + ATE by Ntile (as defined by CATE)") +
labs(fill = "Value", caption = "Note: ATE as % of baseline on grid in black")
由reprex 包于 2021-02-24 创建(v1.0.0)
推荐阅读
- amazon-cloudwatch - AWS RDS Aurora 可用磁盘空间指标
- angular - 我在组件 Angular 8 中调用服务时未定义
- c - 使用包含字符串的 sscanf 解析字符串
- flutter - 无法使用提供程序将表单登录屏幕导航到 bottom_tab_screen
- android - 使用 Source.Server 但用户未连接到 Internet 时,我可以获得 Firestore 错误代码吗?
- python - 无法在 Windows 上的 pycharm 上安装 scikit-learn
- python - 将输入值列表与 Flask webapp 中的数据库查询匹配
- flutter - Flutter 树视图小部件和 Firestore
- java - 有没有办法将字母形式的数字字符串数组转换为数字形式?
- spring - Spring Security - 重定向到自定义登录页面而不通过控制器