0

I am using ggplot and geom_line to plot a graph in R, but I see that the labels are being over-written by the plot. Is there any way I can address this issue and have the labels be visible clearly? Also, I would like to have the words "Gross sales" in place of the y-axis label "value". How do I achieve that? Thank you.

Here is the code I have used to get this graph.

    library(ggrepel)
    plots_yearly<-lapply(overall_yearly_gross_sales,function(category_table){o<-melt(category_table, id = "Year", measure = c("XXXXXX","YYYYYY","ZZZZZ"));
    ggplot(o, aes(Year, value, colour = variable,label=value)) + geom_text_repel()+geom_line()+ 
    #geom_text(aes(label=value), hjust=c(0.5), vjust=c(1))
    labs(title=paste(category_table$Category,"Yearly gross sales (in $M)",sep=" "))})

enter image description here

user17144
  • 428
  • 3
  • 18
  • 1
    One option would be to use ```geom_label_repel``` - then a background and box is drawn around the labels. Or you might try other parameters like point_padding (see e.g. https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html) – Wolfgang Arnold Mar 03 '20 at 10:36

2 Answers2

1

geom_text_repel() or geom_label_repel() take many arguments which you can find here:

https://www.rdocumentation.org/packages/ggrepel/versions/0.8.1/topics/geom_label_repel

Take a look at the examples for inspiration. You could for example use a coloured box to highlight the labels, and add a connecting line to indicate the associated point on the line.

Additionally, to have the labels appear above the line you must add geom_line() before the labels.

To change the axis titles you can just add + labs(y="Gross Sales") or + ylab("Gross Sales")

0

The following worked.

plots_yearly<-lapply(overall_yearly_gross_sales,function(category_table){o<-melt(category_table, id = "Year", measure = c("XXXX","YYYY","ZZZZ"));
ggplot(o, aes(Year, value, colour = variable)) +geom_line()+     geom_label_repel(aes(label=value))+
labs(title=paste(category_table$Category,"Yearly gross sales (in $M)",sep=" "),y="Gross Sales")})

enter image description here

user17144
  • 428
  • 3
  • 18