How can I label every line in a multiple regression plot neatly without overlapping?

58 views Asked by At

I am trying to label each line in my regression plot in ** R ** based on the country the line corresponds to but I'm running into excess labels and labels that are not situated where I would like them. I want the label for each line to be off to the left of each line. I used the check_overlap function but, the labels are still overlapping.

This is a link to the data: https://docs.google.com/spreadsheets/d/14lZajSHqDvDswot8tFh5x6XOAMfmqFkEKZnvsDc_t0c/edit?usp=sharing The datasheet here is what I call "urban" in my code! No manipulation is needed!

This is the code that I tried:

Fig1a <- urban %>%
  ggplot(aes(x = log10(B15), y = log10(E_GR_AH14), color = CTR_MN_NM)) +
  geom_smooth(method = "lm", se = FALSE, fullrange = FALSE) +
  geom_abline(intercept = 0, slope = 1:1, color = "black", size = 1) +
  labs(y="log Total High Greenness km2", x="log Total Built Area km2", title = "Total Built Area vs. Total High Greenness") +
  scale_color_manual(values = c("Argentina"="#fb6542",
                                "Australia"="#3f681c",
                                "Brazil"="#fb6542",
                                "Canada"="#fb6542",
                                "Germany"="#3f681c",
                                "France"="#3f681c",
                                "United Kingdom"="#fb6542",
                                "Indonesia"="#fb6542",
                                "India"="#3f681c",
                                "Nigeria"="#3f681c",
                                "United States"="#375E97",
                                "South Africa"="#3f681c",
                                "Japan"="#3f681c",
                                "Mexico"="#375E97",
                                "Russia"="#3f681c",
                                "Ukraine"="#375E97",
                                "Bangladesh"="#fb6542",
                                "China"="#375E97"))+ 
  theme_minimal() +
  geom_text(aes(label = CTR_MN_NM), nudge_x = 0.2, nudge_y = 0.2, check_overlap = TRUE)

I expected to at least get a concise number of labels but the labels doubled and are all over the place and looks like this:

enter image description here

1

There are 1 answers

0
Nir Graham On

remove your geom_text, and try to use library(gghighlight) with the use_direct_label = TRUE parameter