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

如何移除地图中的网格并将背景设置为白色?

移除地图网格并设置白色背景

静态ggplot地图解决方案

在theme()中添加两个参数即可移除网格并设置白色背景:

  • panel.grid = element_blank():移除所有网格线
  • panel.background = element_rect(fill = "white"):将绘图区域背景设为白色

修改后的完整代码:

# Plot
us = map_data("state")
religiosity = religiosity %>% mutate(State = tolower(State))
us_rel = merge(us, religiosity, by.x = "region", by.y = "State")
p1 = ggplot(us_rel, aes(x = long, y = lat, group = group, fill = Rank)) +
  geom_polygon() +
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(),
        axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(),
        panel.grid = element_blank(),  # 移除网格
        panel.background = element_rect(fill = "white")) +  # 设置白色背景
  scale_fill_gradient(low = "azure3", high = "deeppink4") +
  labs(title = "Religiosity by State", fill = "Rank")
p1

交互式ggplotly地图解决方案

静态图修改后转成ggplotly可能仍残留网格,需额外通过layout()配置坐标轴和背景:

  • xaxis = list(showgrid = FALSE)、yaxis = list(showgrid = FALSE):移除坐标轴网格
  • plot_bgcolor = "white":设置绘图区域背景为白色

修改后的完整代码:

# Interactive Plot
ip1 = ggplotly(p1) %>% 
  layout(
    xaxis = list(showgrid = FALSE),
    yaxis = list(showgrid = FALSE),
    plot_bgcolor = "white"
  )
ip1

内容的提问来源于stack exchange,提问作者Data and AI Nerd

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 23:02:46