同时使用ggplot进行分组和着色
Grouping and colouring with ggplot at the same time
我有两个团队,每个团队和年份都有一个特定时间间隔的给定值。我的数据如下所示:
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()
导致以下情节:
目前只有颜色缺失。我尝试了几种方法,但 none 成功了:
ggplot(hr_by_team_year_sf_la_df, aes(x='yearID', y='HR', group='yearID')) + geom_line() + geom_point(aes(color='teamID'))
ggplot(hr_by_team_year_sf_la_df, aes(x='yearID', y='HR', group='yearID', color='teamID')) + geom_line() + geom_point()
有人知道如何根据 teamId 绘制点吗?
你的意思是这样的吗?
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)
我有两个团队,每个团队和年份都有一个特定时间间隔的给定值。我的数据如下所示:
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()
导致以下情节:
目前只有颜色缺失。我尝试了几种方法,但 none 成功了:
ggplot(hr_by_team_year_sf_la_df, aes(x='yearID', y='HR', group='yearID')) + geom_line() + geom_point(aes(color='teamID'))
ggplot(hr_by_team_year_sf_la_df, aes(x='yearID', y='HR', group='yearID', color='teamID')) + geom_line() + geom_point()
有人知道如何根据 teamId 绘制点吗?
你的意思是这样的吗?
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)