2

I am creating a scatter plot using ggplot/geom_point. Here is my code for building the function in ggplot.

AddPoints <- function(x) {
    list(geom_point(data = dat , mapping =  aes(x = x, y = y) , shape = 1 , size = 1.5 ,
color = "blue"))
} 

I am wondering if it would be possible to replace the standard points on the plot with numbers. That is, instead of seeing a dot on the plot, you would see a number on the plot to represent each observation. I would like that number to correspond to a column for that given observation (column name 'RP'). Thanks in advance.

Sample data.

 Data <- data.frame(
    X = sample(1:10),
     Y = sample(3:12),
    RP = sample(c(4,8,9,12,3,1,1,2,7,7)))
4
  • It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.
    – MrFlick
    Nov 12, 2020 at 19:17
  • 2
    geom_text seems useful here.
    – r2evans
    Nov 12, 2020 at 19:17
  • @MrFlick please see updated sample data
    – 887
    Nov 12, 2020 at 19:27
  • 2
    BTW, the function argument is just x, but the contents references dat and y as well. Also, your additional sample data uses upper-case X and Y, so it isn't clear what is what here. Regardless, I think geom_text(aes(x=Y, y=Y, label=RP), data=x) might suffice.
    – r2evans
    Nov 12, 2020 at 19:28

1 Answer 1

4

Use geom_text() and map the rp variable to the label argument.

ggplot(Data, aes(x = X, y = Y, label = RP)) +
  geom_text()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.