2

I am facing an issue here. I owe to create a line plot composed of four lines, one of which should be dashed while the others remain continuous. Which I managed to do:

plot with four lines]

Nevertheless, the problem with this method is the legend does not appear as is in the plot. Here are my command-lines:

library(dplyr)
library(ggplot2)

DF$Distractor <- factor(DF$Distractor, levels = c("NF", "LSF", "HSF", "MASK")) ## Rearrange modalities
DF %>%
  mutate(dummy = ifelse(Distractor  != "MASK", 0, 1)) %>% ## MASK dashed ##
  ggplot(aes(x = Lag, y = ., colour = Distractor, group = Distractor, linetype = dummy %>% as.factor())) +
  guides(linetype = FALSE) + ## Drop the dummy legend ##
  geom_line(size = .75) +
  scale_colour_grey() +
  theme_classic() +
  labs(x = "Lag", y = "Probability of T2 report") +
  ylim(0, 1)

Here are the data:

   Distractor  Lag         .
1          NF Lag1 0.4556818
2          NF Lag3 0.2113636
3          NF Lag8 0.4954545
4         LSF Lag1 0.4238636
5         LSF Lag3 0.2397727
6         LSF Lag8 0.5659091
7         HSF Lag1 0.4579545
8         HSF Lag3 0.3125000
9         HSF Lag8 0.4954545
10       MASK Lag1 0.4170455
11       MASK Lag3 0.2022727
12       MASK Lag8 0.4443182

Is there a/another solution in order to get this straight? I would prefer a clean and neat one rather than tricky tricks.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

1 Answers1

1

One solution is to add override.aes in the guides option. The following code should do it:

DF %>%
    mutate(dummy = ifelse(Distractor  != "MASK", 0, 1)) %>% ## MASK dashed ##
    ggplot(aes(x = Lag, y = ., colour = Distractor, group = Distractor, linetype = dummy %>% as.factor())) +
    guides(linetype = FALSE,color=guide_legend(override.aes=list(linetype=c(1,1,1,2)))) + ## Drop the dummy legend ##
    geom_line(size = .75) +
    scale_colour_grey() +
    theme_classic() +
    labs(x = "Lag", y = "Probability of T2 report") +
    ylim(0, 1)

Notice the addition of color in the guides option. I hope this helps.

A Gore
  • 1,870
  • 2
  • 15
  • 26