【问题标题】:error while plotting multiple lines using ggplot使用 ggplot 绘制多条线时出错
【发布时间】:2018-03-14 17:24:03
【问题描述】:

我想画四条线,下面是随机生成的数据:

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

但输出却不如预期:

线条颜色乱七八糟,在图中应该是“橙色”、“黑色”、“橙色”、“黑色”的顺序。

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

但是,在此图中,线条颜色与预期一致,但标签与预期不同。

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

更新:

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

虽然代码大大简化了,但线型却不尽如人意。

【问题讨论】:

  • 错误是什么?
  • 在使用 ggplot 之前整理你的数据 (tidyr::gather) 要好得多。大大简化了代码

标签: r plot ggplot2


【解决方案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"))))

【讨论】:

  • 请检查,颜色和线型不符合预期(或在命令中给出)。
  • +1 表示发现的错误。尽管如此,标签仍然不同步。 Te ESTr ES 没有虚线标签。如何让它显示这些预期的变化?
  • 是的..完全正确。标签中没有虚线(对不起,我的意思是legend)。
  • 感谢您的预期输出。 Alternatively,使用tidyr::gather即可轻松实现。
【解决方案2】:

根据@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"))

【讨论】:

    猜你喜欢
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    相关资源
    最近更新 更多