Up front, @RuiBarradas's comment about scale_fill_manual
is both correct and misled from your code. I believe you included it with the expectation of adding your areas (survey and access) later. Perhaps you thought that it could have an effect on your color legend (or just confused them). Regardless, his point is correct: scale_fill_manual
here does nothing unless you actually use fill=
as an aes
thetic somewhere in the plot. All of the naming and such would have to be done in scale_color_manual
for your dots.
Further, you have two options for setting your labels. I believe you can use labels=
in your scale calls to set them, and this can be a named vector of c("201905" = "MAB Leg 1", ...)
. Or you can use the labels initially in the aesthetic (as I do below). They both work, but the former requires a little more data-management without ggplot.
I suggest a slightly different data organization.
Since you don't want to show the 201905
(etc) numbers, yet you want to assign labels and colors on them, I'll create a labels
frame that maps them together, and then I'll merge
them in.
Since you have scale_fill_manual
yet have assigned no fill=
aesthetics, I'll create a fake areas
frame that contains your two types of area. I'll assign them arbitrary CruiseID
values so that we can still use the labels
frame to identify them (and their fill as well).
labels <- data.frame(
CruiseID = c(201905, 201906, -1, -2),
# 'factor' to preserve the order
label = factor(c("MAB Leg 1", "MAB Leg 2", "Survey Domain", "Access Area")),
color = c("red", "blue", "#00000000", "black"),
fill = c("#00000000", "#00000000", "gray", "white")
)
areas <- data.frame(
label = c(-1, -2),
beglat = NA_real_, beglong = NA_real_)
Next, the values=
value of the scale_*_manual
functions can work with a named vector. I'll re-use labels
for this, using a small trick to turn two columns into a named vector:
labels
# CruiseID label color fill
# 1 201905 MAB Leg 1 red #00000000
# 2 201906 MAB Leg 2 blue #00000000
# 3 -1 Survey Domain #00000000 gray
# 4 -2 Access Area black white
setNames(labels$color, labels$label)
# MAB Leg 1 MAB Leg 2 Survey Domain Access Area
# "red" "blue" "#00000000" "black"
For removing the legend titles, merely add name=NULL
to the applicable scale_
calls.
The code:
ggplot() +
geom_point( # UPDATED
data = merge(data, labels, by = "CruiseID", all.x = TRUE),
aes(x = beglong, y = beglat, colour = label)) +
scale_colour_manual( # UPDATED
name = NULL,
values = setNames(labels$color, labels$label),
drop = TRUE) +
geom_polygon( # NEW, placeholder
data = merge(areas, labels, by = "CruiseID", all.x = TRUE),
aes(x = beglong, y = beglat, fill = label),
na.rm = TRUE) +
scale_fill_manual( # UPDATED
name = NULL,
values = setNames(labels$fill, labels$label),
drop = TRUE) +
xlim(-76,-71) +
ylim(36,42) +
ggtitle("2019 MAB Survey Stations") +
labs(x = "Longitude", y = "Latitude") +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title=element_text(size=14),
text=element_text(size=12),
axis.text.x=element_text(colour="black", size = 12),
axis.text.y=element_text(colour="black", size = 12),
legend.justification=c(.5,.5),
legend.background = element_rect(fill = "white", color = NA),
legend.position=c(0.8, 0.3),legend.box="vertical",
legend.margin=margin(),
legend.key = element_rect(colour = "black") # NEW
)
Finally, without editing the grob (gr
aphic ob
ject) and/or graphic-table manually, I don't think there's a way to box in the colors of one legend and not the other: the theme option legend.key
allows us to box them in, but it applies to all legends. If you don't mind the boxes around the dots, you're good. If you can't have them, then you can remove the legend.key=
option and change your "Access Area" from white to something different than the background color, so that it is visible.
labels <- data.frame(
CruiseID = c(201905, 201906, -1, -2),
# 'factor' to preserve the order
label = factor(c("MAB Leg 1", "MAB Leg 2", "Survey Domain", "Access Area")),
color = c("red", "blue", "#00000000", "black"),
fill = c("#00000000", "#00000000", "gray", "gray90")
)
ggplot() +
geom_point( # UPDATED
data = merge(data, labels, by = "CruiseID", all.x = TRUE),
aes(x = beglong, y = beglat, colour = label)) +
scale_colour_manual( # UPDATED
name = NULL,
values = setNames(labels$color, labels$label),
drop = TRUE) +
geom_polygon( # NEW, placeholder
data = merge(areas, labels, by = "CruiseID", all.x = TRUE),
aes(x = beglong, y = beglat, fill = label),
na.rm = TRUE) +
scale_fill_manual( # UPDATED
name = NULL,
values = setNames(labels$fill, labels$label),
drop = TRUE) +
xlim(-76,-71) +
ylim(36,42) +
ggtitle("2019 MAB Survey Stations") +
labs(x = "Longitude", y = "Latitude") +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title=element_text(size=14),
text=element_text(size=12),
axis.text.x=element_text(colour="black", size = 12),
axis.text.y=element_text(colour="black", size = 12),
legend.justification=c(.5,.5),
legend.background = element_rect(fill = "white", color = NA),
legend.position=c(0.8, 0.3),legend.box="vertical",
legend.margin=margin()
)
+ scale_fill_manual(name = NULL, ...)
? (for removing the legend title) – r2evansscale_fill_manual
cannot be recognized without an aesthetic mappingfill
to a variable. Inaes(.)
you havecolor
, notfill
. – Rui Barradas