ggplot: adjusting alpha/fill two factors cdf

拥有回忆 提交于 2019-12-07 07:33:42

问题


I'm having some issues getting my ggplot alpha to be sufficiently dark for my plot.

Example code:

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

As you can see, whenever carb == 1, it's very difficult to see the plot elements. In my real world data set, the factor for color has four levels and the alpha factor has two levels. I was hoping to have the alpha a slightly lighter shade of the color, but more visible than how it's occurring in that example).


回答1:


You can adjust the alpha scale, as the user in the comment suggests, either by specifying a range or a specific set breaks to scale_alpha_discrete. That doesn't produce a very easy-to-read result, though:

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

Another option would be to save color for the many-leveled factor and choose a different aesthetic for the few-leveled one, like maybe linetype

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

For readability, though, faceting might be a better bet.

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



来源:https://stackoverflow.com/questions/27612634/ggplot-adjusting-alpha-fill-two-factors-cdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!