Home > Mobile >  R: Adding Labels to a Point on a Scatterplot
R: Adding Labels to a Point on a Scatterplot

Time:09-01

I am working with the R programming language. I would like to make a scatterplot using the plotly library and add labels to each of the points.

Here is the data I have:

library(ggplot2)
library(plotly)
library(ggrepel)

a = rnorm(10,100,100)
b = rnorm(10,100,100)

# imagine that the labels are only available for some of the points
labels = c("FKUEV3116M", "PUGOV5350W", "ILRBL1594B", "ZCWUS4589U", "ZRWJN8129J", 
"FYNMZ0695L", "LTMGK4676V", NA, NA, NA)

my_data = data.frame(a,b, labels)

I know how to do this using ggplot2 and then convert the result into a plotly object:

d = ggplot(my_data, aes(x=a, y=b))  
    geom_point()   
    geom_text(label=labels, position=position_jitter(width=1,height=1))

ggplotly(d)

But is it possible to do this using the plotly library itself and adding "jitter" to the labels? I tried the following code and it is not working:

d = ggplot(my_data, aes(x=a, y=b))  
  geom_point()   
  geom_text(label=labels)   
    geom_jitter(position = position_jitter(seed = 1))  
    geom_text(position = position_jitter(seed = 1))

ggplotly(d)

Can someone please tell me what I am doing wrong?

Thanks!

Note: Alternate Way I Tried:

# note: unfortunately, this does not support the "ggrepel" function 
d = ggplot(my_data, aes(x=a, y=b))  
    geom_point()      geom_text_repel(aes(x = a, 
                                                    y = b, 
                                                    label = labels))

CodePudding user response:

This would add the jitter in to the labels and propagate to the plotly plot. You can change the height and width to give more or less random variation in the labels.

library(dplyr)
library(ggplot2)
library(plotly)
a = rnorm(10,100,100)
b = rnorm(10,100,100)

# imagine that the labels are only available for some of the points
labels = c("FKUEV3116M", "PUGOV5350W", "ILRBL1594B", "ZCWUS4589U", "ZRWJN8129J", 
           "FYNMZ0695L", "LTMGK4676V", NA, NA, NA)

my_data = data.frame(a,b, labels)

d = ggplot(my_data, aes(x=a, y=b))  
     geom_point()   
     geom_text(aes(label=labels), position=position_jitter(15, 15, seed=1))
ggplotly(d)

Created on 2022-08-30 by the reprex package (v2.0.1)

An alternative option would be to put the label in the tooltip. You could then just plot the points and let viewers hover over the points to identify the labels.

  my_data <- my_data %>% 
    mutate(txt = paste0("A: ", round(a, 3), 
                        "\nB: ", round(b, 3), 
                        "\nLabel: ", labels))
  d = ggplot(my_data, aes(x=a, y=b, text=txt))  
       geom_point() 
       
  ggplotly(d, tooltip="text")

CodePudding user response:

In plotly you can use the argument text and add_markers with add_text like this:

library(plotly)

a = rnorm(10,100,100)
b = rnorm(10,100,100)

# imagine that the labels are only available for some of the points
labels = c("FKUEV3116M", "PUGOV5350W", "ILRBL1594B", "ZCWUS4589U", "ZRWJN8129J", 
           "FYNMZ0695L", "LTMGK4676V", NA, NA, NA)

my_data = data.frame(a,b, labels)

plot_ly(data = my_data, x= ~a, y = ~b, text = ~labels) %>%
  add_markers() %>%
  add_text(textposition = "top right") %>%
  layout(showlegend = FALSE)

Created on 2022-08-31 with reprex v2.0.2

  • Related