0

I'm trying to figure out two problems in R ggplot:

  1. Show only data labels for every N day/data point
  2. Highlight (make the line bigger and/or dotted) for a specific variable

My code is below:

gplot(data = sales, 
        aes(x = dates, y = volume, colour = Country, size = ifelse(Country=="US", 1, 0.5) group = Country)) + 
        geom_line() + 
        geom_point() +
        geom_text(data = sales, aes(label=volume), size=3, vjust = -0.5)

I can't find out a way how to space the data labels as currently they are being shown for each data point per every day and it's very hard to read the plot.
As for #2, unfortunately, the size with ifelse doesn't work as 'US' line is becoming super huge and I can't change that size not matter what I specify in the first parameter of ifelse.

Would appreciate any help!

Tart
  • 305
  • 1
  • 6
  • 20
  • Can you provide a reproducible example of the dataset ? see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Mar 25 '20 at 16:19

1 Answers1

1

As no data was provided the solution is probably not perfect, but nonetheless shows you the general approach. Try this:

sales_plot <- sales %>% 
  # Create label
  # e.g. assuming dates are in Date-Format labels are "only" created for even days
  mutate(label = ifelse(lubridate::day(dates) %% 2 == 0, volume, ""))

ggplot(data = sales_plot, 
       # To adjust the size: Simply set labels. The actual size is set in scale_size_manual
      aes(x = dates, y = volume, colour = Country, size = ifelse(Country == "US", "US", "other"), group = Country)) + 
  geom_line() + 
  geom_point() +
  geom_text(aes(label = label), size = 3, vjust = -0.5) +
  # Set the size according the labels
  scale_size_manual(values = c(US = 2, other = .9))
stefan
  • 90,330
  • 6
  • 25
  • 51
  • thanks a lot, works like a charm! The only two questions: is it possible to keep only "Country" colored lines in the legend. Right now it also shows "ifelse" statement with lines' sizes: ifelse(Country == "US", "US", "other"). And some data labels are overlapping others - is there a way to separate them? – Tart Mar 25 '20 at 19:31
  • 1
    Simple way is to add `+ guides(size = FALSE, label = FALSE)` to suppress the legends for size and text labels. Concerning your second question. There are several options. However, depending on the number of lines to plot and labels it is in general difficult to avoid some overlapping. One basic option is to plot only every third or fifth label. Another option would be to use geom_text_repel from the ggrepel package. This will automatically do its best to avoid overlapping of data and labels. – stefan Mar 25 '20 at 19:46
  • Thank you so much! I will play with geom_text_repel, will see how it goes :-) – Tart Mar 25 '20 at 19:54