-1

As shown in the plot below, the state names are sometimes far from the line. One option to consider is not to put the labels next to the lines, but instead to include them in a box at the top right-hand corner of the figure. How can I do this?

Here is the part of my codes that I believe need to be modified.

    geom_line(aes(x =Year, y = Age.Adjusted.Rate, colour = State), data = d_filtered_top5_fe)+
  
  ggrepel::geom_text_repel(aes(x =Year, y = Age.Adjusted.Rate, colour = State, label = State, fontface = 'bold'), data = d_filtered_top5_fe %>% 
                             
                             filter(Year == max(Year)),
                           segment.color = 'transparent',
                           size = 2.5
  ) +

enter image description here

Nader Mehri
  • 514
  • 1
  • 5
  • 21
  • 1
    Could you please add a sample of your data using `dput(d_filtered_top5_fe)` by editing your question and pasting the output from `dput()` on it! – Duck Oct 20 '20 at 00:57

1 Answers1

0

Without your data, it's difficult to work out an exact solution. I guess there might be several ways to fix this. The easiest one is to use legend. We could try something like this:

library(ggplot2)
ggplot(data = mtcars)+
geom_line(aes(x =wt, y = mpg, colour = factor(cyl))) + 
  theme( legend.position = c(0.9, .9),
  ) +
  facet_wrap(vars(am))

If you could provide a reproducible example, it would help others help you.

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
  • Thanks. How can I have two separate legends for 0 and 1 plots? Also, I was wondering if I can sort the legend labels based on their values. It appears that R sort them alphabetically. – Nader Mehri Oct 20 '20 at 01:20