Home > OS >  R ggplot2 draw a label box around ggtext for geom_hline
R ggplot2 draw a label box around ggtext for geom_hline

Time:07-19

Based on the data and code below, how can I add a label boundary box (similar to when you hover your mouse over a line and the value shows up with a boundary box via ggplotly) as shown in the desired output below?

As a note for some unknown reason the mean horizontal line appears black, even though in the legend it's blue (as defined in the code).

Desired Output:

enter image description here

Sample data (AvgTMeanYear):

structure(list(year = 1980:2021, AvgTMean = c(24.2700686838937, 
23.8852956598276, 25.094446596092, 24.1561175050287, 24.157183605977, 
24.3047482638362, 24.7899738481466, 24.5756232655603, 24.5833086228592, 
24.7344695534483, 25.3094451071121, 25.2100615173707, 24.3651692293534, 
24.5423890611494, 25.2492166633908, 24.7005097837931, 24.2491591827443, 
25.0912281781322, 25.0779264303305, 24.403294248319, 24.4983991453592, 
24.4292324356466, 24.8179824927011, 24.7243948463075, 24.5086534543966, 
24.2818632071983, 24.4567195220259, 24.8402224356034, 24.6574465515086, 
24.5440715673563, 23.482670620977, 24.9979594684914, 24.5452453980747, 
24.9271462811494, 24.7443215819253, 25.8929839790805, 25.1801908261063, 
25.2079308058908, 25.0722425561207, 25.4554644289799, 25.4548979078736, 
25.0756772250287)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-42L))

Code:

    AvgTMeanYear %>%
     group_by(year) %>%
  summarise(tmean = mean(AvgTMean,na.rm = TRUE)) %>%
  ggplot(aes(x= year, y=tmean))  
  geom_line(aes(color = "Historic Trend"), stat = "identity")  
  geom_hline(yintercept = mean(AvgTMeanYear$tmean), aes(color="Average Temperature"))  
  scale_colour_manual(values = c("Average Temperature" = "blue","Historic Trend" = "black"), name = "Legend")  
  xlab("Year")  
  ylab("Avg. Mean T. (\u00B0C)")  
  ggtitle("Temperature Trend 1980-2021")  
  geom_text(aes(x = 1980 , y = 24.6, label = "24.47 \u00B0C"))

CodePudding user response:

Let's approach all issues.

  1. geom_label draws a box around the text.
  2. We can fix the blue line problem by forcing different data for geom_hline. Tbh, I'm not entirely certain why your initial literal approach did not work, but this one does.
  3. Because of where the labels is located, we can turn off clipping to make sure it is all visible. (This may not always be an issue.) (This could also be resolved by using hjust= and yjust= for manual control, or the ggrepel package for dynamic/stochastic "forcing" things away from data.)
AvgTMeanYear %>%
  group_by(year) %>%
  summarise(AvgTMean = mean(AvgTMean ,na.rm = TRUE)) %>%
  ggplot(aes(x= year, y=AvgTMean))  
  geom_line(aes(color = "Historic Trend"), stat = "identity")  
  # UPDATED to add new data and put yint inside aes
  geom_hline(aes(yintercept = y, color = "Average Temperature"),
             data = data.frame(col="Average Temperature", y=24.4))  
  scale_colour_manual(values = c("Average Temperature" = "blue","Historic Trend" = "black"), name = "Legend")  
  xlab("Year")  
  ylab("Avg. Mean T. (\u00B0C)")  
  ggtitle("Temperature Trend 1980-2021")  
  # UPDATED to change geom_text to geom_label
  geom_label(aes(x = 1980 , y = 24.6, label = "24.47 \u00B0C"))  
  # ADDED
  coord_cartesian(clip = "off")

ggplot with boxed text and blue line

CodePudding user response:

Are you looking for geom_label()? For example,

ggplot(mtcars, aes(wt, mpg))  
  geom_point()  
  geom_label(aes(x = 2.62, y = 21, label = "Mazda RX4"))

CodePudding user response:

Option using annotate and with separate dataframe for geom_hline:

AvgTMeanYear <- structure(list(year = 1980:2021, AvgTMean = c(24.2700686838937, 
                                                              23.8852956598276, 25.094446596092, 24.1561175050287, 24.157183605977, 
                                                              24.3047482638362, 24.7899738481466, 24.5756232655603, 24.5833086228592, 
                                                              24.7344695534483, 25.3094451071121, 25.2100615173707, 24.3651692293534, 
                                                              24.5423890611494, 25.2492166633908, 24.7005097837931, 24.2491591827443, 
                                                              25.0912281781322, 25.0779264303305, 24.403294248319, 24.4983991453592, 
                                                              24.4292324356466, 24.8179824927011, 24.7243948463075, 24.5086534543966, 
                                                              24.2818632071983, 24.4567195220259, 24.8402224356034, 24.6574465515086, 
                                                              24.5440715673563, 23.482670620977, 24.9979594684914, 24.5452453980747, 
                                                              24.9271462811494, 24.7443215819253, 25.8929839790805, 25.1801908261063, 
                                                              25.2079308058908, 25.0722425561207, 25.4554644289799, 25.4548979078736, 
                                                              25.0756772250287)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                          -42L))                                                              
df_hline <- data.frame(col_line = "Average Temperature", y = mean(AvgTMeanYear$AvgTMean))

library(ggplot2)
library(dplyr)
AvgTMeanYear %>%
  group_by(year) %>%
  summarise(tmean = mean(AvgTMean ,na.rm = TRUE)) %>%
  ggplot(aes(x= year, y=tmean))  
  geom_line(aes(color = "Historic Trend"), stat = "identity")  
  geom_hline(df_hline, mapping = aes(yintercept = y, color="Average Temperature"))  
  scale_colour_manual(values = c("Average Temperature" = "blue","Historic Trend" = "black"), name = "Legend")  
  xlab("Year")  
  ylab("Avg. Mean T. (\u00B0C)")  
  ggtitle("Temperature Trend 1980-2021")  
  annotate("label", x = 1985 , y = 25, label = "24.47 \u00B0C") 

Created on 2022-07-18 by the reprex package (v2.0.1)

  • Related