使 R 中特定条形的颜色比其他条形更坚固

问题描述 投票:0回答:1

我正在创建一个考虑两年和一组国家的条形图。为此,我使用 ggplot 包中的 geom_bar 函数。我想仅突出显示特定国家/地区的颜色。有办法做到吗?

这是图表的示例,与我正在处理的类似:

Panel <- read.dta("http://dss.princeton.edu/training/Panel101.dta")
Panel$y = Panel$y / 1000000

Panel %>%
  filter(year %in% c(1990, 1991)) %>% 
  ggplot(mapping = aes(x = country, y = y, fill = as.factor(year))) +
  geom_bar(stat = 'identity',
           position = 'dodge2', colour = 'white') +
  scale_fill_manual(values = c('#001d87', '#d9912e')) +
  labs(fill = 'Year', x = 'Country') +
  theme_classic()

目标是突出 C 国的颜色,例如,通过使该国家/地区的颜色比其他国家更坚固,可以在不影响图例的情况下区分该国家/地区。我想突出显示颜色而不仅仅是边框。我感谢任何帮助!

r ggplot2 graph geom-bar
1个回答
0
投票

您可以使用 alpha 来突出显示您想要的国家/地区

Panel <- read.dta("http://dss.princeton.edu/training/Panel101.dta")
Panel$y = Panel$y / 1000000

Panel %>% mutate(country_alpha=ifelse(country %in% c("C"),1,0.5)) %>% 
  filter(year %in% c(1990, 1991)) %>% 
  ggplot(mapping = aes(x = country, y = y, fill = as.factor(year), alpha=country_alpha)) +
  geom_bar(stat = 'identity',
           position = 'dodge2', colour = 'white') +
  scale_fill_manual(values = c('#001d87', '#d9912e')) +
  labs(fill = 'Year', x = 'Country') +
  theme_classic()
© www.soinside.com 2019 - 2024. All rights reserved.