Reputation: 114
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
Upvotes: 1
Views: 213
Reputation: 124213
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)
Upvotes: 1