具有离散和连续变量交互作用的 ggplot2 图例
Legend for ggplot2 with interaction of discrete and continous variables
我在为以下数据集绘制好看的图例时遇到了一个普遍问题。
df1 <- data.frame("ShearRate" = rep(c(1,5,10), rep(5,3)), "Time" = seq(1,15,1),
"Viscosity" = c(runif(5,15,20), runif(5,10,15), runif(5,5,10)),
"Test" = "Test1", "Interval" = rep(c("Interval1", "Interval2", "Interval3"),rep(5,3)))
df2 <- data.frame("ShearRate" = rep(c(1,5,10), rep(5,3)), "Time" = seq(1,15,1),
"Viscosity" = c(runif(5,15,20), runif(5,10,15), runif(5,5,10)),
"Test" = "Test2", "Interval" = rep(c("Interval1", "Interval2", "Interval3"),rep(5,3)))
df <- full_join(df1,df2)
rm(df1,df2)
df$Test <- as.factor(df$Test)
df$Interval <- as.factor(df$Interval)
现在我可以制作以下情节:
df %>%
ggplot(aes(Time, Viscosity, col = interaction(Test,factor(ShearRate), sep="/Shear Rate [mPas]: ")))+
theme_bw() +
geom_smooth(show.legend = T) +
scale_colour_manual(values = c("red3", "blue3", "red2", "blue2", "red", "blue")) +
geom_point(aes(shape = Interval)) +
labs(x = "Time [s]", y = "Viscosity [mPas]", col = "Test ID")
我觉得不错:
我需要使用变量 Test
与变量 ShearRate
的相互作用来实现此图。但我真的不满足于它所产生的传奇。其实我很想有一个测试的传奇。像红色的东西:Test1 和蓝色的东西:Test2。还有一个带有色调的图例,它给了我们连续 ShearRate
的强度
我希望我的问题很清楚,希望我的 MWE 能帮助您理解我的问题。
此致!
如果您想根据 Test
指定颜色,我不太确定您对 ShearRate
的值使用不同的色调是什么意思。我 认为 你的意思是当 Test == "Test2"
时 ShearRate
有蓝色渐变,当 Test == "Test1"
时有红色渐变。这在 ggplot 中是不可能的,但你可以使用 ggnewscale
:
library(ggnewscale)
ggplot(mapping = aes(Time, Viscosity)) +
geom_line(data = df[df$Test == "Test1",],
aes(color = ShearRate, group = factor(ShearRate)),
stat = "smooth",
show.legend = TRUE, size = 1) +
geom_point(data = df[df$Test == "Test1",],
aes(shape = Interval, color = ShearRate)) +
scale_color_gradient(low = "red4", high = "tomato",
name = "Test1:\nShear rate") +
ggnewscale::new_scale_color() +
geom_line(data = df[df$Test == "Test2",],
aes(color = ShearRate, group = factor(ShearRate)),
stat = "smooth",
show.legend = TRUE, size = 1) +
geom_point(data = df[df$Test == "Test2",],
aes(shape = Interval, color = ShearRate)) +
scale_color_gradient(low = "blue4", high = "deepskyblue",
name = "Test2:\nShear rate") +
labs(x = "Time [s]", y = "Viscosity [mPas]") +
guides(alpha = guide_none(), shape = guide_none()) +
theme_bw() +
theme(legend.direction = "horizontal")
我在为以下数据集绘制好看的图例时遇到了一个普遍问题。
df1 <- data.frame("ShearRate" = rep(c(1,5,10), rep(5,3)), "Time" = seq(1,15,1),
"Viscosity" = c(runif(5,15,20), runif(5,10,15), runif(5,5,10)),
"Test" = "Test1", "Interval" = rep(c("Interval1", "Interval2", "Interval3"),rep(5,3)))
df2 <- data.frame("ShearRate" = rep(c(1,5,10), rep(5,3)), "Time" = seq(1,15,1),
"Viscosity" = c(runif(5,15,20), runif(5,10,15), runif(5,5,10)),
"Test" = "Test2", "Interval" = rep(c("Interval1", "Interval2", "Interval3"),rep(5,3)))
df <- full_join(df1,df2)
rm(df1,df2)
df$Test <- as.factor(df$Test)
df$Interval <- as.factor(df$Interval)
现在我可以制作以下情节:
df %>%
ggplot(aes(Time, Viscosity, col = interaction(Test,factor(ShearRate), sep="/Shear Rate [mPas]: ")))+
theme_bw() +
geom_smooth(show.legend = T) +
scale_colour_manual(values = c("red3", "blue3", "red2", "blue2", "red", "blue")) +
geom_point(aes(shape = Interval)) +
labs(x = "Time [s]", y = "Viscosity [mPas]", col = "Test ID")
我觉得不错:
我需要使用变量 Test
与变量 ShearRate
的相互作用来实现此图。但我真的不满足于它所产生的传奇。其实我很想有一个测试的传奇。像红色的东西:Test1 和蓝色的东西:Test2。还有一个带有色调的图例,它给了我们连续 ShearRate
我希望我的问题很清楚,希望我的 MWE 能帮助您理解我的问题。
此致!
如果您想根据 Test
指定颜色,我不太确定您对 ShearRate
的值使用不同的色调是什么意思。我 认为 你的意思是当 Test == "Test2"
时 ShearRate
有蓝色渐变,当 Test == "Test1"
时有红色渐变。这在 ggplot 中是不可能的,但你可以使用 ggnewscale
:
library(ggnewscale)
ggplot(mapping = aes(Time, Viscosity)) +
geom_line(data = df[df$Test == "Test1",],
aes(color = ShearRate, group = factor(ShearRate)),
stat = "smooth",
show.legend = TRUE, size = 1) +
geom_point(data = df[df$Test == "Test1",],
aes(shape = Interval, color = ShearRate)) +
scale_color_gradient(low = "red4", high = "tomato",
name = "Test1:\nShear rate") +
ggnewscale::new_scale_color() +
geom_line(data = df[df$Test == "Test2",],
aes(color = ShearRate, group = factor(ShearRate)),
stat = "smooth",
show.legend = TRUE, size = 1) +
geom_point(data = df[df$Test == "Test2",],
aes(shape = Interval, color = ShearRate)) +
scale_color_gradient(low = "blue4", high = "deepskyblue",
name = "Test2:\nShear rate") +
labs(x = "Time [s]", y = "Viscosity [mPas]") +
guides(alpha = guide_none(), shape = guide_none()) +
theme_bw() +
theme(legend.direction = "horizontal")