You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言Plotly地图中将标记改为方形或三角形(非圆形)

解决Plotly中add_markers标记形状改为方形的问题

嘿,我懂你想把plotly地图上的圆形标记换成方形的需求啦!你之前试的type=list("squares")找错了参数入口,plotly里控制标记形状的是marker参数下的symbol属性,不是type哦。

正确修改方案

你只需要在add_markers()里添加marker=list(symbol="square")就能实现方形标记了。我把你的代码补全并调整好,完整代码如下:

require(plotly)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_ebola.csv')
# 限制时间为6-9月
df <- subset(df, Month %in% 6:9)
# 把月份转为有序因子(补全你截断的代码部分)
df$Month <- factor(df$Month, levels = 6:9, labels = c("June", "July", "August", "September"), ordered = TRUE)

# 绘制地图并使用方形标记
plot_geo(df, locationmode = "country names") %>%
  add_markers(
    x = ~Longitude,
    y = ~Latitude,
    size = ~Cases,
    color = ~Month,
    text = ~paste(Country, Month, "Cases:", Cases),
    # 关键设置:把标记形状改为方形
    marker = list(symbol = "square")
  ) %>%
  layout(title = "2014年6-9月埃博拉病例分布")

额外小技巧

除了square,plotly还支持不少其他标记符号,比如:

  • square-open:空心方形
  • diamond:实心菱形
  • cross:十字形
  • x:X形
    你可以根据需求替换symbol的值来切换标记样式。

内容的提问来源于stack exchange,提问作者ClimateUnboxed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:50:23