简体   繁体   中英

move ggplot2 legend to top right corner?

I would like to make the legend in my plot horizontal and move it to the top right corner. Is that possible?

DF3 <- data.frame(
  x = rnorm(100, mean = 0, sd = 1),
  y = rnorm(n = 100, mean = 1, sd = 1),
  shape = sample(
    x = c('circle', 'square'), size = 100, replace = T, prob = c(.5,.5)
  ),
  Precision = runif(n = 100, min = 0.1, max = 1)
)

library(ggplot2)

DF3 %>%
  ggplot(aes(
    x = x, y = y, color = Precision, size = 2, shape = shape
  )) +
  geom_point(show_guide = TRUE) + 
  theme_bw()+ 
  scale_colour_gradient(low = "#f7fbff", high = "#08306b") + guides(shape = "none", size="none")

Try:

+ theme(legend.position=c(1,1),
        legend.direction="horizontal",
        legend.justification=c(1, 0), 
        legend.key.width=unit(1, "lines"), 
        legend.key.height=unit(1, "lines"), 
        plot.margin = unit(c(5, 1, 0.5, 0.5), "lines"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM