如何在 R 中对绘图颜色进行分组
How to group plot colors in R
我在 R 中使用 ggplot
绘制了一个图,如下所示。如何对绘图颜色进行分组,例如所有 Control 具有相同颜色,而 Treated 具有另一种颜色?所以基本上总共只有两种不同的颜色。可能我需要使用群体美学来做到这一点。
Here是数据文件。这是代码:
{r}
growth_data_long <- growth_data %>% pivot_longer(-`animal`,
names_to=("Day"),
values_to=("Growth"))
growth_data_long
{r}
growth_data_long %>% ggplot(aes(Day,Growth, group= animal)) + aes(color=animal) +geom_line()+
geom_point()
编辑
这是一个不使用 tidyverse 包的解决方案:
library(ggplot2)
library(tidyr)
growth_data <- read.csv("~/Downloads/growth_data.txt", sep = "\t")
growth_data_long <- growth_data %>% pivot_longer(-`animal`,
names_to=("Day"),
values_to=("Growth"))
growth_data_long
#> # A tibble: 60 × 3
#> animal Day Growth
#> <chr> <chr> <dbl>
#> 1 Control 1 Day.1 1.08
#> 2 Control 1 Day.2 1.49
#> 3 Control 1 Day.3 2.73
#> 4 Control 1 Day.4 2.81
#> 5 Control 1 Day.5 3.8
#> 6 Control 1 Day.6 4.8
#> 7 Control 2 Day.1 1.22
#> 8 Control 2 Day.2 1.86
#> 9 Control 2 Day.3 2.01
#> 10 Control 2 Day.4 2.53
#> # … with 50 more rows
growth_data_long$group <- gsub(x = growth_data_long$animal, pattern = " \d+", replacement = "")
ggplot(growth_data_long, aes(x = Day, y = Growth, group = animal, color = group)) +
geom_line() +
geom_point()
由 reprex package (v2.0.1)
于 2021-10-06 创建
您可以分开您的动物栏,以便获得控制或治疗标签。
library(tidyr)
growth_data_long <- growth_data_long %>% separate(animal, into = c("group", NA), sep = " ", remove = F)
然后以与将 color=animal
更改为 color=group
之前相同的方式生成图表。
growth_data_long %>% ggplot(aes(Day,Growth, group= animal)) + aes(color=group) +geom_line()+
geom_point()
我在 R 中使用 ggplot
绘制了一个图,如下所示。如何对绘图颜色进行分组,例如所有 Control 具有相同颜色,而 Treated 具有另一种颜色?所以基本上总共只有两种不同的颜色。可能我需要使用群体美学来做到这一点。
Here是数据文件。这是代码:
{r}
growth_data_long <- growth_data %>% pivot_longer(-`animal`,
names_to=("Day"),
values_to=("Growth"))
growth_data_long
{r}
growth_data_long %>% ggplot(aes(Day,Growth, group= animal)) + aes(color=animal) +geom_line()+
geom_point()
编辑
这是一个不使用 tidyverse 包的解决方案:
library(ggplot2)
library(tidyr)
growth_data <- read.csv("~/Downloads/growth_data.txt", sep = "\t")
growth_data_long <- growth_data %>% pivot_longer(-`animal`,
names_to=("Day"),
values_to=("Growth"))
growth_data_long
#> # A tibble: 60 × 3
#> animal Day Growth
#> <chr> <chr> <dbl>
#> 1 Control 1 Day.1 1.08
#> 2 Control 1 Day.2 1.49
#> 3 Control 1 Day.3 2.73
#> 4 Control 1 Day.4 2.81
#> 5 Control 1 Day.5 3.8
#> 6 Control 1 Day.6 4.8
#> 7 Control 2 Day.1 1.22
#> 8 Control 2 Day.2 1.86
#> 9 Control 2 Day.3 2.01
#> 10 Control 2 Day.4 2.53
#> # … with 50 more rows
growth_data_long$group <- gsub(x = growth_data_long$animal, pattern = " \d+", replacement = "")
ggplot(growth_data_long, aes(x = Day, y = Growth, group = animal, color = group)) +
geom_line() +
geom_point()
由 reprex package (v2.0.1)
于 2021-10-06 创建您可以分开您的动物栏,以便获得控制或治疗标签。
library(tidyr)
growth_data_long <- growth_data_long %>% separate(animal, into = c("group", NA), sep = " ", remove = F)
然后以与将 color=animal
更改为 color=group
之前相同的方式生成图表。
growth_data_long %>% ggplot(aes(Day,Growth, group= animal)) + aes(color=group) +geom_line()+
geom_point()