Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate legend in ggplot2

Tags:

plot

r

ggplot2

I wish to rotate a ggplot2 legend 90°

From

qplot(mpg, wt, data=mtcars, colour=cyl)

enter image description here

to produce

enter image description here

like image 665
Etienne Low-Décarie Avatar asked Apr 12 '15 19:04

Etienne Low-Décarie


2 Answers

Something like:

p <- qplot(mpg, wt, data=mtcars, colour=cyl)
p + scale_colour_continuous(guide = guide_legend(direction = "horizontal", title.position = "top",
                             label.position="bottom", label.hjust = 0.5, label.vjust = 0.5,
                             label.theme = element_text(angle = 90))) + 
      theme(legend.position = c(0.5, 0.9))

Ref: ggplot docs

enter image description here

like image 102
r.bot Avatar answered Oct 31 '22 23:10

r.bot


you can try this

library(ggplot2)
qplot(mpg, wt, data=mtcars, colour=cyl) + theme(legend.position = "top")

like image 44
Mamoun Benghezal Avatar answered Nov 01 '22 00:11

Mamoun Benghezal