Search code examples
rggplot2geom-area

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


I am using a geom_area to plot a set of Variables across samples stacked, and I would like to make the lines of a specific "Case" = N bigger and of a specific colour (red). I have already defined the colours, but how can I add the command scale_color_manual with the right code? I am working with a bigger dataset Variable=92 so I'd like to avoid plotting Variables individually...

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

enter image description here I have tried this code as well, but plots me both N values again, on top, and overlapping the already existing values:

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

enter image description here

Something similar to this could be?

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

Solution

  • A workaround: you can map a var to size using aes(size = ), then size can be reset manually using 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")
    

    Created on 2020-04-21 by the reprex package (v0.3.0)