0

I would like to add labels for my data points, but would like to add both the x and y value for only the start and end data point. With geom_text, I can only add either x or y for all of my data points. Is there a way for me to achieve what I would like to on my plot?

Some code of my data:

Area <- structure(list(Cumulative.increase = c(14.69, 37.21, 50.72, 51.81, 57.05, 57.98, 72.65, 79.93, 95.82, 
97.25, 115.69, 147.69, 155.82, 161.03, 169.9, 185.31, 190.57, 194.82), 
Reclaim.date = structure(c(13610, 13722, 13994, 14010, 14154, 14218, 14362, 14426, 14730, 14826, 15082, 
15850, 16170, 16378, 16650, 16922, 17370, 17546), class = "Date")), 
.Names = c("Cumulative.increase",  "Reclaim.date"), 
row.names = c(NA, 18L), class = "data.frame")

Here is the code I now have for my plot.

    plot1 <- ggplot(Area, aes(x=Reclaim.date, y=Cumulative.increase))+
      geom_area() + 
      geom_text(data = Area, aes(x=Reclaim.date, y=Cumulative.increase, 
         label = Reclaim.date))

In this case, I would like to include labels of both Date and Cumulative.increase in the plot, but only for the first and last data point.

I checked some posts about this, some of them use ifelse to choose their specific points, but as I would like the first and last points, I'm not sure what I should do. Thanks a lot.

Here is what my graph currently looks like, with only the x/y axis (which is the x-axis in this case) and also showing labels for every point but I would like only the first and last point.

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • @ Sheena Chung check these links out please https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – tjebo Jul 09 '18 at 13:42
  • https://stackoverflow.com/help/how-to-ask – tjebo Jul 09 '18 at 13:42
  • 1
    Some ideas for making this more reproducible: See if one of the included R [datasets](http://www.sthda.com/english/wiki/r-built-in-data-sets) can be used to demonstrate the issue instead of your data so we can run the code you are trying, and remove the code for title, labels, theme and any other pieces not relevant to your question. – Brian Stamper Jul 09 '18 at 13:54
  • I tried adding some codes for a reproducible example and removing not related codes. The geom_text part is the one I am not sure about. Is the code I included enough for a reproducible example? Thanks in advance. @Tjebo – Sheena Chung Jul 09 '18 at 15:40
  • @BrianStamper this is getting spooky. Is there an algorithm which is flagging some specific key words?? – tjebo Jul 09 '18 at 22:19

1 Answers1

1

one way with dplyr

require(dplyr)
  data_text <- Area %>% filter(row_number() %in% c(1,n()))

I have done this with row_number, because your previous code included several groups. You want to group by your variable first using group_by.

Then use this new data frame for your plot

 ggplot(Area, aes(x=Reclaim.date, y=Cumulative.increase))+
  geom_area() + 
  geom_text(data = data_text, aes(x=Reclaim.date, y=Cumulative.increase, label = Reclaim.date))

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • I see, thanks a lot! Is it also possible to add the y-value on the graph or does the labelling only allow either one of the two (x/y) values? – Sheena Chung Jul 09 '18 at 17:05
  • 1
    @SheenaChung of course - easiest in a second geom_text : currently you specify the x and y coordinates for the “x-labels”. do the same but with the “y labels”. You might need to add a position adjusting parameter because the two labels will overlay – tjebo Jul 09 '18 at 17:17
  • Oh, should have thought of that! Thank you very much! – Sheena Chung Jul 09 '18 at 17:24