0

I am using ggplot in R on a Mac, doing a line graph using the group option. I want to add the values that correspond to the end points for each of the lines. This is part of the data I am using:

  Year Foundation Type No. of Houses Percent Shares
1 2000     Crawl Space        209529       16.84583
2 2001     Crawl Space        206431       16.58441
3 2002     Crawl Space        204327       15.58577
4 2003     Crawl Space        213328       15.39025
5 2004     Crawl Space        224195       14.63272
6 2005     Crawl Space        258254       15.91873

I run the following code:

ggplot(USbyFoundType, aes(x=Year, y=`Percent Shares`, 
            group=`Foundation Type`, color=`Foundation Type`)) +
  geom_line() 

I get this chart. I want to place the value at the end of each of the lines.

enter image description here

Thanks for any help

Peter
  • 11,500
  • 5
  • 21
  • 31
Manuel
  • 21
  • 2
  • Does this answer your question? [Plot labels at ends of lines](https://stackoverflow.com/questions/29357612/plot-labels-at-ends-of-lines) – stefan Jul 02 '21 at 21:07

1 Answers1

0

It would be nice to have a reproducible example, but something like:

endpts <- (USbyFoundType 
      %>% group_by(`Foundation Type`) 
      %>% filter(Year == max(Year))
)

Then add

+ geom_text(data = endpts, aes(x = Year, y = `Percent Shares`,
          colour = `Foundation Types`,
          label = `Percent Shares`)

You'll probably have to play with horizontal justification (hjust), spacing (nudge_x), and margins (e.g. + expand_limits(y=2030)).

This question is about plotting labels (not values) at the end of the lines, but contains lots of useful information about adjusting positioning, margins, clipping etc.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453