1

I want to plot something without using theme(), but instead theme_classic(). How can I change the position of the legend to the "bottom"?

Thanks!

  • 2
    `library(ggplot2); ggplot(mtcars, aes(wt, mpg, color = as.factor(gear))) + geom_point() + theme_classic() + theme(legend.position = "bottom")` Not sure what you mean "without using `theme`" -- that is the way to change the legend position from your current theme's defaults. – Jon Spring Mar 01 '22 at 21:27
  • Is there a reason why you want to change a theme without using the `theme()` function? – markus Mar 01 '22 at 21:27
  • 1
    Welcome to SO, Beatrice Mihaela! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. (Though JonSpring's comment resolves the OP.) – r2evans Mar 01 '22 at 21:37
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 02 '22 at 07:26
  • Thanks! + theme_classic() + theme(legend.position = "bottom" – Beatrice Mihaela Mar 03 '22 at 10:24

1 Answers1

3

I also struggled to answer your question; @JonSpring provided valuable code and explanation.

Sample code:

ggplot(diamonds, mapping = aes(x = clarity)) + 
  geom_bar(aes(fill = cut)) + 
  labs(x = NULL, y = NULL) + 
  guides(x = "none", y = "none")+
  theme_classic()+
   theme(legend.position = 'bottom', legend.direction = "horizontal")

Output:

enter image description here

Rfanatic
  • 2,224
  • 1
  • 5
  • 21