add dot to each line based on a separate column us

2019-08-21 23:01发布

问题:

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:

  1. Add a coloured dot (ideally same size as the line) next to each line based on the time_period column

  2. Add 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))

回答1:

Let's see if this works for you (and if this is what you're looking for).

I've added a geom_point() layer located next to the lines with a fill aes that is matched to the time period column (note that all data in the csv are in the same factor level though). Setting the aes to fill and then giving it shape 21 is to avoid creating a mess with the color aes as it's already present for a different variable in the previous layer.

library(tidyverse)
library(wesanderson) 

df%>%
  ggplot() +
  geom_linerange( aes(ymin = minyear, ymax = maxyear, 
                                                colour = factor(diet),
                                               x = fct_reorder(factor(id), desc(sort))),
                 size = 1) +
  geom_point(aes(x = fct_reorder(factor(id), desc(sort)), y = maxyear + 1, fill = time_period), 
show.legend = TRUE, pch = 21, color = "white", 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))



标签: r ggplot2