Jonas Stein
Jonas Stein

Reputation: 7043

change textposition of colourbar in scale_color_discrete

I generate the legend in my ggplot2 plot with

scale_colour_discrete(name=textCF)

I like that i can see which colour belongs to which value, but it consumes to much space in the plot.

legend does not fit

I tried also the scale_colour_gradientn scheme, but this is so condensed, that i can not distiguish the values anymore. If i could only change the width of this colourbar.

scale_colour_gradientn(name=textCF,colours = rainbow(nrow(mydf), start=2/6), breaks=round(mydf$CF)) +

ticks are to close

How can I improve the lookout?

Upvotes: 0

Views: 994

Answers (1)

Brian Diggs
Brian Diggs

Reputation: 58845

The size of a colourbar can be controlled with the barwidth and barheight parameters.

Start with a reproducible example to see the effects:

ggplot(mtcars, aes(x=wt, y=mpg, colour=qsec)) +
  geom_point() +
  scale_colour_gradient() +
  theme(legend.position = "bottom")

I'm using scale_colour_gradient, but this works in general.

You control aspects of the guide with the guide argument to a scale. See the help on guide_colourbar for all the parameters that you can set.

ggplot(mtcars, aes(x=wt, y=mpg, colour=qsec)) +
  geom_point() +
  scale_colour_gradient(guide = guide_colourbar(barwidth=20, barheight=2)) +
  theme(legend.position = "bottom")

enter image description here

Upvotes: 3

Related Questions