简体   繁体   English

R:向散点图上的点添加标签

[英]R: Adding Labels to a Point on a Scatterplot

I am working with the R programming language.我正在使用 R 编程语言。 I would like to make a scatterplot using the plotly library and add labels to each of the points.我想使用 plotly 库制作散点图,并为每个点添加标签。

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:我知道如何使用 ggplot2 执行此操作,然后将结果转换为 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?但是是否可以使用 plotly 库本身并在标签中添加“抖动”来做到这一点? 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))

This would add the jitter in to the labels and propagate to the plotly plot.这会将抖动添加到标签中并传播到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)reprex package (v2.0.1) 于 2022 年 8 月 30 日创建

An alternative option would be to put the label in the tooltip.另一种选择是将 label 放在工具提示中。 You could then just plot the points and let viewers hover over the points to identify the labels.然后,您可以只 plot 点并让查看者 hover 在点上识别标签。

  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")

In plotly you can use the argument text and add_markers with add_text like this:plotly ,您可以将参数textadd_markersadd_text一起使用,如下所示:

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使用reprex v2.0.2创建于 2022-08-31

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM