I have a linerange graph that shows sightings of birds and their diet over time.
Using the below example I would like to add a further layer, based on the column entitled time_period
, which has grouped the data into 3 levels: <10 years
, 10-20 years
, >20 years
. However, I've come stuck. I would like to:
Add a coloured dot (ideally same size as the line) next to each line based on the
time_period
columnAdd a legend underneath that shows what each dot means
The dataset is taken from this csv file here, and looks like this (thank you @Stephen Henderson, who rightly pointed out I had previously attached the wrong dataset - this is the same used for the linegraph):
# A tibble: 200 x 18
decimal.latitude decimal.longitu~ class species.name id duration minyear maxyear
<dbl> <dbl> <chr> <chr> <fct> <dbl> <dbl> <dbl>
1 -54.6 159. Aves Aptenodytes~ 2283 10 1970 1980
2 -43.0 147. Aves Larus domin~ 8990 28 1980 2008
3 -43.0 147. Aves Larus novae~ 8992 25 1983 2008
4 -43.0 147. Aves Larus pacif~ 8991 28 1980 2008
5 -42.9 147. Aves Calidris fe~ 8940 33 1974 2007
6 -42.9 147. Aves Calidris ru~ 8942 34 1974 2008
7 -42.9 147. Aves Limosa lapp~ 8939 34 1974 2008
8 -42.9 147. Aves Numenius ma~ 8941 34 1974 2008
9 -42.9 147. Aves Tringa nebu~ 8938 34 1974 2008
10 -42.0 148. Aves Numenius ma~ 12022 12 1988 2000
# ... with 190 more rows, and 10 more variables: system <chr>, common.name <chr>,
# estimate <dbl>, std.error <dbl>, statistic <dbl>, p.value <dbl>, diet <fct>,
# mean_trend <dbl>, sort <dbl>, time_period <fct>
I have created the following graph:
Using this code:
library(tidyverse)
library(wesanderson)
ggplot() +
geom_linerange(data = bird_models_traits, aes(ymin = minyear, ymax = maxyear,
colour = diet,
x = fct_reorder(id, desc(sort))),
size = 1) +
scale_colour_manual(values = wes_palette("Cavalcanti1")) +
labs(x = NULL, y = NULL) +
theme_bw() +
coord_flip() +
guides(colour = F) +
theme(panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(),
axis.ticks = element_blank(),
legend.position = "bottom",
panel.border = element_blank(),
legend.title = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.title = element_text(size = 20, vjust = 1, hjust = 0),
axis.text = element_text(size = 16),
axis.title = element_text(size = 20))