Highlight a line in ggplot with multiple lines

笑着哭i 提交于 2021-01-29 10:55:47

问题


I want to change the size, linetype, color etc. for one line in ggplot. Here is a minimal reproducible example:

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() 

Output:

How can I change the size, linetype, color of Country1, without having to plot every line separately, like: geom_line(aes( y = Country1...)) + geom_line(aes(y = Country2...)), and therefore highlight the line of Country1?

Thanks a lot in advance!


回答1:


Not every line but you can plot only 'Country1' separately :

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() 




回答2:


One approach to achieve this is to make use of named vectors for color, size, .. which you can then use inside scale_xxxx_manual to set the values as you like.

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




回答3:


Following this duplicate here is another answer to this question.
It uses a simple trick, to tell the highlighted variable by comparing it with the target value. This dichotomyzes it, becoming a logical FALSE/TRUE value.

variable == "Country1"

The plot legend now needs more care to be taken, in order to have its title and text right.

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



来源:https://stackoverflow.com/questions/64532659/ggplot-color-question-regarding-one-country-be-red-or-other-be-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!