使用ggplot2 + linerange()根据单独的列将点添加到每行 [英] add dot to each line based on a separate column using ggplot2 + linerange()

查看:47
本文介绍了使用ggplot2 + linerange()根据单独的列将点添加到每行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线形图,显示了随时间推移目击鸟类及其饮食的情况.

使用以下示例,我想在标题为 time_period 的列的基础上再添加一层,该列将数据分为3个级别: <10年 10-20年> 20年.但是,我被困住了.我想:

  1. 根据 time_period

  2. 在每行旁边添加一个彩色的点(与该行大小相同)
  3. 在下面添加一个图例,以显示每个点的含义

数据集来自

使用此代码:

 库(tidyverse)图书馆(韦桑德森)ggplot()+geom_linerange(data = bird_models_traits,aes(ymin = minyear,ymax = maxyear,颜色=饮食,x = fct_reorder(id,desc(sort))),大小= 1)+scale_colour_manual(值= wes_palette("Cavalcanti1"))+实验室(x = NULL,y = NULL)+theme_bw()+coord_flip()+指南(颜色= F)+主题(panel.grid.minor = element_blank(),panel.grid.major.y = element_blank(),panel.grid.major.x = element_line(),axis.ticks = element_blank(),legend.position =底部",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(大小= 20,垂直= 1,垂直= 0),axis.text = element_text(大小= 16),axis.title = element_text(大小= 20)) 

解决方案

让我们看看它是否对您有效(以及是否正是您所要的).

我添加了geom_point()层,该层位于与填充aes匹配的行旁边,该aes与时间段列匹配(请注意,csv中的所有数据均处于同一因子级别).将aes设置为填充并设置为21形状是为了避免产生颜色aes的混乱,因为在上一层中已经为不同的变量提供了颜色.

 库(tidyverse)图书馆(韦桑德森)df%&%;%ggplot()+geom_linerange(aes(ymin = minyear,ymax = maxyear,颜色=因素(饮食),x = fct_reorder(factor(id),desc(sort))),大小= 1)+geom_point(aes(x = fct_reorder(factor(id),desc(sort)),y = maxyear + 1,fill = time_period),show.legend = TRUE,pch = 21,颜色=白色",尺寸= 1)+scale_colour_manual(值= wes_palette("Cavalcanti1"))+实验室(x = NULL,y = NULL)+theme_bw()+coord_flip()+指南(颜色= F)+主题(panel.grid.minor = element_blank(),panel.grid.major.y = element_blank(),panel.grid.major.x = element_line(),axis.ticks = element_blank(),legend.position =底部",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(大小= 20,垂直= 1,垂直= 0),axis.text = element_text(大小= 16),axis.title = element_text(大小= 20)) 

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))

解决方案

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))

这篇关于使用ggplot2 + linerange()根据单独的列将点添加到每行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆