Prradep
Prradep

Reputation: 5696

error while plotting multiple lines using ggplot

I would like to plot four lines and the data is randomly generated below:

trE <- runif(12, 0.5, 0.8)
teE <- runif(12, 0.8, 1)
trES <- runif(12, 0, 0.3)
teES <- runif(12, 0.3, 0.5)
plotData <- data.frame(k=1:12, trE=trE, teE=teE, trES=trES, teES=teES)

I have plotted it using the below code:

ggplot(plotData, aes(k)) + 
  geom_line(aes(y = trE, colour = "Tr E")) + 
  geom_line(aes(y = teE, colour = "Te E")) +
  geom_line(aes(y = trES, colour = "Tr ES"), linetype="dashed") + 
  geom_line(aes(y = teES, colour = "Te ES"), linetype="dashed") +
  geom_hline(aes(yintercept = 0.5), linetype="dotted",colour='red') +
  scale_colour_manual(values=c("black", "orange","black", "orange"), 
                      labels=c("Tr E", "Te E", "Tr ES", "Te ES")) +
  scale_x_discrete(limits=1:12) +
  theme_bw()

But the output is not as expected:

enter image description here

The line colors are messed up and they should be in the order: "orange","black", "orange", "black" in the plot.

ggplot(plotData, aes(k)) + 
  geom_line(aes(y = trE, colour = "black")) + 
  geom_line(aes(y = teE, colour = "orange")) +
  geom_line(aes(y = trES, colour = "black"), linetype="dashed") + 
  geom_line(aes(y = teES, colour = "orange"), linetype="dashed") +
  geom_hline(aes(yintercept = 0.5), linetype="dotted",colour='red') +
  scale_colour_manual(values=c("black", "orange","black", "orange"), 
                      labels=c("Tr E", "Te E", "Tr ES", "Te ES")) +
  scale_x_discrete(limits=1:12) +
  theme_bw()

enter image description here

However, in this plot, the line colors are as expected but the labels are not as expected.

Do you have any thoughts for this weird behavior? or point me to the missing details.

update:

plotDataLong <- plotData %>% tidyr::gather(Error, value, 2:5)

ggplot(plotDataLong, aes(k, value, col=Error)) + geom_line() + 
  geom_hline(aes(yintercept = 0.5), linetype="dotted",colour='red') +
  scale_colour_manual(values=c("black", "orange","black", "orange"), 
                      labels=c("Tr E", "Te E", "Tr ES", "Te ES")) +
  scale_linetype(labels=c("solid","solid","dashed","dashed"))

enter image description here

Though the code is greatly simplified, the linetypes are not as expected.

Upvotes: 0

Views: 266

Answers (2)

Prradep
Prradep

Reputation: 5696

As per @Richard's suggestion, I have tried using tidyr::gather and the expected solutions below.

plotDataLong <- plotData %>% tidyr::gather(Error, value, 2:5)

ggplot(plotDataLong, aes(k, value, col=Error,linetype=Error)) + geom_line() +
  geom_hline(aes(yintercept = 0.5), linetype="dotted",colour='red') +
  scale_colour_manual(values=c("black", "black", "orange", "orange"),
                      name="Errors",
                      breaks=c('teE', 'teES',  'trE', 'trES'),
                      labels=c("Te E", "Te ES", "Tr E", "Tr ES")) +
  scale_linetype_manual(labels=c("Te E", "Te ES", "Tr E", "Tr ES"),
                 name="Errors",
                 breaks=c('teE', 'teES',  'trE', 'trES'),
                 values=c("solid","dashed","solid","dashed"))

enter image description here

Upvotes: 1

Marco Sandri
Marco Sandri

Reputation: 24252

set.seed(1)
trE <- runif(12, 0.5, 0.8)
teE <- runif(12, 0.8, 1)
trES <- runif(12, 0, 0.3)
teES <- runif(12, 0.3, 0.5)
plotData <- data.frame(k=1:12, trE=trE, teE=teE, trES=trES, teES=teES)

library(ggplot2)
ggplot(plotData, aes(k)) + 
  geom_line(aes(y = trE, colour = "Tr E"),lwd=1) + 
  geom_line(aes(y = teE, colour = "Te E"),lwd=1) +
  geom_line(aes(y = trES, colour = "Tr ES"), linetype="dashed",lwd=1) + 
  geom_line(aes(y = teES, colour = "Te ES"), linetype="dashed",lwd=1) +
  geom_hline(aes(yintercept = 0.5), linetype="dotted",colour='red') +
  scale_colour_manual(
     values=c("Te E"="orange","Tr E"="black", "Te ES"="orange", "Tr ES"="black"),
     breaks=c("Te E", "Tr E", "Te ES", "Tr ES")) +
  scale_x_discrete(limits=1:12) +
  theme_bw() +
  guides(colour = guide_legend(keywidth = 2, 
    override.aes = list(linetype = c("solid", "solid", "dashed", "dashed"))))

enter image description here

Upvotes: 1

Related Questions