使用ggplot绘制多行时出错

时间:2017-10-03 13:32:38

标签: r plot ggplot2

我想绘制四条线,数据随机生成如下:

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)

我使用以下代码绘制了它:

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

但输出并不像预期的那样:

enter image description here

线条颜色混乱,它们应按顺序排列:图中的“橙色”,“黑色”,“橙色”,“黑色”。

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

但是,在此图中,线条颜色符合预期,但标签不符合预期。

你对这种奇怪的行为有什么想法吗?或者指出我缺少的细节。

更新

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

虽然代码大大简化,但线型并不像预期的那样。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

根据@ Richard的suggestion,我尝试使用tidyr::gather以及下面的预期解决方案。

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