r - 移除部分图例键或自定义图例项
问题描述
目前,下面的代码显示了所有 8 个键:
library(data.table)
library(ggplot2)
temp721 <- data.table(
YearRange = c("A1", "A1", "A2", "A2", "B1", "B1", "B2", "B2"),
Gender = c("Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male"),
TN = c(864, 491, 1408, 749, 457, 350, 587, 412),
PrctPosNum= c(16, 20, 13, 14, 19, 18, 14, 14)
)[, Prct:=paste0(PrctPosNum,"%")][, BarText:=paste0(Prct, " of ", TN)]
colors <- c(
rep(c("#F29F9E", "#FEE8AC"),2),
rep(c("#AD1A17","#FBBF16"),2)
)
ggplot( data=temp721, aes(x=YearRange, y=PrctPosNum, fill=interaction(Gender,YearRange) ) ) +
geom_bar( stat="identity", position=position_dodge(width=0.8), width = 0.7) +
geom_text(aes(label=BarText), position = position_dodge(0.8), vjust=-0.8, size=3.5) +
scale_x_discrete(labels=function(x){sub("_", "\n", x)}) +
scale_y_continuous(labels = function(x) paste0(x, "%"), limits = c(0, 30), breaks = seq(0, 30, by = 5), expand = c(0, 0) ) +
scale_fill_manual(values = colors, name="") +
ylab("Proportion (%)") +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white"),
axis.line = element_line(),
axis.title.x = element_blank(),
legend.position = c(0.95, 0.99),
legend.key.size = unit(0.4, "cm"),
legend.key.width = unit(0.6,"cm"),
legend.justification = c("right", "top"),
legend.direction = "horizontal",
legend.box.just = "right",
legend.margin = margin(-5, 0, 0, 0))
解决方案
您可能会这样做的一种方法是使用美学fill = Gender
,alpha = YearRange
然后使用scale_alpha_manual()
为不同的组设置透明度:
ggplot(data=temp721, aes(x=YearRange, y=PrctPosNum, fill = Gender, alpha = YearRange)) +
geom_bar(stat="identity", position=position_dodge(width=0.8), width = 0.7) +
geom_text(aes(label=BarText), position = position_dodge(0.8), vjust=-0.8, size=3.5) +
scale_x_discrete(labels=function(x){sub("_", "\n", x)}) +
scale_y_continuous(labels = function(x) paste0(x, "%"), limits = c(0, 30), breaks = seq(0, 30, by = 5), expand = c(0, 0) ) +
scale_fill_manual(values = c("#AD1A17","#FBBF16"), name="") +
ylab("Proportion (%)") +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white"),
axis.line = element_line(),
axis.title.x = element_blank(),
legend.position = c(0.95, 0.99),
legend.key.size = unit(0.4, "cm"),
legend.key.width = unit(0.6,"cm"),
legend.justification = c("right", "top"),
legend.direction = "horizontal",
legend.box.just = "right",
legend.margin = margin(-5, 0, 0, 0)) +
scale_alpha_manual(values = c(.5, .5, 1, 1), guide = NULL)
推荐阅读
- r - 使用 tidyverse 动词将以下函数翻译为基础 R 作为函数
- c# - 一对多关系,数据未在数据库中播种
- javascript - 如何在CKEditor中的选定元素之后插入一个元素?
- blockchain - 区块链重组
- javascript - ReactJs Redux:在决定 redux 状态时(在 map、filter 等函数之后)如何从数组中删除重复的对象(具有重复值)?
- ios - iOS 上的 Firebaseui:无法在自定义的子类登录屏幕上设置背景颜色
- r - 由于最近的日期时间,不等长度的数据帧会导致合并以引入无效值
- javascript - Next JS 给脚本标签添加数据属性
- android - 在Android中使用Dagger将成员字段注入到扩展来自不同android模块的抽象类的类中?
- r - 在 x 和 y 轴上显示数据点的位置