【问题标题】:Grouping and colouring with ggplot at the same time同时使用ggplot进行分组和着色
【发布时间】:2021-02-16 12:08:25
【问题描述】:

我有两个团队,每个团队和年份都有一个特定时间间隔的给定值。我的数据如下所示:

yearID teamID value
2020 0 5
2020 1 7
2019 0 3
2019 1 1

我想用每一年的分数和价值来绘制这个图,用颜色区分团队之间的区别。 此外,我想在按年份分组的点之间画线。我快到了:

ggplot(hr_by_team_year_sf_la_df, aes(x='yearID', y='HR', group='yearID')) + geom_line() + geom_point()

导致以下情节:

目前只有颜色缺失。我尝试了几种方法,但都没有成功:

  1. ggplot(hr_by_team_year_sf_la_df, aes(x='yearID', y='HR', group='yearID')) + geom_line() + geom_point(aes(color='teamID'))
  2. ggplot(hr_by_team_year_sf_la_df, aes(x='yearID', y='HR', group='yearID', color='teamID')) + geom_line() + geom_point()

有人知道如何根据 teamId 绘制点吗?

【问题讨论】:

    标签: r ggplot2 geom-point


    【解决方案1】:

    你的意思是这样的吗?

    library(tidyverse)
    
    df <- tibble(yearID = c("2020","2020","2019","2019"), 
                 teamID = c(0, 1, 0, 1), 
                 value = c(5, 7, 3, 1)) %>% 
      mutate(teamID = as.factor(teamID))
    
    ggplot(df) + 
      geom_line(aes(yearID, value)) + 
      geom_point(aes(yearID, value, color = teamID), size = 3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 2021-07-15
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 2022-01-12
      相关资源
      最近更新 更多