使用 scale_color_manual 根据一组特定变量手动 select 绘制线条大小

Manually select plot line size from a specific set of variables using scale_color_manual

我正在使用 geom_area 在堆叠的样本中绘制一组变量,我想使特定 "Case" = N 的线条更大并具有特定颜色(红色) ).我已经定义了颜色,但是如何使用正确的代码添加命令 scale_color_manual? 我正在使用更大的数据集 Variable=92 所以我想避免单独绘制变量...

data <- tibble::tibble(
  value = c(0.0693, 0.0677, 0.0727, 0.0650, 0.0908, 0.00112,    0.131,  0.0975, 0.109,  0.105,  0.0927, 0.0552, 0.0532, 0.0559, 0.0771, 0.0563, 0.0551,
0.191,  0.193,  0.147,  0.157,  0.258,  0.00738,    0.00808,    0.00661,    0.00495210983601696,    0.451,  0.379,  0.0653, 0.0350, 0.0559, 0.192,  0.0738, 0.107,
0.0138, 0.0104, 0.0145, 0.0103, 5.08255237961193E-05,   0.0361, 0.0264, 0.0454, 0.0277, 0.0117, 8.92140244446427E-05,   0.0173727368061961, 0.0108, 8.54627809588924E-05,   2.35593459925552E-05,   3.13020069803476E-05,   1.12019464502152E-05, 
0.0453, 0.0577, 0.0627, 0.0450, 0.0508, 0.00212,    0.031,  0.0875, 0.100,  0.115,  0.0827, 0.0452, 0.0332, 0.0459, 0.0671, 0.0263, 0.0451), 
Sample = rep(c(1:17),4),
Variable = rep(c(rep("A1",17),rep("A2",17),rep("A3",17),rep("A4",17))),
Case = rep(c(rep("P",17),rep("N",34),rep("L",17))))

ggplot(data, aes(x=Sample, y=value, color=Case, group = interaction(Variable,Case))) + geom_line(position = "stack") + scale_color_manual(values = c("P" = "green4", "N" = "red", "L"="black"))

我也尝试过这段代码,但是在顶部再次绘制了两个 N 值,并与已经存在的值重叠:

p + geom_line(data=subset(data, Case == "N"), colour="red", size=1.5) 

可能是类似这样的东西?

scale_size_manual(values=c(size = ("L"=0.5), ("N"=1), ("P"=0.5)))

解决方法:您可以使用 aes(size = ) 将 var 映射到 size,然后可以使用 scale_size_manual()

手动重置大小
library(ggplot2)
data <- tibble::tibble(
  value = c(0.0693, 0.0677, 0.0727, 0.0650, 0.0908, 0.00112,    0.131,  0.0975, 0.109,  0.105,  0.0927, 0.0552, 0.0532, 0.0559, 0.0771, 0.0563, 0.0551,
    0.191,  0.193,  0.147,  0.157,  0.258,  0.00738,    0.00808,    0.00661,    0.00495210983601696,    0.451,  0.379,  0.0653, 0.0350, 0.0559, 0.192,  0.0738, 0.107,
    0.0138, 0.0104, 0.0145, 0.0103, 5.08255237961193E-05,   0.0361, 0.0264, 0.0454, 0.0277, 0.0117, 8.92140244446427E-05,   0.0173727368061961, 0.0108, 8.54627809588924E-05,   2.35593459925552E-05,   3.13020069803476E-05,   1.12019464502152E-05, 
    0.0453, 0.0577, 0.0627, 0.0450, 0.0508, 0.00212,    0.031,  0.0875, 0.100,  0.115,  0.0827, 0.0452, 0.0332, 0.0459, 0.0671, 0.0263, 0.0451), 
  Sample = rep(c(1:17),4),
  Variable = rep(c(rep("A1",17),rep("A2",17),rep("A3",17),rep("A4",17))),
  Case = rep(c(rep("P",17),rep("N",34),rep("L",17))))

data$size = ifelse(data$Case == "N", "big", "small")
ggplot(data, 
  aes(x=Sample, y=value, color=Case, group = interaction(Variable,Case))) + 
  geom_line(position = "stack", aes(size = factor(size))) + 
  scale_color_manual(values = c("P" = "green4", "N" = "red", "L"="black")) +
  scale_size_manual(values=c("big" = 1.5, "small" = 0.5)) +
  guides(size = "none")

reprex package (v0.3.0)

于 2020-04-21 创建

您还可以直接在 scale_color_manual 上为每个函数分配特定值,如下所示: pd:还考虑到需要显示的所有 "Case" 个变量,并且必须为所有变量分配一个值。

ggplot(data, 
       aes(x = Sample, y = value, color = Case, group = Variable)) + 
  geom_line(position = "stack", aes(size = factor(Case))) + 
  scale_color_manual(values = c("P" = "green4", "N" = "red", "L"="black")) +
  scale_size_manual(values = c("L" = 0.11, "N" = 1, "P" = 2)) +
  guides(size = "none")