R: "Counts" 是否需要交互式剧情标题?

R: Are "Counts" Necessary for Interactive Plotly Titles?

我正在使用 R 编程语言。

使用以下link作为教程(https://plotly.com/r/lines-on-maps/),我能够制作一个交互式情节:

#load libraries
library(dplyr)
library(leaflet)
library(plotly)
library(data.table)

#generate data for example (longitude and latitude of cities)
lat = rnorm(100, 43, 3)
long = rnorm(100, -79, 3)

map_data = data.frame(lat, long)
map_data$type = as.factor(1:100)

#change format of the data so that it is compatible for this example 

result = rbind(
  cbind(map_data[1:nrow(map_data)-1,c(1,2)], map_data[-1,c(1,2)]),
  cbind(map_data[nrow(map_data), c(1,2)], map_data[1,c(1,2)])
)
colnames(result) <- c("start_lat", "start_long", "end_lat", "end_long")

my_data = result

my_data$type = as.factor(1:nrow(my_data))
my_data$type1 = as.character(1:100)
my_data$count = as.integer(1)
my_data$id = 1:100

#### begin visualization

# map projection
geo <- list(
    scope = 'north america',
    projection = list(type = 'azimuthal equal area'),
    showland = TRUE,
    landcolor = toRGB("gray95"),
    countrycolor = toRGB("gray80")
)

fig <- plot_geo(locationmode = 'USA-states', color = I("red"))

fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, alpha = 0.5
)


fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, hoverinfo = "text", alpha = 0.5
)


fig <- fig %>% add_segments(
    data = group_by(my_data, type),
    x = ~start_long, xend = ~end_long,
    y = ~start_lat, yend = ~end_lat,
    alpha = 0.3, size = I(1), hoverinfo = "none"
)

fig <- fig %>% layout(
    title = 'Plot 1',
    geo = geo, showlegend = FALSE, height=800
)

#final result
fig

这会产生以下结果:

现在,我正在尝试让“交互式文本”起作用:

# map projection
geo <- list(
    scope = 'north america',
    projection = list(type = 'azimuthal equal area'),
    showland = TRUE,
    landcolor = toRGB("gray95"),
    countrycolor = toRGB("gray80")
)

fig <- plot_geo(locationmode = 'USA-states', color = I("red"))

fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, alpha = 0.5
)


fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, text = ~type1, size = ~count, hoverinfo = "text", alpha = 0.5
)


fig <- fig %>% add_segments(
    data = group_by(my_data, type),
    x = ~start_long, xend = ~end_long,
    y = ~start_lat, yend = ~end_lat,
    alpha = 0.3, size = I(1), hoverinfo = "none"
)

fig <- fig %>% layout(
    title = 'Plot 1',
    geo = geo, showlegend = FALSE, height=800
)

fig

交互式文本现在可以正常工作,但数据点显得“大得多”。

我的问题:是否可以使交互式文本工作,但数据点的显示方式与第一张图片中的显示方式相同?

我最初尝试在没有“计数”变量的情况下执行此操作:

fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, text = ~type1, hoverinfo = "text", alpha = 0.5
)

但是当我这样做时,交互式文本不起作用 - 交互式文本仅在添加“计数”变量时才起作用。

这个“计数”变量是必需的吗?有人可以告诉我如何解决这个问题吗?

谢谢!

您不需要使用 count。然而,这里的段有些奇怪。无论哪种方式,这都实现了我认为您正在寻找的东西。

我提供了两个示例,因为您没有在悬停文本中说出您想要的内容。在第一个示例中,我只使用 x 和 y(纬度和经度)。在第二个中,我使用了自定义悬停内容。

创建 fig 之前的所有内容均保持不变。

显着变化:

  • fig 个元素的组装顺序;段似乎只有在标记
  • 之前才有效
  • hoverinfo 段添加现在设置为 text--这没有添加悬停内容,但由于某种原因 none 这里有一个问题...奇怪
  • 我给 fig 打了一两个电话,好像什么都没做...
  • add_markers中,这两个选项有不同的变化
    • 其中,hovertext = "text"被改成了hovertext = "lat+lon"
    • 在另一个中,有多个更改——您必须查看这个的代码
  • layout中,我删除了height参数;它被忽略了
fig <- plot_geo(locationmode = 'USA-states', color = I("red"))
fig <- fig %>% add_segments( # add segments
  data = group_by(my_data, type),
  x = ~start_long, xend = ~end_long,
  y = ~start_lat, yend = ~end_lat,
  alpha = 0.3, size = I(1), hoverinfo = "text" # changed hoverinfo
)
fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat,
  alpha = 0.5, hoverinfo = "lat+lon"           # changed hoverinfo
)
fig <- fig %>% layout(
  title = 'Plot 1',
  geo = geo, showlegend = FALSE  # removed height argument
  )

#final result
fig

这是自定义文本版本

fig <- plot_geo(locationmode = 'USA-states', color = I("red"))
fig <- fig %>% add_segments( # add segments
  data = group_by(my_data, type),
  x = ~start_long, xend = ~end_long,
  y = ~start_lat, yend = ~end_lat,
  alpha = 0.3, size = I(1), hoverinfo = "text" # changed hoverinfo
)
fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat,
  alpha = 0.5, hoverinfo = "text",          # hoverinfo unchanged
  text = ~paste0("Longitude: ",             # text changed here**
                 round(my_data$start_long, 2),
                 "<br>Latitude: ", 
                 round(my_data$start_lat, 2))
  )
fig <- fig %>% layout(
  title = 'Plot 1',
  geo = geo, showlegend = FALSE  # removed height argument
)

#final result
fig

如果您有任何问题,请告诉我!