用多行突出显示ggplot中的一行
Highlight a line in ggplot with multiple lines
我想更改 ggplot 中一行的 size
、linetype
、color
等。
这是一个最小的可重现示例:
library(tidyverse)
# Data in wide format
df_wide <- data.frame(
Horizons = seq(1,10,1),
Country1 = c(2.5, 2.3, 2.2, 2.2, 2.1, 2.0, 1.7, 1.8, 1.7, 1.6),
Country2 = c(3.5, 3.3, 3.2, 3.2, 3.1, 3.0, 3.7, 3.8, 3.7, 3.6),
Country3 = c(1.5, 1.3, 1.2, 1.2, 1.1, 1.0, 0.7, 0.8, 0.7, 0.6)
)
# Convert to long format
df_long <- df_wide %>%
gather(key = "variable", value = "value", -Horizons)
# Plot the lines
plotstov <- ggplot(df_long, aes(x = Horizons, y = value)) +
geom_line(aes(colour = variable, group = variable))+
theme_bw()
输出:
如何更改 Country1
的 size
、linetype
、color
,而不必绘制每一行
分开,例如:geom_line(aes( y = Country1...)) + geom_line(aes(y = Country2...))
,因此突出显示 Country1
?
行
非常感谢!
不是每一行,但你只能单独绘制 'Country1'
:
library(ggplot2)
ggplot(subset(df_long, variable != 'Country1'), aes(x = Horizons, y = value)) +
geom_line(aes(colour = variable, group = variable)) +
geom_line(data = subset(df_long, variable == 'Country1'),
size = 3, linetype = 'dashed', color = 'blue') +
theme_bw()
实现此目的的一种方法是为 color
、size
、.. 使用命名向量,然后您可以在 scale_xxxx_manual
中使用它来设置您喜欢的值.
library(tidyverse)
# Data in wide format
df_wide <- data.frame(
Horizons = seq(1,10,1),
Country1 = c(2.5, 2.3, 2.2, 2.2, 2.1, 2.0, 1.7, 1.8, 1.7, 1.6),
Country2 = c(3.5, 3.3, 3.2, 3.2, 3.1, 3.0, 3.7, 3.8, 3.7, 3.6),
Country3 = c(1.5, 1.3, 1.2, 1.2, 1.1, 1.0, 0.7, 0.8, 0.7, 0.6)
)
# Convert to long format
df_long <- df_wide %>%
gather(key = "variable", value = "value", -Horizons)
colors <- c(Country1 = "red", Country2 = "grey50", Country3 = "grey50")
sizes <- c(Country1 = 2, Country2 = .5, Country3 = .5)
# Plot the lines
plotstov <- ggplot(df_long, aes(x = Horizons, y = value)) +
geom_line(aes(colour = variable, size = variable, group = variable)) +
scale_color_manual(values = colors) +
scale_size_manual(values = sizes) +
theme_bw()
plotstov
下面this duplicate是这个问题的另一个答案。
它使用了一个简单的技巧,通过将突出显示的变量与目标值进行比较来判断突出显示的变量。这将其二分法,成为一个合乎逻辑的 FALSE/TRUE
值。
variable == "Country1"
情节图例现在需要更加小心,以使其标题和文字正确。
g <- ggplot(df_long, aes(x = Horizons, y = value)) +
geom_line(aes(colour = variable == "Country1", size = variable == "Country1", group = variable)) +
scale_color_manual(name = "variable", labels = c("Other", "Country1"), values = c("grey50", "red")) +
scale_size_manual(name = "variable", labels = c("Other", "Country1"), values = c(0.5, 2)) +
theme_bw()
g
我想更改 ggplot 中一行的 size
、linetype
、color
等。
这是一个最小的可重现示例:
library(tidyverse)
# Data in wide format
df_wide <- data.frame(
Horizons = seq(1,10,1),
Country1 = c(2.5, 2.3, 2.2, 2.2, 2.1, 2.0, 1.7, 1.8, 1.7, 1.6),
Country2 = c(3.5, 3.3, 3.2, 3.2, 3.1, 3.0, 3.7, 3.8, 3.7, 3.6),
Country3 = c(1.5, 1.3, 1.2, 1.2, 1.1, 1.0, 0.7, 0.8, 0.7, 0.6)
)
# Convert to long format
df_long <- df_wide %>%
gather(key = "variable", value = "value", -Horizons)
# Plot the lines
plotstov <- ggplot(df_long, aes(x = Horizons, y = value)) +
geom_line(aes(colour = variable, group = variable))+
theme_bw()
输出:
如何更改 Country1
的 size
、linetype
、color
,而不必绘制每一行
分开,例如:geom_line(aes( y = Country1...)) + geom_line(aes(y = Country2...))
,因此突出显示 Country1
?
非常感谢!
不是每一行,但你只能单独绘制 'Country1'
:
library(ggplot2)
ggplot(subset(df_long, variable != 'Country1'), aes(x = Horizons, y = value)) +
geom_line(aes(colour = variable, group = variable)) +
geom_line(data = subset(df_long, variable == 'Country1'),
size = 3, linetype = 'dashed', color = 'blue') +
theme_bw()
实现此目的的一种方法是为 color
、size
、.. 使用命名向量,然后您可以在 scale_xxxx_manual
中使用它来设置您喜欢的值.
library(tidyverse)
# Data in wide format
df_wide <- data.frame(
Horizons = seq(1,10,1),
Country1 = c(2.5, 2.3, 2.2, 2.2, 2.1, 2.0, 1.7, 1.8, 1.7, 1.6),
Country2 = c(3.5, 3.3, 3.2, 3.2, 3.1, 3.0, 3.7, 3.8, 3.7, 3.6),
Country3 = c(1.5, 1.3, 1.2, 1.2, 1.1, 1.0, 0.7, 0.8, 0.7, 0.6)
)
# Convert to long format
df_long <- df_wide %>%
gather(key = "variable", value = "value", -Horizons)
colors <- c(Country1 = "red", Country2 = "grey50", Country3 = "grey50")
sizes <- c(Country1 = 2, Country2 = .5, Country3 = .5)
# Plot the lines
plotstov <- ggplot(df_long, aes(x = Horizons, y = value)) +
geom_line(aes(colour = variable, size = variable, group = variable)) +
scale_color_manual(values = colors) +
scale_size_manual(values = sizes) +
theme_bw()
plotstov
下面this duplicate是这个问题的另一个答案。
它使用了一个简单的技巧,通过将突出显示的变量与目标值进行比较来判断突出显示的变量。这将其二分法,成为一个合乎逻辑的 FALSE/TRUE
值。
variable == "Country1"
情节图例现在需要更加小心,以使其标题和文字正确。
g <- ggplot(df_long, aes(x = Horizons, y = value)) +
geom_line(aes(colour = variable == "Country1", size = variable == "Country1", group = variable)) +
scale_color_manual(name = "variable", labels = c("Other", "Country1"), values = c("grey50", "red")) +
scale_size_manual(name = "variable", labels = c("Other", "Country1"), values = c(0.5, 2)) +
theme_bw()
g