R:是“计数”吗?交互式情节标题有必要吗?

发布于 2025-01-09 13:07:52 字数 3349 浏览 1 评论 0原文

我正在使用 R 编程语言。

使用以下链接作为教程(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

这会产生以下结果:

<图片src="https://i.sstatic.net/B7DYl.png" alt="在此处输入图像描述">

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

# 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

在此处输入图像描述< /a>

交互式文本现在是有效,但数据点显得“庞大得多”。

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

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

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

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

这个“count”变量有必要吗?有人可以告诉我如何解决这个问题吗?

谢谢!

I am working with the R Programming language.

Using the following link as a tutorial (https://plotly.com/r/lines-on-maps/), I was able to make an interactive plot:

#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

This produces the following result:

enter image description here

Now, I am trying to get the "interactive text" to work:

# 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

enter image description here

The interactive text is now working, but the data points are appearing "much bulkier".

My Question: Is it possible to make the interactive text work, but have the data points appear the same way they do in the first picture?

I originally tried to do this without a "count" variable:

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

But when I do this, the interactive text isn't working - the interactive text only works when a "count" variable is added.

Is this "count" variable necessary? Can someone please show me how to fix this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2025-01-16 13:07:52

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

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

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

显着变化:

  • fig 元素的组装顺序;分段似乎只有在
  • 分段添加的标记 hoverinfo 之前才起作用,现在将其设置为 text - 这不会添加悬停内容,但由于某种原因 < code>none 这里有一个问题...奇怪的是
  • 我放弃了对 fig 或两个的调用,这似乎没有做任何事情...
  • add_markers 中,这在两个选项中发生了不同的变化
    • 其中,hovertext = "text" 更改为 hovertext = "lat+lon"
    • 在另一个中,有多个更改 - 您必须查看这个的代码
  • 中,我删除了 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

在此处输入图像描述

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

You don't need to use count. However, there is something odd here with the segments. Either way, this achieves what I think you're looking for.

I have provided two examples because you didn't say what you wanted to have in the hover text. In the first example, I just use the x and y (lat and long). In the second, I used custom hover content.

Everything that precedes the creation of fig was left unchanged.

Notable changes:

  • the order the fig elements are assembled; segments seems to only work if it is before the markers
  • hoverinfo for the segments add is now set to text--this didn't add hover content, but for some reason none here was a problem...odd
  • I dropped a call to fig or two, that seemed to be doing nothing...
  • in add_markers, this changed differently in the two options
    • in one, hovertext = "text" was changed to hovertext = "lat+lon"
    • in the other, there were multiple changes--you'll have to look at the code for this one
  • in layout, I deleted the height argument; it's ignored
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

enter image description here

Here's the custom text version

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

enter image description here

Let me know if you have any questions!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文