Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

stat_density linetype not in legend #802

idot opened this Issue Apr 24, 2013 · 5 comments


None yet
4 participants

idot commented Apr 24, 2013

df <- data.frame(line=c("A","B","C","D"), col=sample(c("E","F")), count=rnorm(100))
ggplot(df, aes(x=count)) + stat_density(position="identity", aes(linetype=line,colour=col),fill=NA)

the line type legend show empty boxes


krlmlr commented Jul 5, 2013

Can replicate with

screenshot from 2013-07-05 11 15 41

The problem is that both linetype and colour are specified in the aesthetics. The colour legend is drawn, the linetype legend seems to use the default colour, which is NA for stat_density:

ggplot(df, aes(x=count)) + stat_density(position="identity", aes(linetype=line),fill=NA)

screenshot from 2013-07-05 11 19 05

Legends for linetype should use the color of the theme, not the default color of the geometry.


BrianDiggs commented Jul 5, 2013

A workaround would be

ggplot(df, aes(x=count)) + 
  stat_density(position="identity", aes(linetype=line,colour=col),fill=NA) +



krlmlr commented Jul 5, 2013

Thanks for reminding me of override.aes. But isn't this a glitch in ggplot? What would be the correct way to fix it?


BrianDiggs commented Jul 5, 2013

I didn't mean to imply that the current behavior is optimal; I just wanted to give a workaround, not a fix (because I could do that off the top of my head, where a true fix would be a lot harder).

I think your assessment

the linetype legend seems to use the default colour, which is NA for stat_density

is probably right. A simple fix may be that legends should make sure that they don't use NA for any of their aesthetics if it would be need to be non-NA to be seen. There s a question as to where to get a global default from, then. But I don't know if that would break something else; the logic (or really, where the logic/assumptions broke down) would have to be understood first to give a full fix.


hadley commented Feb 24, 2014

This sounds like a great feature/horrible bug, but unfortunately we don't currently have the development bandwidth to support it/fix it. If you'd like to submit a pull request that implements this feature/fixes this bug, please follow the instructions in the development vignette.

@hadley hadley closed this Feb 24, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment