用多行高亮显示ggplot中的一行 [英] Highlight a line in ggplot with multiple lines

查看:70
本文介绍了用多行高亮显示ggplot中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ggplot中将sizelinetypecolor等更改为一行. 这是一个最小的可重现示例:

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

输出:

如何更改Country1sizelinetypecolor,而不必绘制每条线 分别,例如:geom_line(aes( y = Country1...)) + geom_line(aes(y = Country2...)),并因此突出显示Country1的行?

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?

非常感谢!

推荐答案

不是每行,但您只能分别绘制'Country1':

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

这篇关于用多行高亮显示ggplot中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆