0

I've created a small set of data in an excel sheet, the example being baseball stats, and uploaded it to R. I have no issue creating a basic scatter plot using the data as well as changing the colors, shapes, and giving dots different colors based on where certain data came from (example being red dots for players from school a, blue bots for players from school b, etc.).

My issue is trying to give the dots labels of the name of the players the dots represent using geom_text(). I've search through different websites and YouTube videos trying to see if someone had described my problem as well as show a solution. So far I'm not having much luck.

This is the code that I've seen to be having the most "results" from:

library(ggplot2)

ggplot(sportsref_download, aes(x=HR, y=SO)) +
  geom_point() + 
  geom_text(
    label=rownames, 
    nudge_x = 0.25, nudge_y = 0.25, 
    check_overlap = T
)

So far this is the error message that keeps popping up:

Error in geom_text(): ! Problem while setting up geom aesthetics. ℹ Error occurred in the 2nd layer. Caused by error in rep(): ! attempt to replicate an object of type 'closure'

L Tyrone
  • 1,268
  • 3
  • 15
  • 24
  • 1
    Difficult to help without [a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). But "an object of type closure" usually means that you have used the name of a function where there should be a variable. I think the issue here may be `rownames`, which is a function - perhaps try `geom_text(aes(label = rownames))`. – neilfws May 11 '23 at 03:43
  • 1
    @neilfws thank you! This worked for me. Also I will do better next time to create a better reproducible example. Still new to a lot of this and trying to get the hang of things – ArkansasKing1 May 12 '23 at 01:31

0 Answers0