首页 > 解决方案 > 更改 facet_grid 内单个图的线条大小

问题描述

我想在单个 内更改特定行的大小facet_grid,并保持其他行不变。这为了“突出”更多的一条线。

假数据:

set.seed(123)
my_data <- data.frame(
  time = 1:100,
  a = runif(100, min = 0, max = 10),
  b = runif(100, min = 0, max = 20),
  c = runif(100, min = 0, max = 30)
)


library(ggplot2)
library(dplyr)
my_data %>% 
  gather("key", "value", -time) %>%
  ggplot(aes(x = time, y = value, color = key)) +
  geom_line() +
  facet_grid(key~., scales = "free") +
  theme_minimal() +
  guides(color = FALSE, size = FALSE)

在此处输入图像描述

在这个例子中,我希望b情节有一个更大的 line size

标签: rggplot2

解决方案


这可以通过创建一个具有重复大小的新向量来实现:

linesize = rep(c(0, 1, 0), each=100) # externally define the sizes
# note that a,c will have size=0 while b size=1

这将在geom_line调用中使用:

my_data %>% 
  gather("key", "value", -time) %>%
  ggplot(aes(x = time, y = value, color = key)) +
  geom_line(size = linesize) + # here we can pass linesize
  facet_grid(key~., scales = "free") +
  theme_minimal() +
  guides(color = FALSE)

在此处输入图像描述


推荐阅读