0

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.

2
  • 1
    What's the error? Commented Oct 3, 2017 at 13:36
  • 2
    Much better to tidy your data (tidyr::gather) before using ggplot. Greatly simplifies the code Commented Oct 3, 2017 at 13:47

2 Answers 2

1
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

Sign up to request clarification or add additional context in comments.

4 Comments

Please check, the colors and line types are not as expected(or given in the commands).
+1 for the mistake identified. Still, the labels are not in sync. There are no dashed labels for Te ES, Tr ES. How to make it show these expected changes?
yes.. exactly. The dashed lines are not present in the labels(sorry, I meant legend).
Thanks for the expected output. Alternatively, it can be achieved easily using tidyr::gather.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.