有没有办法在同一个 ggplot 上使用 2 个色标?
Is there any way to use 2 color scales on the same ggplot?
将数据按类别(样本 A 和 B)分开,制作了 2 层,一层用于点,一层用于线。我想按类别分隔我的数据,指示点的颜色,还分隔线,但使用与用于点的颜色不同的颜色。
library(ggplot2)
Sample <- c("a", "b")
Time <- c(0,1,2)
df <- expand.grid(Time=Time, Sample = Sample)
df$Value <- c(1,2,3,2,4,6)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = Sample)) +
geom_line(aes(color = Sample)) +
scale_color_manual(values = c("red", "blue")) + #for poits
scale_color_manual(values = c("orange", "purple")) #for lines
使用 ggnewscale
包可以这样实现:
library(ggplot2)
library(ggnewscale)
Sample <- c("a", "b")
Time <- c(0,1,2)
df <- expand.grid(Time=Time, Sample = Sample)
df$Value <- c(1,2,3,2,4,6)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = Sample)) +
scale_color_manual(name = "points", values = c("red", "blue")) + #for poits
new_scale_color() +
geom_line(aes(color = Sample)) +
scale_color_manual(name = "lines", values = c("orange", "purple")) #for lines
使用颜色列和scale_color_identity:
df$myCol1 <- rep(c("red", "blue"), each = 3)
df$myCol2 <- rep(c("orange", "purple"), each = 3)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = myCol1)) +
geom_line(aes(color = myCol2)) +
scale_color_identity()
将数据按类别(样本 A 和 B)分开,制作了 2 层,一层用于点,一层用于线。我想按类别分隔我的数据,指示点的颜色,还分隔线,但使用与用于点的颜色不同的颜色。
library(ggplot2)
Sample <- c("a", "b")
Time <- c(0,1,2)
df <- expand.grid(Time=Time, Sample = Sample)
df$Value <- c(1,2,3,2,4,6)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = Sample)) +
geom_line(aes(color = Sample)) +
scale_color_manual(values = c("red", "blue")) + #for poits
scale_color_manual(values = c("orange", "purple")) #for lines
使用 ggnewscale
包可以这样实现:
library(ggplot2)
library(ggnewscale)
Sample <- c("a", "b")
Time <- c(0,1,2)
df <- expand.grid(Time=Time, Sample = Sample)
df$Value <- c(1,2,3,2,4,6)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = Sample)) +
scale_color_manual(name = "points", values = c("red", "blue")) + #for poits
new_scale_color() +
geom_line(aes(color = Sample)) +
scale_color_manual(name = "lines", values = c("orange", "purple")) #for lines
使用颜色列和scale_color_identity:
df$myCol1 <- rep(c("red", "blue"), each = 3)
df$myCol2 <- rep(c("orange", "purple"), each = 3)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = myCol1)) +
geom_line(aes(color = myCol2)) +
scale_color_identity()