GGMAP传奇未显示

发布于 2025-02-12 20:06:48 字数 1524 浏览 6 评论 0原文

我有两个数据范围都记录了前10个电台骑手的走了。一个是为了休闲骑手,另一个是为成员骑手。两个数据范围都包含列“站”,“ freq”,“ Latitude”,“经度”。我能够使用GGMAP绘制图形,显示两个数据范围内的电台位置,但无法显示传说。 R scripe如下所示:

library(ggplot2)
library(rstudioapi)
library(ggmap)

map_location <- c (lon =  -87.623177, lat = 41.881832)

chicago_map_zoom <- get_map (location = map_location, 
                        maptype = 'roadmap', 
                        color='bw', 
                        source='google', 
                        zoom=13,
)

chicago_plot <- ggmap(chicago_map_zoom) +
  geom_point (data = casual_top_station,
              aes (x = longitude,
                   y = latitude),
              color = "red",
              shape = 15,
              alpha = 0.5,
              size = 3) +
  geom_point (data = member_top_station,
              aes (x = longitude,
                   y = latitude),
              color = "blue",
              shape = 16,
              alpha = 0.5,
              size = 2) +
  scale_color_identity (name = "Subscription type",
                        breaks = c("red","blue"),
                        labels = c("Casual","Member"),
                        guide = "legend") +
  theme (axis.ticks = element_blank(),
         axis.text = element_blank(),
         axis.title = element_blank()) +
  labs (title = "Top 10 casual and member rider stations",
        subtitle = "Both start and end stations")

结果图: chicago_map

I have two dataframes both recording the top 10 stations riders went. One is for casual rider, the other one is for member rider. Both dataframes contain column 'station','freq','latitude','longitude'. I'm able to use ggmap to plot the graph showing the locations of the stations from both dataframes, but not able to show the legend.
R scripe is showing below:

library(ggplot2)
library(rstudioapi)
library(ggmap)

map_location <- c (lon =  -87.623177, lat = 41.881832)

chicago_map_zoom <- get_map (location = map_location, 
                        maptype = 'roadmap', 
                        color='bw', 
                        source='google', 
                        zoom=13,
)

chicago_plot <- ggmap(chicago_map_zoom) +
  geom_point (data = casual_top_station,
              aes (x = longitude,
                   y = latitude),
              color = "red",
              shape = 15,
              alpha = 0.5,
              size = 3) +
  geom_point (data = member_top_station,
              aes (x = longitude,
                   y = latitude),
              color = "blue",
              shape = 16,
              alpha = 0.5,
              size = 2) +
  scale_color_identity (name = "Subscription type",
                        breaks = c("red","blue"),
                        labels = c("Casual","Member"),
                        guide = "legend") +
  theme (axis.ticks = element_blank(),
         axis.text = element_blank(),
         axis.title = element_blank()) +
  labs (title = "Top 10 casual and member rider stations",
        subtitle = "Both start and end stations")

Result graph: Chicago_map

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

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

发布评论

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

评论(1

只是偏爱你 2025-02-19 20:06:48

而不是使用scale_color_identity ...为了设置颜色,形状和大小的值,我建议您首先将ID列列到您的data.frames
然后可以在aes内的美学上映射。之后,通过scale_xxx_manual函数系列设置所需的颜色,形状和大小。

使用一些虚假数据来点:

library(ggplot2)
library(ggmap)

casual_top_station <- data.frame(
  longitude = -87.65,
  latitude = 41.9
)

member_top_station <- data.frame(
  longitude = -87.65,
  latitude = 41.86
)

casual_top_station$id <- "Casual"
member_top_station$id <- "Member"
legend_title <- "Subscription type"

base <- ggmap(chicago_map_zoom) +
  scale_color_manual(values = c(Casual = "red", Member = "blue")) +
  scale_shape_manual(values = c(Casual = 15, Member = 16)) +
  scale_size_manual(values = c(Casual = 3, Member = 2)) +
  theme(
    axis.ticks = element_blank(),
    axis.text = element_blank(),
    axis.title = element_blank()
  ) +
  labs(
    title = "Top 10 casual and member rider stations",
    subtitle = "Both start and end stations",
    color = legend_title, shape = legend_title, size = legend_title
  )

base + 
  geom_point(
    data = casual_top_station,
    aes(
      x = longitude,
      y = latitude,
      color = id, shape = id, size = id
    ),
    alpha = 0.5
  ) +
  geom_point(
    data = member_top_station,
    aes(
      x = longitude,
      y = latitude,
      color = id, shape = id, size = id
    ),
    alpha = 0.5
  )

“”

,为了进一步简化代码例如dplyr :: bind_rows,它将仅通过一个geom_point添加您的点。

top_station <- dplyr::bind_rows(casual_top_station, member_top_station)

base + 
  geom_point(
    data = top_station,
    aes(
      x = longitude,
      y = latitude,
      color = id, shape = id, size = id
    ), alpha = .5)

“”

Instead of using scale_color_identity ... to set the values for color, shape and size I would suggest to first an id column to your data.frames
which could then be mapped on aesthetics inside aes. Afterwards set your desired colors, shapes and sizes via the scale_xxx_manual family of functions.

Using some fake data for the points:

library(ggplot2)
library(ggmap)

casual_top_station <- data.frame(
  longitude = -87.65,
  latitude = 41.9
)

member_top_station <- data.frame(
  longitude = -87.65,
  latitude = 41.86
)

casual_top_station$id <- "Casual"
member_top_station$id <- "Member"
legend_title <- "Subscription type"

base <- ggmap(chicago_map_zoom) +
  scale_color_manual(values = c(Casual = "red", Member = "blue")) +
  scale_shape_manual(values = c(Casual = 15, Member = 16)) +
  scale_size_manual(values = c(Casual = 3, Member = 2)) +
  theme(
    axis.ticks = element_blank(),
    axis.text = element_blank(),
    axis.title = element_blank()
  ) +
  labs(
    title = "Top 10 casual and member rider stations",
    subtitle = "Both start and end stations",
    color = legend_title, shape = legend_title, size = legend_title
  )

base + 
  geom_point(
    data = casual_top_station,
    aes(
      x = longitude,
      y = latitude,
      color = id, shape = id, size = id
    ),
    alpha = 0.5
  ) +
  geom_point(
    data = member_top_station,
    aes(
      x = longitude,
      y = latitude,
      color = id, shape = id, size = id
    ),
    alpha = 0.5
  )

Also, to simplify your code further I would suggest to bind both data frames by row using e.g. dplyr::bind_rows which would allow to add your points via just one geom_point.

top_station <- dplyr::bind_rows(casual_top_station, member_top_station)

base + 
  geom_point(
    data = top_station,
    aes(
      x = longitude,
      y = latitude,
      color = id, shape = id, size = id
    ), alpha = .5)

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