R ggplot:添加颜色美学更改堆栈顺序

R ggplot:添加颜色美学更改堆栈顺序,r,ggplot2,R,Ggplot2,发现一个奇怪的边缘案例 假设您想要一个带有标记线段的堆叠条形图(撇开这种图是否为最佳数据viz) 堆栈顺序已更改,这是意外的,我不确定如何修复此问题 > sessionInfo() R version 3.5.0 (2018-04-23) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS High Sierra 10.13.4 Matrix products: default BLAS: /System/

发现一个奇怪的边缘案例

假设您想要一个带有标记线段的堆叠条形图(撇开这种图是否为最佳数据viz)

堆栈顺序已更改,这是意外的,我不确定如何修复此问题

> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.4

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.0.0.9000

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     digest_0.6.15    withr_2.1.2      dplyr_0.7.4      assertthat_0.2.0 grid_3.5.0       plyr_1.8.4       R6_2.2.2        
 [9] gtable_0.2.0     magrittr_1.5     scales_0.5.0     pillar_1.2.2     rlang_0.2.1      lazyeval_0.2.1   bindrcpp_0.2.2   labeling_0.3    
[17] tools_3.5.0      glue_1.2.0       munsell_0.4.3    yaml_2.1.19      compiler_3.5.0   pkgconfig_2.0.1  colorspace_1.3-2 bindr_0.1.1     
[25] tibble_1.4.2  

啊,好吧,这里有一个答案+变通一下。这是因为
aes(color=…)
调用是在
geom_text
级别调用的,而不是在最初的ggplot调用中调用的

统一到单个
aes
调用将导致geom_col和geom_text遵循相同的顺序,但需要一些技巧才能使颜色美学仅在文本层显示:

ggplot(dat,
       aes(x, y, fill=category, label=paste0(category, ': ', y),
           color = category == 'a')) +

  # if you call geom_col just like this, you'll get colored borders
  # geom_col() +

  # so you have to blank out the color aesthetic for this geom
  geom_col(color=NA) +

  geom_text(position = position_stack(vjust=.5)) +

  scale_color_manual(values = c("black", 'white'))

在我的脑海中,
geom_text(aes(color=category),position=position\u stack(vjust=.5))+scale\u color_manual(values=c(a=“black”,b=“black”,c=“white”)
,尽管你可能会反对它以你不喜欢的方式组合了图例……你“normal”示例中的文本标签是不正确的。尝试在ggplot调用之外创建标签:
mutate(cat_label=paste0(category,“:”,y))
,然后将
label=cat_label
放在
aes()
@joran:主要是我对这种行为感到惊讶,似乎它可能是bug?你同意这是出乎意料的吗,如上所述?这对我来说似乎出乎意料…但我试图确定我是否遗漏了一些东西-比如它如何改变堆栈顺序?它似乎是根据您的expressionthx的真实/错误结果进行分组的-如果没有好的答案,我会在几天后将其作为一个问题发布在ggplot github上;如果您感兴趣,请随时投票支持可见性
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.4

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.0.0.9000

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     digest_0.6.15    withr_2.1.2      dplyr_0.7.4      assertthat_0.2.0 grid_3.5.0       plyr_1.8.4       R6_2.2.2        
 [9] gtable_0.2.0     magrittr_1.5     scales_0.5.0     pillar_1.2.2     rlang_0.2.1      lazyeval_0.2.1   bindrcpp_0.2.2   labeling_0.3    
[17] tools_3.5.0      glue_1.2.0       munsell_0.4.3    yaml_2.1.19      compiler_3.5.0   pkgconfig_2.0.1  colorspace_1.3-2 bindr_0.1.1     
[25] tibble_1.4.2  
ggplot(dat,
       aes(x, y, fill=category, label=paste0(category, ': ', y),
           color = category == 'a')) +

  # if you call geom_col just like this, you'll get colored borders
  # geom_col() +

  # so you have to blank out the color aesthetic for this geom
  geom_col(color=NA) +

  geom_text(position = position_stack(vjust=.5)) +

  scale_color_manual(values = c("black", 'white'))