Praveen Singh Praveen Singh - 9 months ago 58
R Question

Positioning the Legend Box

I have a bar Plot that looks like this:
enter image description here

If I use the command

then I get the plot like this:
enter image description here

But in this case the legend overlaps the plot. I want that the legend just shifts upwards without overlapping as in the first plot. What should I do ?

Answer Source

You can try this:

theme(legend.justification = "top")


mtcars$gear = factor(mtcars$gear)
mtcars$cyl = factor(mtcars$cyl)
p = ggplot(data = mtcars, aes(x=gear, fill=cyl) ) + geom_bar()
p = p + xlab("Gears") + ggtitle("Cylinders by Gears")
p = p + theme(legend.justification = "top")

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download