scale_linetype_manual not respecting order of 'values'

235 views
Skip to first unread message

Mauricio

unread,
Mar 24, 2015, 11:14:34 AM3/24/15
to ggp...@googlegroups.com
Hello,

I'm using scale_linetype_manual (in ggplot2 1.0.1) to specify that lines should begin 'solid' and change to 'longdash' ... but the results of the code below reverses those type choices, starting the lines as dashed then changing to solid.

Why is this happening?

Thank you
--------------------------------------

library(ggplot2)

df = data.frame(x=1:20, y=rnorm(20), center=0, low= -1, high=1, calibrate=c(rep(T,10), rep(F,10)))

ggplot(df, aes(x, y, linetype=calibrate)) +  
  geom_line(aes(y=center), color="green") +
  geom_line(aes(y=low), color="red") +
  geom_line(aes(y=high), color="red") +
  scale_linetype_manual(breaks=c(TRUE, FALSE), values=c("solid", "longdash")) +
  geom_point()

Ben Bond-Lamberty

unread,
Mar 24, 2015, 11:28:30 AM3/24/15
to Mauricio, ggplot2
Thanks for the reproducible example. I believe it's happening because
the 'breaks' are still subject to the default factor ordering, which
is (F, T), which means that solid gets mapped to F and longdash to T.
You can just take out the breaks argument entirely, which is not
needed in this case, to get what you want:

... + scale_linetype_manual(values=c("solid", "longdash")) + ...

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Mauricio

unread,
Mar 24, 2015, 12:22:30 PM3/24/15
to ggp...@googlegroups.com
Thanks Ben.

I hadn't realized the sorting of the logical factor.  I guess I could always reorder the column in the data frame using the 'levels' attribute of factor() prior to calling ggplot()... but there's no need.

Thanks again for your explanation.
Reply all
Reply to author
Forward
0 new messages