How do I fix these labels at the end of line?

Kevin

I am using the ggrepel package to add labels to the end of lines in ggplot. I am having an issue where the labels are overlapping and adding more labels than needed. I adjusted my max.overlap value a few times and was still getting overlaps. My 2022 line has no label on it. Attached an image of what was going on, and here is the code to generate the line. Do I need to add some y-axis nudge?

        output$creditPlot <- renderPlot({
      ggplot(temp_df_long_credit_hours, aes(week, value, group = school_year, alpha = school_year)) +
        geom_line(lwd = 1.5,
                  show.legend = F) +
        geom_label_repel(aes(label = school_year), 
                         nudge_x = 4, 
                         na.rm = T, 
                         max.overlaps = 0,
                         stat = "unique",
                         show.legend = F,
                         force_pull = 10) +
        geom_vline(xintercept = 0)
    })
    

Dataframe used.

> dput(temp_df_long_credit_hours)
structure(list(week = c(-22, -22, -22, -22, -22, -21, -21, -21, 
-21, -21, -20, -20, -20, -20, -20, -19, -19, -19, -19, -19, -18, 
-18, -18, -18, -18, -17, -17, -17, -17, -17, -16, -16, -16, -16, 
-16, -15, -15, -15, -15, -15, -14, -14, -14, -14, -14, -13, -13, 
-13, -13, -13, -12, -12, -12, -12, -12, -11, -11, -11, -11, -11, 
-10, -10, -10, -10, -10, -9, -9, -9, -9, -9, -8, -8, -8, -8, 
-8, -7, -7, -7, -7, -7, -6, -6, -6, -6, -6, -5, -5, -5, -5, -5, 
-4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -1, 
-1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 
7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 
11, 11, 11, 11, 11, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA), school_year = c("fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022", "fall_2018", "fall_2019", "fall_2020", 
"fall_2021", "fall_2022"), value = c(NA, NA, NA, NA, NA, 20761, 
18661.5, NA, NA, 10408.5, 56914, 49744.5, NA, 12039, 28930, 72070, 
62730, NA, 34960.5, 38809, 85278, 77023.5, NA, 46043, 47951, 
94803.5, 87161.5, 57634.5, 55161, 55711.5, 104565.5, 96156.5, 
66395, 62856.5, 61944.5, 113641, 103396, 76650.5, 69399, 67723.5, 
120581, 109970.5, 84893.5, 75854, 72565, 126255, 114543.5, 90434, 
81376, 75630, 131825, 119571, 95729.5, 85729.5, 79555, 136776.5, 
124173, 99772, 90196, 83577, 141738, 128459.5, 103475, 94700.5, 
87207, 147072, 133202.5, 108800, 98973.5, 91474, 152369, 138274.5, 
114452, 103632.5, 95704, 156527, 141785, 118685, 107489.5, 100413, 
162992, 147955.5, 124000.5, 111988.5, 107615, 170244, 154728, 
131260.5, 118594.5, 114076.5, 179047, 161710.5, 138620.5, 125615, 
120784, 188901, 168262.5, 147012, 133236, 129033, 200011, 177967.5, 
156769, 142968, 137916.5, 210551.5, 188592, 167904, 152467, 147424, 
221325, 197919, 180528, 162947, 158055, 223855.5, 199881.5, 183164, 
165206.5, 160699.5, 220775, 197768.5, 180640, 163221.5, 158929, 
220600, 198025, 180133, 163479, NA, 221621, 199232, 180923, 164650, 
NA, 222392, 200337, 182174, 165379, NA, 223202, 200842, 182492, 
166214, NA, 224024.5, 201569, 182758, 166424.5, NA, 224416, 202112, 
183081, 166685.5, NA, 224966.5, 202713.5, 183616, 167111.5, NA, 
225698.5, 203292, 184464, 167943.5, NA, 226119.5, 203663, 184718, 
168058, NA, 227276.5, 206149.5, 186401, 169126.5, NA, 227276.5, 
206149.5, 186401, 169126.5, 164678.706656292, NA, NA, NA, NA, 
NA)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-185L))

Example of line and label output

Peter

Here maybe one option by creating a separate helper dataframe for the labels. A bit of tinkering around with the geom_label arguments will allow fine tuning of the labels. Editing scale_x_continuous expand argument allows adjustment of the margin to the plot to give the labels a bit more space.

The reason you are not getting a label for 2022 seems to be related to NA values.

library(ggplot2)
library(dplyr)

labs <- 
  temp_df_long_credit_hours |> 
  na.omit() |> 
  group_by(school_year) |> 
  filter(week == max(week))|>
  mutate(vjust = case_when(school_year == "fall_2022" ~ 0.9,
                           TRUE ~0.5))


ggplot(temp_df_long_credit_hours, aes(week, value, group = school_year, alpha = school_year)) +
  geom_line(lwd = 1.5,
            show.legend = FALSE) +
  geom_label(data = labs, aes(label = school_year, x = week, y = value),
            hjust = -0.1, 
            vjust = labs$vjust,
            show.legend = FALSE)+
  scale_x_continuous(expand = expansion(mult = c(0.05, 0.2)))+
  geom_vline(xintercept = 0)

Created on 2022-10-04 with reprex v2.0.2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I move to end of line in Vim?

R: How do I position multi-line tick labels at plots in base R?

How do I fix this URL regexp to match URLs at the end of the string?

How do I add an extra line to the end of a th?

Python 2.7.15 adds a new line in Windows to the end but not on Linux. How do I fix this?

How do I concisely end print() with a double new line?

Index labels removed when deleting rows, do I want to fix this?

How do I end a string in '<Carriage Return> <Line Feed>'?

How do I end the program once the last line of the text is reached?

How do I add these tiny dots at the end of line plots in matplotlib?

How do I end a line half way along the X axis in Google Charts Line Graph?

How do I delete a line at the end of the file and put it at the top of the file?

How do I echo a line with linebreak at the end at window border?

How do I append a character to the end of every line in an Excel cell?

How to add labels to the end of a highcharts line graph

How do I append text from one line, to the end of another?

How do I move a word to the end of line in notepad++?

How do I determine end points of a Line drawing object?

How do I fix line spacing between views in notepad++

how do i add everying in each line between " " and put , at the end of each line using javascript

How do I put button in the same line with text (labels)

How do I end line when writing to a text file?

Java Problem at scanner (Line not found) How do i fix this?

How do I print a line below another line that is using end=" "

Why are my tkinter labels arranged by .pack() arranging themselves strangely and how do I fix it

How do I fix this 101 keeps showing at the end of result

How do I create a line widget with rounds in its start and end?

how do i fix ')' (to close '(' at line 38), got '.'

how do I fix a point on a CSS line