Labels missing on ggplot graph returned from function

I have a function to generate plots that includes labels for some of the points. When I run the code outside of a function call everything looks good, but when I return the plot from the function and then display it the labels are gone.

myFun <- function(data){
...

diff.out, numDiff produced from data input

...

p1 <- ggplot(diff.out, aes(x = log2FoldChange, y = -log10(padj), label = text.label)) + geom_point(aes(color = factor(point.color))) + scale_color_manual(labels = labs,values = colors ) + geom_text_repel(aes(label = diff.out$text.label), nudge_y = 0.01, size = 3) + labs(color =(paste0("Diff. Exp. Genes (", sum(numDiff[c("down", "up")]), ")")))

return(p1)
}

Running the plotting code gives plot with labels

p1 <- ggplot(......)
print(p1)

#Running the function and returning the plot gives plot without labels
myPlot <- myFun(data)
print(myPlot)

Any advice would be greatly appreciated! I am new to ggplot2...

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

There's also a nice FAQ on how to do a minimal reprex for beginners, below:

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.