简体   繁体   中英

How do I add labels to both extremities of my axes?

so imagine the following example

mtcars %>%   
  ggplot(aes(mpg,hp)) + geom_point()

这导致了这个图像

How can I add labels to both extremities of my axes without changing the title of my x and y axes? For instance:

  • bottom right: "MPG increases"
  • bottom left (for x axis): "MPG decreases"
  • bottom left (for y axis): "HP decreases"
  • top left: "HP increases"

Does anyone have any leads on how to do this?

Thank you!

You can try something like this:

library(dplyr)
library(ggplot2)

mtcars %>% ggplot(aes(mpg,hp)) + 
  geom_point() + annotate("text", x = 33, y = 10,
              label = "MPG increases",
              hjust=1.1, vjust=-1.1, col="black",
              cex=5, fontface = "bold", alpha = 0.8)+
  annotate("text", x = 17, y = 10,
           label = "MPG decreases",
           hjust=1.1, vjust=-1.1, col="black",
           cex=5, fontface = "bold", alpha = 0.8)+
  annotate("text", x = 17, y = 100,
           label = "HP decreases",
           hjust=1.1, vjust=-1.1, col="black",
           cex=5, fontface = "bold", alpha = 0.8)+
  annotate("text", x = 17, y = 310,
           label = "HP increases",
           hjust=1.1, vjust=-1.1, col="black",
           cex=5, fontface = "bold", alpha = 0.8)

Here's one way to do this keeping within the maximum and minimum limits of the data. And picks up on @Tjebo suggestion to include the labelling text within a data frame.

Although not in the question have included arrows to see if this would help distinguish the HP and MPG decreases labels. The code for this can easily be disregarded.

If you want to use this presentation regularly it would pay to make a function.

library(ggplot2)
library(tibble)


mtcars %>% 
  ggplot(aes(mpg,hp)) + 
  geom_point()+
  geom_text(data = df_lab, aes(x, y, label = lab), vjust = df_lab$vj, hjust = df_lab$hj)+
  geom_segment(data = df_ar, aes(x = x, y = y, xend = xend, yend = yend), 
               lineend = "square", linejoin = "mitre", size = 1, arrow = arrow(length = unit(2, "mm")))

Data

# tibble for labels 
df_lab <- 
  tibble(lab = c("HP increases", "HP decreases", "MPG decreases", "MPG increases"),
         x = c(rep(min(mtcars$mpg), 3), max(mtcars$mpg)),
         y = c(max(mtcars$hp), rep(min(mtcars$hp), 3)),
         vj = rep(c("bottom", "top") , each = 2),
         hj = c(rep("left", 3), "right"))

# postitioning helpers for arrows

x_len <- 2
y_len <- 40
y_off <- 15
x_off <- 0.5

# tibble for arrows  
  
df_ar <- 
  tibble(xend = c(rep(min(mtcars$mpg) - x_off, 2), min(mtcars$mpg), max(mtcars$mpg)),
         yend = c(max(mtcars$hp), min(mtcars$hp), rep(min(mtcars$hp) - y_off, 2)),
         x = c(rep(min(mtcars$mpg) - x_off, 2), min(mtcars$mpg) + x_len, max(mtcars$mpg) - x_len),
         y = c(max(mtcars$hp) - y_len, min(mtcars$hp) + y_len, rep(min(mtcars$hp) - y_off, 2)))

Created on 2020-05-12 by the reprex package (v0.3.0)

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