【问题标题】:transperancy in ggplot become just whiteoutggplot 中的透明度变成了白化
【发布时间】:2018-02-23 21:06:34
【问题描述】:

我有这样的数据:

PENATVTY    educ    prop    order   alphayr
Vietnam         1   0.28109453  13  0.5
Vietnam         2   0.51243781  14  0.5
Vietnam         3   0.20646766  15  0.5
U.S. natives    1   0.28956793  16  0.1
U.S. natives    2   0.57418815  17  0.1
U.S. natives    3   0.13624393  18  0.1
All Immigrants  1   0.30822711  19  0.1
All Immigrants  2   0.42321587  20  0.1
All Immigrants  3   0.26855702  21  0.1
Germany         1   0.35264484  22  0.5
Germany         2   0.5768262   23  0.5
Germany         3   0.07052897  24  0.5
Philippines     1   0.40591398  25  0.5
Philippines     2   0.50672043  26  0.5
Philippines     3   0.08736559  27  0.5
Canada          1   0.4600639   28  0.5
Canada          2   0.46964856  29  0.5
Canada          3   0.07028754  30  0.5
China           1   0.48217054  31  0.5
China           2   0.35193798  32  0.5
China           3   0.16589147  33  0.5
India           1   0.82162162  34  0.5
India           2   0.13648649  35  0.5
India           3   0.04189189  36  0.5

我想让“美国原住民”和“所有移民”行变得透明。无论我赋予 alpha 哪个值,透明度水平都不会改变。我哪里做错了?我两周前开始写 R,所以我的程序不容易阅读。谢谢。

 # create transperancy bars
      catsx$alphayr <- (ifelse(
      catsx$PENATVTY  == "U.S. natives"| 
      catsx$PENATVTY  == "All Immigrants", .5, 1))

  ggplot(catsx, 
   aes(x=reorder(PENATVTY, order), 
       y=prop, 
    fill=factor(educ, le`enter code here`vels = c("3", "2", "1")))) +
    geom_bar(stat="identity", position = "stack",
       aes(alpha=alphayr)) +
    coord_flip() +
    theme_bw() +
    ylab("Percent (%)") +
    xlab('') +
    theme(legend.position='none') +
    ggtitle('Exhibit 1') 

【问题讨论】:

  • 请尝试这里给出的代码:pastebin.com/3spu7HcU。我得到以下情节:imgur.com/a/9UcQi
  • 请告诉我,你在哪里改变了?另外,我得到了同样的情节。我想要 50% 的透明度,但这张图是 99% 的透明度。不仅如此,无论我如何更改 alphayr 中的数字,都没有任何变化……当我输入真实值而不是 alhpayr 时,例如:aes(alpha=0.5) 所有条形都变为 50 % 透明。所以我知道 aes(alpha=alphayr) 有问题,但我不知道应该怎么做。

标签: r ggplot2 alpha-transparency


【解决方案1】:

按照here 的建议,您应该将alphayr 定义为factor 以获得离散比例,然后使用scale_alpha_manual 手动调整alpha 比例。

ggplot(catsx, aes(x=reorder(PENATVTY, order), y=prop,
                  fill=factor(educ, levels=c("3","2","1")))) +
  geom_bar(stat="identity", position = "stack", aes(alpha=factor(alphayr))) +
  scale_alpha_manual(values = c("0.5"=0.5, "1"=1)) +
  coord_flip() + theme_bw() +
  ylab("Percent (%)") + xlab('') +
  theme(legend.position='none') + ggtitle('Exhibit 1') 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-02
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2014-02-27
    • 2010-12-08
    相关资源
    最近更新 更多