Reputation: 343
Below is the code and reproducible data (a subset of my actual data) that I'm using to plot disease rank (the impact of a disease) against country rank (how well Ireland is doing relative to other countries)
The data in this example is filtered to include only 2015. I would however like to remove that filter and include the other years (2014-2018) on the same plot (but with the alpha and size of the point decreasing with each preceding year). So the 2014 point will be small and light, for 2015 a little bigger and darker, and so on.
I'm guessing that I need to have repeating layers of "geom_point()+" and that I need to somehow include a "year" filter in each of those layers. I can't seem to get it right. I'm sure its easy. Any advice would be greatly appreciated.
(I have found a similar question on here but not been able to apply it to my problem: Filter in ggplot2's geoms using common aesthetics and data frames across geoms)
Here is the data (code is below)
reprex <- structure(list(Country = c("Australia", "Australia", "Australia",
"Australia", "Australia", "Australia", "Australia", "Australia",
"Australia", "Australia", "Australia", "Australia", "Austria",
"Austria", "Austria", "Austria", "Austria", "Austria", "Austria",
"Austria", "Austria", "Austria", "Austria", "Austria", "Austria",
"Austria", "Austria", "Austria", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "Canada", "Canada", "Canada",
"Canada", "Canada", "Canada", "Canada", "Canada", "Czech Republic",
"Czech Republic", "Czech Republic", "Czech Republic", "Czech Republic",
"Czech Republic", "Czech Republic", "Czech Republic", "Czech Republic",
"Czech Republic", "Czech Republic", "Czech Republic", "Czech Republic",
"Czech Republic", "Czech Republic", "Czech Republic", "Denmark",
"Denmark", "Denmark", "Denmark", "Denmark", "Denmark", "Denmark",
"Denmark", "Finland", "Finland", "Finland", "Finland", "Finland",
"Finland", "Finland", "Finland", "Finland", "Finland", "Finland",
"Finland", "Ireland", "Ireland", "Ireland", "Ireland", "Ireland",
"Ireland", "Ireland", "Ireland", "Israel", "Israel", "Israel",
"Israel", "Israel", "Israel", "Israel", "Israel", "Israel", "Israel",
"Israel", "Israel", "Russia", "Russia", "Russia", "Russia", "Slovenia",
"Slovenia", "Slovenia", "Slovenia", "Slovenia", "Slovenia", "Slovenia",
"Slovenia", "South Africa", "South Africa", "South Africa", "South Africa",
"South Africa", "South Africa", "South Africa", "South Africa",
"Colombia", "Colombia", "Latvia", "Latvia", "Latvia", "Latvia",
"Latvia", "Latvia", "Costa Rica", "Colombia", "Colombia", "Costa Rica",
"Latvia", "Latvia", "Costa Rica", "Colombia", "Colombia", "Costa Rica",
"Colombia", "Colombia", "Lithuania", "Lithuania", "Lithuania",
"Lithuania", "Lithuania", "Lithuania", "Lithuania", "Lithuania",
"Lithuania", "Lithuania", "Lithuania", "Lithuania", "Lithuania",
"Lithuania", "Lithuania", "Lithuania"), Year = c(2014, 2015,
2016, 2014, 2015, 2016, 2014, 2015, 2016, 2014, 2015, 2016, 2014,
2015, 2016, 2017, 2014, 2015, 2016, 2017, 2014, 2015, 2016, 2017,
2014, 2015, 2016, 2017, 2014, 2015, 2016, 2014, 2015, 2016, 2014,
2015, 2016, 2014, 2015, 2016, 2014, 2015, 2014, 2015, 2014, 2015,
2014, 2015, 2014, 2015, 2016, 2017, 2014, 2015, 2016, 2017, 2014,
2015, 2016, 2017, 2014, 2015, 2016, 2017, 2014, 2015, 2014, 2015,
2014, 2015, 2014, 2015, 2014, 2015, 2016, 2014, 2015, 2016, 2014,
2015, 2016, 2014, 2015, 2016, 2014, 2015, 2014, 2015, 2014, 2015,
2014, 2015, 2014, 2015, 2016, 2014, 2015, 2016, 2014, 2015, 2016,
2014, 2015, 2016, 2014, 2014, 2014, 2014, 2014, 2015, 2014, 2015,
2014, 2015, 2014, 2015, 2014, 2015, 2014, 2015, 2014, 2015, 2014,
2015, 2014, 2015, 2014, 2015, 2014, 2015, 2014, 2015, 2014, 2014,
2015, 2014, 2014, 2015, 2014, 2014, 2015, 2014, 2014, 2015, 2014,
2015, 2016, 2017, 2014, 2015, 2016, 2017, 2014, 2015, 2016, 2017,
2014, 2015, 2016, 2017), Disease = c("Diabetes mellitus", "Diabetes mellitus",
"Diabetes mellitus", "Ischaemic heart diseases", "Ischaemic heart diseases",
"Ischaemic heart diseases", "Influenza", "Influenza", "Influenza",
"Accidental falls", "Accidental falls", "Accidental falls", "Diabetes mellitus",
"Diabetes mellitus", "Diabetes mellitus", "Diabetes mellitus",
"Ischaemic heart diseases", "Ischaemic heart diseases", "Ischaemic heart diseases",
"Ischaemic heart diseases", "Influenza", "Influenza", "Influenza",
"Influenza", "Accidental falls", "Accidental falls", "Accidental falls",
"Accidental falls", "Diabetes mellitus", "Diabetes mellitus",
"Diabetes mellitus", "Ischaemic heart diseases", "Ischaemic heart diseases",
"Ischaemic heart diseases", "Influenza", "Influenza", "Influenza",
"Accidental falls", "Accidental falls", "Accidental falls", "Diabetes mellitus",
"Diabetes mellitus", "Ischaemic heart diseases", "Ischaemic heart diseases",
"Influenza", "Influenza", "Accidental falls", "Accidental falls",
"Diabetes mellitus", "Diabetes mellitus", "Diabetes mellitus",
"Diabetes mellitus", "Ischaemic heart diseases", "Ischaemic heart diseases",
"Ischaemic heart diseases", "Ischaemic heart diseases", "Influenza",
"Influenza", "Influenza", "Influenza", "Accidental falls", "Accidental falls",
"Accidental falls", "Accidental falls", "Diabetes mellitus",
"Diabetes mellitus", "Ischaemic heart diseases", "Ischaemic heart diseases",
"Influenza", "Influenza", "Accidental falls", "Accidental falls",
"Diabetes mellitus", "Diabetes mellitus", "Diabetes mellitus",
"Ischaemic heart diseases", "Ischaemic heart diseases", "Ischaemic heart diseases",
"Influenza", "Influenza", "Influenza", "Accidental falls", "Accidental falls",
"Accidental falls", "Diabetes mellitus", "Diabetes mellitus",
"Ischaemic heart diseases", "Ischaemic heart diseases", "Influenza",
"Influenza", "Accidental falls", "Accidental falls", "Diabetes mellitus",
"Diabetes mellitus", "Diabetes mellitus", "Ischaemic heart diseases",
"Ischaemic heart diseases", "Ischaemic heart diseases", "Influenza",
"Influenza", "Influenza", "Accidental falls", "Accidental falls",
"Accidental falls", "Diabetes mellitus", "Ischaemic heart diseases",
"Influenza", "Accidental falls", "Diabetes mellitus", "Diabetes mellitus",
"Ischaemic heart diseases", "Ischaemic heart diseases", "Influenza",
"Influenza", "Accidental falls", "Accidental falls", "Diabetes mellitus",
"Diabetes mellitus", "Ischaemic heart diseases", "Ischaemic heart diseases",
"Influenza", "Influenza", "Accidental falls", "Accidental falls",
"Ischaemic heart diseases", "Ischaemic heart diseases", "Accidental falls",
"Accidental falls", "Influenza", "Influenza", "Diabetes mellitus",
"Diabetes mellitus", "Diabetes mellitus", "Diabetes mellitus",
"Diabetes mellitus", "Accidental falls", "Ischaemic heart diseases",
"Ischaemic heart diseases", "Influenza", "Accidental falls",
"Accidental falls", "Ischaemic heart diseases", "Influenza",
"Influenza", "Diabetes mellitus", "Diabetes mellitus", "Diabetes mellitus",
"Diabetes mellitus", "Accidental falls", "Accidental falls",
"Accidental falls", "Accidental falls", "Influenza", "Influenza",
"Influenza", "Influenza", "Ischaemic heart diseases", "Ischaemic heart diseases",
"Ischaemic heart diseases", "Ischaemic heart diseases"), Value = c(18.7,
19.6, 19.5, 85.6, 82.1, 76.6, 1, 1.2, 1.8, 10.1, 10.6, 10.7,
28.9, 31.4, 29.5, 27.8, 131.1, 134.2, 124.4, 121.9, 0.1, 0.6,
0.4, 1, 8.4, 8.2, 7.7, 7.6, 10.8, 10.4, 9.8, 54.3, 54.4, 50.1,
0.3, 2, 0.8, 10.6, 11.2, 10.3, 19.5, 18.3, 94.6, 86.1, 2.5, 4.1,
13.9, 11.9, 31.2, 32.5, 32.1, 31.2, 237.1, 237.5, 206.6, 207.3,
0.4, 1.1, 0.9, 1.3, 5.1, 5.3, 4.9, 5.1, 20, 20.5, 59.5, 57.6,
0.2, 0.3, 7.8, 7.2, 7.3, 7.1, 7.7, 146.9, 141.8, 137.6, 0.4,
1.6, 2.4, 16.4, 16.1, 16.4, 12.9, 13.6, 113, 116, 0.8, 0.8, 7,
6.2, 39.5, 40.6, 36, 63.8, 62.7, 59.3, 0.2, 0.5, 1, 2, 2.2, 2,
14.8, 381.2, 0.1, 6.2, 11.6, 12.9, 75.6, 82.3, 0.4, 0.4, 18.7,
19.6, 120, 131.7, 66.3, 73.8, 2.5, 2.3, 0.9, 1.2, 172.8, 176.1,
7.6, 8.8, 0.1, 0.8, 20.5, 18.8, 25.6, 32.8, 33.9, 2.1, 327.5,
320.9, 0.2, 5, 5, 112, 0.1, 0.1, 7.8, 8.9, 8.4, 11.5, 10.4, 11.4,
11.7, 11.9, 0.1, 0.1, 0.9, 0.4, 406.9, 423.5, 406.9, 382.5)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -160L), spec = structure(list(
cols = list(VAR = structure(list(), class = c("collector_character",
"collector")), Variable = structure(list(), class = c("collector_character",
"collector")), UNIT = structure(list(), class = c("collector_character",
"collector")), Measure = structure(list(), class = c("collector_character",
"collector")), COU = structure(list(), class = c("collector_character",
"collector")), Country = structure(list(), class = c("collector_character",
"collector")), YEA = structure(list(), class = c("collector_double",
"collector")), Year = structure(list(), class = c("collector_double",
"collector")), Value = structure(list(), class = c("collector_double",
"collector")), `Flag Codes` = structure(list(), class = c("collector_character",
"collector")), Flags = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
Here is the code:
library(tidyverse)
reprex %>%
filter(Year == "2015") %>%
group_by(Disease) %>%
mutate(`Country rank` = percent_rank(Value)) %>%
ungroup() %>%
group_by(Country) %>%
mutate(`Disease rank` = percent_rank(Value)) %>%
filter(Country == "Ireland") %>%
ggplot(aes(`Disease rank`, `Country rank`))+
geom_point(size = 3)
Upvotes: 0
Views: 182
Reputation: 35377
You don't want to add more geom_point
layers, but rather you want to map Year
to the aesthestics. E.g.:
reprex_summ <- reprex %>%
group_by(Disease) %>%
mutate(`Country rank` = percent_rank(Value)) %>%
ungroup() %>% # This line is actually not necessary, group_by overrides previous
group_by(Country) %>%
mutate(`Disease rank` = percent_rank(Value)) %>%
filter(Country == 'Ireland')
ggplot(reprex_summ, aes(`Disease rank`, `Country rank`, alpha = Year, size = Year)) +
geom_point() +
scale_size_continuous(range = c(4, 5), breaks = 2) +
scale_alpha_continuous(range = c(0.5, 1), breaks = 2)
Note: You might have to also add Year
to your group_by()
, I'm not sure what the reasoning behind your ranking is.
Or showing all the countries:
reprex_summ2 <- reprex %>%
group_by(Disease) %>%
mutate(`Country rank` = percent_rank(Value)) %>%
ungroup() %>%
group_by(Country) %>%
mutate(`Disease rank` = percent_rank(Value))
ggplot(reprex_summ2, aes(`Disease rank`, `Country rank`, alpha = Year, size = Year)) +
geom_point() +
scale_size_continuous(range = c(1, 3), breaks = unique(reprex$Year)) +
scale_alpha_continuous(range = c(0.5, 1), breaks = unique(reprex$Year)) +
facet_wrap(~Country)
Upvotes: 3
Reputation: 11
I left in some options to filter based on Country, but otherwise I think that this will address your question.
library(tidyverse)
d <- reprex %>%
# No longer filtering on year
# filter(Year == "2015") %>%
group_by(Disease) %>%
mutate(`Country rank` = percent_rank(Value)) %>%
ungroup() %>%
group_by(Country) %>%
mutate(`Disease rank` = percent_rank(Value))
# To do it only for Ireland
#d <- d %>% filter(Country == "Ireland")
time_var <- d %>% ungroup() %>% distinct(Year) %>% unlist() %>% unname()
for (.y in seq_along(time_var)) {
.x <- time_var[.y]
if (.y == 1){
p <- ggplot(d %>% filter(Year == .x),
aes(x = `Disease rank`, y = `Country rank`)) +
geom_point(size = .y, alpha = .y/length(time_var))
} else {
p <- p + geom_point(data = d %>%
filter(Year == .x),
aes(x = `Disease rank`, y = `Country rank`),
size = .y, alpha = .y/length(time_var))
}
}
p
Upvotes: 0