Удалить часть ключей легенды или настроить элементы легенды - PullRequest
1 голос
/ 30 марта 2020

Последняя легенда, которую я хотел бы показать только для женщин и мужчин в двух цветах: enter image description here

На данный момент код ниже показывает все 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)) 

enter image description here

1 Ответ

2 голосов
/ 30 марта 2020

Один из способов сделать это - использовать эстетику 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)

enter image description here

...