ggplot2玫瑰图中星巴克门店分布图示例分析

发布时间:2022-01-15 17:19:09 作者:柒染
来源:亿速云 阅读:198
# ggplot2玫瑰图中星巴克门店分布图示例分析

## 引言

玫瑰图(Rose Diagram)又称极坐标条形图,是数据可视化中展示周期性或方向性数据的有效工具。本文将使用R语言的`ggplot2`包,结合`tidyverse`生态系统,演示如何构建星巴克全球门店分布的玫瑰图分析。通过本案例,读者将掌握:

1. 玫瑰图的基本原理与适用场景
2. 使用`ggplot2`创建高级玫瑰图的完整流程
3. 空间分布数据的预处理技巧
4. 可视化定制与美学优化方法

## 一、数据准备与预处理

### 1.1 数据来源

我们使用Kaggle上的公开数据集:
- [星巴克门店位置数据集](https://www.kaggle.com/starbucks/store-locations)
- 包含全球76个国家/地区的星巴克门店地理坐标数据

```r
library(tidyverse)
library(ggplot2)
library(sf)
library(ggspatial)

# 数据加载
starbucks <- read_csv("starbucks_locations.csv") %>% 
  filter(!is.na(Longitude), !is.na(Latitude))

1.2 地理坐标转换

将经纬度转换为笛卡尔坐标,并计算每个门店的角度(用于玫瑰图分区):

earth_radius <- 6371 # 地球半径(km)

starbucks <- starbucks %>%
  mutate(
    x = earth_radius * cos(Latitude*pi/180) * cos(Longitude*pi/180),
    y = earth_radius * cos(Latitude*pi/180) * sin(Longitude*pi/180),
    angle = atan2(y, x) * 180/pi %% 360 # 转换为0-360度
  )

1.3 创建角度分箱

将360度圆周分为24个扇形区域(每15度一个区间):

bins <- 24
angle_breaks <- seq(0, 360, length.out = bins + 1)

starbucks <- starbucks %>%
  mutate(
    angle_bin = cut(angle, breaks = angle_breaks, 
                   labels = 1:24, include.lowest = TRUE)
  )

二、基础玫瑰图构建

2.1 数据聚合

统计每个扇形区域的门店数量:

rose_data <- starbucks %>%
  count(angle_bin, .drop = FALSE) %>%
  mutate(
    angle_mid = (as.numeric(angle_bin) - 0.5) * 360/bins
  )

2.2 基础绘图

使用ggplot2的极坐标系统创建玫瑰图:

ggplot(rose_data, aes(x = angle_bin, y = n, fill = n)) +
  geom_bar(stat = "identity", width = 1, color = "white") +
  coord_polar(start = -pi/24) + # 旋转起始角度
  scale_fill_viridis_c(option = "plasma") +
  labs(title = "星巴克全球门店方向分布") +
  theme_minimal()

ggplot2玫瑰图中星巴克门店分布图示例分析

图1:基础玫瑰图展示门店数量在不同方向的分布

三、高级可视化增强

3.1 添加地理参考线

world_map <- st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))

ggplot() +
  geom_sf(data = world_map, fill = "gray95", color = "gray70") +
  geom_point(data = starbucks, aes(x = Longitude, y = Latitude), 
             size = 0.3, alpha = 0.2, color = "red") +
  annotation_scale(location = "bl") +
  theme_void()

3.2 复合玫瑰图

结合地理分布与玫瑰图:

p_rose <- ggplot(rose_data) +
  geom_col(aes(x = angle_bin, y = n, fill = n), 
           width = 1, show.legend = FALSE) +
  geom_text(aes(x = angle_bin, y = max(n)*1.1, 
                label = ifelse(n > quantile(n, 0.9), n, "")),
            size = 3) +
  coord_polar() +
  scale_fill_gradientn(colors = RColorBrewer::brewer.pal(9, "YlOrRd")) +
  theme_void()

p_map <- ggplot() +
  geom_sf(data = world_map, fill = NA, color = "gray60") +
  geom_point(data = starbucks, aes(x = Longitude, y = Latitude),
             size = 0.1, color = "darkred", alpha = 0.3) +
  theme_void() +
  theme(panel.background = element_rect(fill = "transparent"))

library(patchwork)
p_rose + inset_element(p_map, left = 0.5, bottom = 0.5, right = 1, top = 1)

ggplot2玫瑰图中星巴克门店分布图示例分析

图2:玫瑰图与地理分布的复合可视化

四、深度分析维度

4.1 按国家/地区分层

top_countries <- starbucks %>%
  count(Country, sort = TRUE) %>%
  head(5) %>% pull(Country)

starbucks %>%
  filter(Country %in% top_countries) %>%
  count(Country, angle_bin) %>%
  ggplot(aes(x = angle_bin, y = n, fill = Country)) +
  geom_col(position = "dodge", color = NA) +
  coord_polar() +
  facet_wrap(~Country, nrow = 2) +
  theme(axis.text = element_blank())

4.2 时间趋势分析

starbucks %>%
  mutate(year = lubridate::year(`Opened Date`)) %>%
  filter(year >= 2010) %>%
  count(year, angle_bin) %>%
  ggplot(aes(x = angle_bin, y = n, fill = year)) +
  geom_col() +
  coord_polar() +
  scale_fill_continuous(type = "viridis") +
  transition_time(year) +
  labs(title = "年份: {frame_time}")

五、技术细节解析

5.1 坐标系统转换

coord_polar()参数详解: - theta:指定极坐标角度变量(默认”x”) - start:起始角度偏移(弧度制) - direction:1为顺时针,-1为逆时针

5.2 颜色映射优化

使用scale_fill_*系列函数时的注意事项:

scale_fill_gradient2(
  low = "blue", mid = "white", high = "red",
  midpoint = median(rose_data$n),
  limits = c(0, max(rose_data$n))

5.3 交互式增强

使用plotly创建交互版本:

library(plotly)
ggplotly(
  tooltip = c("angle_bin", "n"),
  originalData = FALSE
)

六、商业洞察

6.1 分布模式解读

6.2 选址策略分析

七、完整代码示例

library(tidyverse)
library(ggplot2)
library(sf)

# 数据预处理
starbucks_clean <- starbucks %>%
  filter(!is.na(Longitude), !is.na(Latitude)) %>%
  mutate(
    angle = atan2(
      cos(Latitude*pi/180) * sin(Longitude*pi/180),
      cos(Latitude*pi/180) * cos(Longitude*pi/180)
    ) * 180/pi %% 360,
    angle_bin = cut(angle, breaks = seq(0, 360, length.out = 25),
                   include.lowest = TRUE)
  )

# 高级可视化
ggplot(starbucks_clean, aes(x = angle_bin, fill = ..count..)) +
  geom_bar(width = 1, color = "white", linewidth = 0.1) +
  coord_polar(start = -pi/24) +
  scale_fill_viridis_c(option = "magma", begin = 0.2) +
  labs(
    title = "星巴克全球门店方向分布玫瑰图",
    subtitle = "基于24个方向分区的门店数量统计",
    caption = "数据来源: Kaggle Starbucks Locations Dataset"
  ) +
  theme_minimal() +
  theme(
    axis.title = element_blank(),
    axis.text.y = element_blank(),
    panel.grid.major.y = element_line(color = "gray90", linewidth = 0.2),
    plot.background = element_rect(fill = "white", color = NA)
  )

结论

通过本案例我们展示了: 1. 玫瑰图在空间分布分析中的独特价值 2. ggplot2实现复杂极坐标可视化的完整流程 3. 地理空间数据与统计图表的融合方法

未来可扩展方向: - 结合OpenStreetMap底图增强地理参考 - 加入3D渲染提升视觉冲击力 - 开发交互式参数调节界面

“数据可视化不仅是技术实现,更是认知框架的构建过程。” — Hadley Wickham “`

注:实际使用时需要: 1. 替换示例数据路径为实际数据源 2. 调整图形参数适应具体显示设备 3. 补充引用文献和具体数据版本信息 4. 图片链接需替换为实际生成的图表URL

推荐阅读:
  1. python如何绘制玫瑰
  2. UML序列图中消息和约束概念的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ggplot2

上一篇:怎么用ES做Redis监控

下一篇:springboot整合quartz定时任务框架的方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》