0

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

年号 团队ID 价值
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绘制点?

4

1 回答 1

0

你的意思是这样的吗?

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)

在此处输入图像描述

于 2021-02-16T12:46:19.683 回答