Reputation: 119
I have an issue related to displaying legends. I am not even sure it is something I can solve with ggplot, but since I am quite a basic user of R, I am confident it is just my ignorance to speak for me.
The Setting
I am trying to plot two distinct sets of points from two distinct dataset, over the same sets of aesthetics: x, y, size. The two sets of points change in the vertical nudge with respect to the y axis, and with respect to their shape.
The issue
The plot comes out as expected (see image below the code). The only thing I have not been able to fix is related to the legends. In particular, I would like to have the size legend to display both the shapes I am using in the chart (in this case, both circles and triangles). Is there any way to do so? I browsed a bit in the internet, but I did not find any question dealing with this issue.
Data and Code
Here I put the data I am using as a reproducible example (obtained with dput()
).
# Packages
library(tidyverse)
library(ggtext)
library(janitor)
library(delabj)
library(wesanderson)
library(forcats)
# Basic data
basedata<-structure(list(country = structure(c("Argentina", "Argentina",
"Argentina", "Argentina", "Argentina", "Argentina", "Argentina",
"Argentina", "Argentina", "Argentina", "Argentina", "Argentina",
"Argentina", "Argentina"), format.stata = "%44s"), iso = structure(c("ARG",
"ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG",
"ARG", "ARG", "ARG", "ARG"), label = "iso_3", format.stata = "%9s"),
region = structure(c("Latin America & Caribbean", "Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean"), label = "Region", format.stata = "%26s"),
income_group = structure(c("Upper middle income", "Upper middle income",
"Upper middle income", "Upper middle income", "Upper middle income",
"Upper middle income", "Upper middle income", "Upper middle income",
"Upper middle income", "Upper middle income", "Upper middle income",
"Upper middle income", "Upper middle income", "Upper middle income"
), label = "Income group", format.stata = "%19s"), gdp = structure(c(519871519807.795,
519871519807.795, 519871519807.795, 519871519807.795, 519871519807.795,
519871519807.795, 519871519807.795, 519871519807.795, 519871519807.795,
519871519807.795, 519871519807.795, 519871519807.795, 519871519807.795,
519871519807.795), label = "(first) gdp_o", format.stata = "%9.0g"),
assessment = structure(c(2, 2, 1, 1, 2, 1, 2, 1, 1, 1, 1,
1, 2, 1), label = "Initial assessment", format.stata = "%12.0g", labels = c(liberalising = 1,
restrictive = 2)), start_date = structure(c(18341, 18349,
18354, 18366, 18393, 18393, 18270, 18270, 18339, 18354, 18354,
18393, 18393, 18351), label = "Announcement date", class = "Date", format.stata = "%td"),
duration = structure(c(357, 349, 344, 63, 33, 305, 156, 156,
87, 62, 344, 305, 33, 359), format.stata = "%9.0g"), GTAinterventiontype = structure(c("Export licensing requirement",
"Export licensing requirement", "Import Internal taxation",
"Import Internal taxation", "Import Internal taxation", "Import Internal taxation",
"Import licensing requirement", "Import licensing requirement",
"Import licensing requirement", "Import licensing requirement",
"Import tariff", "Import tariff", "Import tariff", "Import-related, Unknown measure"
), label = "GTA intervention type", format.stata = "%38s"),
any_food = structure(c(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0), label = "Product: Food", format.stata = "%8.0g", labels = c(`FALSE` = 0,
`TRUE` = 1)), any_medical = structure(c(1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), label = "Product: Any medical product", format.stata = "%8.0g", labels = c(`FALSE` = 0,
`TRUE` = 1)), food_position = structure(c("Net Exporter",
"Net Exporter", "Net Exporter", "Net Exporter", "Net Exporter",
"Net Exporter", "Net Exporter", "Net Exporter", "Net Exporter",
"Net Exporter", "Net Exporter", "Net Exporter", "Net Exporter",
"Net Exporter"), format.stata = "%12s"), meds_position = structure(c("Net Importer",
"Net Importer", "Net Importer", "Net Importer", "Net Importer",
"Net Importer", "Net Importer", "Net Importer", "Net Importer",
"Net Importer", "Net Importer", "Net Importer", "Net Importer",
"Net Importer"), format.stata = "%12s"), month = c(3, 3,
4, 4, 5, 5, 1, 1, 3, 4, 4, 5, 5, 3), Announcement = c("March",
"March", "April", "April", "May", "May", "January", "January",
"March", "April", "April", "May", "May", "March"), Domain = c("Medical",
"Medical", "Medical", "Medical", "Medical", "Medical", "Food & Meds",
"Food & Meds", "Medical", "Medical", "Medical", "Medical",
"Medical", "Medical"), gdp_group_avg = c("Low Income", "Low Income",
"Low Income", "Low Income", "Low Income", "Low Income", "Low Income",
"Low Income", "Low Income", "Low Income", "Low Income", "Low Income",
"Low Income", "Low Income"), gdp_group_med = c("High Income",
"High Income", "High Income", "High Income", "High Income",
"High Income", "High Income", "High Income", "High Income",
"High Income", "High Income", "High Income", "High Income",
"High Income"), shp_point = c(23, 23, 21, 21, 23, 21, 23,
21, 21, 21, 21, 21, 23, 21), length_cat = c("More than 3 months",
"More than 3 months", "More than 3 months", "[1-3] months",
"[1-3] months", "More than 3 months", "More than 3 months",
"More than 3 months", "[1-3] months", "[1-3] months", "More than 3 months",
"More than 3 months", "[1-3] months", "More than 3 months"
), type = structure(c(2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 1L), .Label = c("Liberalizig", "Restrictive"
), class = "factor")), row.names = c(NA, -14L), class = "data.frame")
# Subset 1 - assessment == 1
prova1<-structure(list(country = c("Argentina", "Argentina", "Argentina",
"Argentina", "Argentina", "Argentina", "Argentina", "Argentina",
"Argentina"), iso = c("ARG", "ARG", "ARG", "ARG", "ARG", "ARG",
"ARG", "ARG", "ARG"), region = c("Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean", "Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean", "Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean"), income_group = c("Upper middle income",
"Upper middle income", "Upper middle income", "Upper middle income",
"Upper middle income", "Upper middle income", "Upper middle income",
"Upper middle income", "Upper middle income"), gdp = c(519871519807.795,
519871519807.795, 519871519807.795, 519871519807.795, 519871519807.795,
519871519807.795, 519871519807.795, 519871519807.795, 519871519807.795
), assessment = c(1, 1, 1, 1, 1, 1, 1, 1, 1), start_date = structure(c(18354,
18366, 18393, 18270, 18339, 18354, 18354, 18393, 18351), class = "Date"),
duration = c(344, 63, 305, 156, 87, 62, 344, 305, 359), GTAinterventiontype = c("Import Internal taxation",
"Import Internal taxation", "Import Internal taxation", "Import licensing requirement",
"Import licensing requirement", "Import licensing requirement",
"Import tariff", "Import tariff", "Import-related, Unknown measure"
), any_food = c(0, 0, 0, 1, 0, 0, 0, 0, 0), any_medical = c(1,
1, 1, 1, 1, 1, 1, 1, 1), food_position = c("Net Exporter",
"Net Exporter", "Net Exporter", "Net Exporter", "Net Exporter",
"Net Exporter", "Net Exporter", "Net Exporter", "Net Exporter"
), meds_position = c("Net Importer", "Net Importer", "Net Importer",
"Net Importer", "Net Importer", "Net Importer", "Net Importer",
"Net Importer", "Net Importer"), month = c(4, 4, 5, 1, 3,
4, 4, 5, 3), Announcement = c("April", "April", "May", "January",
"March", "April", "April", "May", "March"), Domain = c("Medical",
"Medical", "Medical", "Food & Meds", "Medical", "Medical",
"Medical", "Medical", "Medical"), gdp_group_avg = c("Low Income",
"Low Income", "Low Income", "Low Income", "Low Income", "Low Income",
"Low Income", "Low Income", "Low Income"), gdp_group_med = c("High Income",
"High Income", "High Income", "High Income", "High Income",
"High Income", "High Income", "High Income", "High Income"
), shp_point = c(21, 21, 21, 21, 21, 21, 21, 21, 21), length_cat = c("More than 3 months",
"[1-3] months", "More than 3 months", "More than 3 months",
"[1-3] months", "[1-3] months", "More than 3 months", "More than 3 months",
"More than 3 months"), type = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("Liberalizig", "Restrictive"
), class = "factor")), row.names = c(3L, 4L, 6L, 8L, 9L,
10L, 11L, 12L, 14L), class = "data.frame")
# Subset 2 - assessment == 2
prova2<-structure(list(country = c("Argentina", "Argentina", "Argentina",
"Argentina", "Argentina"), iso = c("ARG", "ARG", "ARG", "ARG",
"ARG"), region = c("Latin America & Caribbean", "Latin America & Caribbean",
"Latin America & Caribbean", "Latin America & Caribbean", "Latin America & Caribbean"
), income_group = c("Upper middle income", "Upper middle income",
"Upper middle income", "Upper middle income", "Upper middle income"
), gdp = c(519871519807.795, 519871519807.795, 519871519807.795,
519871519807.795, 519871519807.795), assessment = c(2, 2, 2,
2, 2), start_date = structure(c(18341, 18349, 18393, 18270, 18393
), class = "Date"), duration = c(357, 349, 33, 156, 33), GTAinterventiontype = c("Export licensing requirement",
"Export licensing requirement", "Import Internal taxation", "Import licensing requirement",
"Import tariff"), any_food = c(0, 0, 0, 1, 0), any_medical = c(1,
1, 1, 1, 1), food_position = c("Net Exporter", "Net Exporter",
"Net Exporter", "Net Exporter", "Net Exporter"), meds_position = c("Net Importer",
"Net Importer", "Net Importer", "Net Importer", "Net Importer"
), month = c(3, 3, 5, 1, 5), Announcement = c("March", "March",
"May", "January", "May"), Domain = c("Medical", "Medical", "Medical",
"Food & Meds", "Medical"), gdp_group_avg = c("Low Income", "Low Income",
"Low Income", "Low Income", "Low Income"), gdp_group_med = c("High Income",
"High Income", "High Income", "High Income", "High Income"),
shp_point = c(23, 23, 23, 23, 23), length_cat = c("More than 3 months",
"More than 3 months", "[1-3] months", "More than 3 months",
"[1-3] months"), type = structure(c(2L, 2L, 2L, 2L, 2L), .Label = c("Liberalizig",
"Restrictive"), class = "factor")), row.names = c(1L, 2L,
5L, 7L, 13L), class = "data.frame")
Notice that prova1
and prova2
are just two subset of basedata
. The code of the plot is the followig
countrydata %>%
ggplot(aes(x = start_date, y = fct_rev(GTAinterventiontype), shape = type)) +
geom_point(data = prova1, aes(color = fct_rev(GTAinterventiontype),size=duration, shape = fct_rev(type)), alpha = 0.65, position = position_nudge(y = +0.05)) +
geom_point(data = prova2, aes(color = fct_rev(GTAinterventiontype),size=duration, shape = fct_rev(type)), alpha = 0.65, position = position_nudge(y = -0.05)) +
scale_shape(drop=FALSE) +
guides(color = FALSE,
shape = guide_legend(order = 1, nrow = 2, ncol = 1),
size = guide_legend(order = 2)) +
delabj::theme_delabj() +
delabj::scale_color_delabj() +
#delabj::legend_none() +
labs(shape = 'Type',
size = "Duration",
x="",
y="",
title = paste("ARG", "Med-related Measures by Announcement date", sep = ": "),
subtitle = "Bubbles are proportional to expected duration of the measure",
caption = "")
The resulting plot is this one.
Any idea, suggestion, or even warning of unfeasibility (if it really so) is more than welcome!
Upvotes: 2
Views: 594
Reputation: 124658
Try this. Basic idea is to duplicate the breaks and the symbols for the size legend. In a second step I adjust the symbols via guide_legend
. Perhaps not perfect but after trying some approaches the best I can come up with.
library(tidyverse)
library(ggtext)
library(janitor)
library(delabj)
library(wesanderson)
library(forcats)
# Breaks, labels and symbols
breaks <- c(100, 200, 300)
n_breaks <- length(breaks)
labels <- c(breaks, rep("", n_breaks))
shapes <- c(rep(16, n_breaks), rep(17, n_breaks))
breaks2 <- rep(breaks, 2)
basedata %>%
ggplot(aes(x = start_date, y = fct_rev(GTAinterventiontype), shape = type)) +
geom_point(data = prova1, aes(color = fct_rev(GTAinterventiontype), size=duration, shape = fct_rev(type)), alpha = 0.65, position = position_nudge(y = +0.05)) +
scale_size_continuous(breaks = breaks2, labels = labels,
guide = guide_legend(order = 2, nrow = 2, byrow = TRUE,
override.aes = list(shape = shapes),
direction = "horizontal", label.vjust = -.5)) +
geom_point(data = prova2, aes(color = fct_rev(GTAinterventiontype), size=duration, shape = fct_rev(type)), alpha = 0.65, position = position_nudge(y = -0.05)) +
scale_shape(drop=FALSE) +
guides(color = FALSE,
shape = guide_legend(order = 1, nrow = 2, ncol = 1)) +
delabj::theme_delabj() +
delabj::scale_color_delabj() +
#delabj::legend_none() +
labs(shape = 'Type',
size = "Duration",
x="",
y="",
title = paste("ARG", "Med-related Measures by Announcement date", sep = ": "),
subtitle = "Bubbles are proportional to expected duration of the measure",
caption = "")
Upvotes: 1