change legend order of ggplot in python

38 views
Skip to first unread message

xcy.q...@gmail.com

unread,
Mar 27, 2018, 4:13:05 PM3/27/18
to ggplot2
I've met a problem of using ggplot in python.
I have legends like this. How can I change the order of the legends to "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19."?


Brandon Hurr

unread,
Mar 27, 2018, 4:43:05 PM3/27/18
to xcy.q...@gmail.com, ggplot2
When the numbers are converted to to factors they are made into characters and sorted by that.

Easiest way is to make it a factor and supply the levels. Something like:

library(tidyverse)

my_data_frame %>%
mutate(label = factor(label, levels = 1:19) %>%
ggplot(., aes(x=x, y=y, colour = label) + geom_line()



--
--
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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

xcy.q...@gmail.com

unread,
Mar 27, 2018, 5:10:50 PM3/27/18
to ggplot2
Is this for R or Python?

在 2018年3月27日星期二 UTC-5下午3:43:05,Brandon Hurr写道:
When the numbers are converted to to factors they are made into characters and sorted by that.

Easiest way is to make it a factor and supply the levels. Something like:

library(tidyverse)

my_data_frame %>%
mutate(label = factor(label, levels = 1:19) %>%
ggplot(., aes(x=x, y=y, colour = label) + geom_line()


On Tue, Mar 27, 2018 at 12:56 PM, <xcy.q...@gmail.com> wrote:
I've met a problem of using ggplot in python.
I have legends like this. How can I change the order of the legends to "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19."?


--
--
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.

Brandon Hurr

unread,
Mar 27, 2018, 5:25:10 PM3/27/18
to xcy.q...@gmail.com, ggplot2
That is R code. If you have code to share that would be helpful because I don’t knohave how you’re using ggplot within python.
Reply all
Reply to author
Forward
0 new messages