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

如何在R语言中按酒类品类可视化门店位置及销量分布?

解决方案:按酒类品类细分的地理销量可视化

先修正经纬度提取代码的笔误

你之前的代码里把data写成了ata,先修正这部分:

data <- data %>% 
  separate(remove = FALSE,
           col = 'Store Location', sep = " ",
           into = c("toss-it", "Latitude", "Longitude")) %>%
  mutate(
    Latitude = parse_number(Latitude),
    Longitude = parse_number(Longitude)
  )

第一步:聚合门店-品类的销量数据

原始数据是单条订单记录,需要先按门店+品类聚合总销量,方便后续可视化:

library(dplyr)

# 按门店、品类聚合总销量(假设销量列是`Sale (Dollars)`,该数据集默认列名)
sales_by_store_category <- data %>%
  group_by(`Store Number`, `Store Name`, Latitude, Longitude, `Category Name`) %>%
  summarise(total_sales = sum(`Sale (Dollars)`, na.rm = TRUE),
            .groups = "drop")

第二步:可视化实现(两种方案)

方案1:分面展示各品类分布(标签清晰不拥挤)

如果品类数量较多,分面是最清晰的展示方式,每个品类单独一个子图:

library(ggplot2)
library(scales)

ggplot(sales_by_store_category, aes(x = Longitude, y = Latitude)) +
  # 点大小映射销量,颜色映射品类
  geom_point(aes(size = total_sales, color = `Category Name`), alpha = 0.7, na.rm = TRUE) +
  # 按品类分面,每个品类独立展示
  facet_wrap(~`Category Name`, scales = "free") +
  # 销量标签格式化为美元
  scale_size_continuous(labels = dollar_format()) +
  # 设置标签和标题
  labs(
    x = "经度",
    y = "纬度",
    size = "总销量",
    color = "酒类品类",
    title = "艾姆斯市各酒类品类门店销量地理分布"
  ) +
  theme_minimal() +
  # 调整字体大小,保证标签可读
  theme(
    axis.text = element_text(size = 8),
    legend.text = element_text(size = 8),
    strip.text = element_text(size = 10, face = "bold"),
    plot.title = element_text(hjust = 0.5, size = 12)
  )

方案2:单图展示+防重叠标签(适合品类较少的情况)

如果只关注核心品类,单图结合颜色区分品类,同时用防重叠标签展示门店名称:

library(ggplot2)
library(scales)
library(ggrepel) # 用于避免标签重叠

# 先筛选销量前10的核心品类,避免图面拥挤
top_categories <- sales_by_store_category %>%
  group_by(`Category Name`) %>%
  summarise(total_category_sales = sum(total_sales, na.rm = TRUE)) %>%
  arrange(desc(total_category_sales)) %>%
  slice(1:10) %>%
  pull(`Category Name`)

filtered_data <- sales_by_store_category %>%
  filter(`Category Name` %in% top_categories)

# 绘制可视化
ggplot(filtered_data, aes(x = Longitude, y = Latitude)) +
  geom_point(aes(size = total_sales, color = `Category Name`), alpha = 0.7, na.rm = TRUE) +
  # 用ggrepel自动调整标签位置,避免重叠
  geom_text_repel(aes(label = `Store Name`), size = 3, na.rm = TRUE) +
  scale_size_continuous(labels = dollar_format()) +
  # 用色盲友好的颜色映射品类
  scale_color_viridis_d(option = "plasma") +
  labs(
    x = "经度",
    y = "纬度",
    size = "总销量",
    color = "酒类品类",
    title = "艾姆斯市核心酒类品类门店销量地理分布"
  ) +
  theme_minimal() +
  theme(
    legend.position = "bottom", # 图例放底部节省空间
    axis.text = element_text(size = 8),
    legend.text = element_text(size = 8),
    plot.title = element_text(hjust = 0.5, size = 12)
  )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 07:00:57