首页 文章

ggplot:调整alpha / fill两个因子cdf

提问于
浏览
5

我有一些问题让我的ggplot alpha对我的情节来说足够黑暗 .

示例代码:

ggplot(mtcars, aes(x=mpg, color=factor(gear), alpha=factor(carb))) + stat_ecdf()

enter image description here

正如您所看到的,只要 carb == 1 ,它就会出现在该示例中) .

1 回答

  • 6

    您可以通过指定 range 或特定的 breaksscale_alpha_discrete 来调整alpha刻度,就像评论建议的用户一样 . 但是,这并不会产生非常容易阅读的结果:

    ggplot(mtcars, aes(x=mpg, color=factor(gear), alpha=factor(carb))) + 
      stat_ecdf() + 
      scale_alpha_discrete(range=c(0.4, 1))
    

    enter image description here

    另一个选择是保存 color 以获得多层次因素,并为少数几个选择不同的美学,例如 linetype

    ggplot(mtcars, aes(x=mpg, linetype=factor(gear), color=factor(carb))) + 
      stat_ecdf()
    

    enter image description here

    但是为了便于阅读,分面可能是更好的选择 .

    ggplot(mtcars, aes(x=mpg, color=factor(carb))) + 
      stat_ecdf() + facet_wrap(~gear, nrow=3)
    

    enter image description here

相关问题