R 在折线图的多行中添加更多行?

R 在折线图的多行中添加更多行?,r,R,我正在做一个R类型的“折线图中的多条线”图表,我想在同一个图表中用5个数字绘制6个向量 问题是,我绘制的线不超过2条,当我尝试绘制第3条线时,它不会显示任何内容 it1 <- c(1406, 1504, 1623, 1405, 1447) it2 <- c(1565, 1496, 1555, 1590, 1555) it3 <- c(459, 534, 534, 626, 626) it4 <- c(642, 643, 482, 661, 651) it5 <- c

我正在做一个R类型的“折线图中的多条线”图表,我想在同一个图表中用5个数字绘制6个向量

问题是,我绘制的线不超过2条,当我尝试绘制第3条线时,它不会显示任何内容

it1 <- c(1406, 1504, 1623, 1405, 1447)
it2 <- c(1565, 1496, 1555, 1590, 1555)
it3 <- c(459, 534, 534, 626, 626)
it4 <- c(642, 643, 482, 661, 651)
it5 <- c(538, 558, 456, 393, 551)
it6 <- c(521, 517, 466, 456, 496)

plot(it1,type="l",col="red")
lines(it2, col="green")
lines(it3, col="blue") #bad

it1Try
matplot

    it1 <- c(1406, 1504, 1623, 1405, 1447)
    it2 <- c(1565, 1496, 1555, 1590, 1555)
    it3 <- c(459, 534, 534, 626, 626)
    it4 <- c(642, 643, 482, 661, 651)
    it5 <- c(538, 558, 456, 393, 551)


it = data.frame(it1, it2, it3, it4, it5)

matplot(it, type = c("b"),pch=1,col = 1:5) 
legend("center", legend = 1:5, col=1:5, pch=1) 

尝试
matplot

    it1 <- c(1406, 1504, 1623, 1405, 1447)
    it2 <- c(1565, 1496, 1555, 1590, 1555)
    it3 <- c(459, 534, 534, 626, 626)
    it4 <- c(642, 643, 482, 661, 651)
    it5 <- c(538, 558, 456, 393, 551)


it = data.frame(it1, it2, it3, it4, it5)

matplot(it, type = c("b"),pch=1,col = 1:5) 
legend("center", legend = 1:5, col=1:5, pch=1) 

它不会显示,因为
it3
完全低于第一个绘图设置的轴范围。添加类似这样的后续绘图时,它不会重新缩放轴,而是使用缩放到第一个绘图的轴。您可以在第一个绘图中手动指定轴范围,然后显示所有轴范围;但是,我建议使用类似于
ggplot2
的方法来实现这一点

plot(it1,type="l",col="red", ylim = c(0, 1800))
lines(it2, col="green")
lines(it3, col="blue") #now works

如果要使用常用的
ggplot2
软件包执行此操作,则必须将数据重新格式化。有几种软件包常用于此,如
tidyr
reformae2
。它看起来像这样:

it_lines <- data.frame(it1,it2,it3,it4,it5,it6)
it_lines$index <- row.names(it_lines)

# Reshape into long format for ggplot2 - requires tidyr, but can be done 
# with other approaches like reshape2's melt/cast
it_lines <- tidyr::gather(it_lines, key = it, value = value, it1:it6)

# Plot
library(ggplot2)
ggplot(it_lines, aes(x = index, y = value, group = it, colour = it)) + 
geom_line()

it\u line它不会显示,因为
it3
完全低于第一个绘图设置的轴范围。添加类似这样的后续绘图时,它不会重新缩放轴,而是使用缩放到第一个绘图的轴。您可以在第一个绘图中手动指定轴范围,然后显示所有轴范围;但是,我建议使用类似于
ggplot2
的方法来实现这一点

plot(it1,type="l",col="red", ylim = c(0, 1800))
lines(it2, col="green")
lines(it3, col="blue") #now works

如果要使用常用的
ggplot2
软件包执行此操作,则必须将数据重新格式化。有几种软件包常用于此,如
tidyr
reformae2
。它看起来像这样:

it_lines <- data.frame(it1,it2,it3,it4,it5,it6)
it_lines$index <- row.names(it_lines)

# Reshape into long format for ggplot2 - requires tidyr, but can be done 
# with other approaches like reshape2's melt/cast
it_lines <- tidyr::gather(it_lines, key = it, value = value, it1:it6)

# Plot
library(ggplot2)
ggplot(it_lines, aes(x = index, y = value, group = it, colour = it)) + 
geom_line()

it_lines或者您可以简单地使用
ggplot2
和一些
tidyverse
工具来绘制它们,并制作比
matplotlib
更好看的东西:

it1 <- c(1406, 1504, 1623, 1405, 1447)
it2 <- c(1565, 1496, 1555, 1590, 1555)
it3 <- c(459, 534, 534, 626, 626)
it4 <- c(642, 643, 482, 661, 651)
it5 <- c(538, 558, 456, 393, 551)
it6 <- c(521, 517, 466, 456, 496)

library(tidyverse)

data <- tibble(index = 1:5, it1,it2,it3,it4,it5,it6) %>%
  gather(var, value, -index)

ggplot(data, aes(x = index, y = value, colour = var)) +
  geom_line()

it1或者您可以简单地使用
ggplot2
和一些
tidyverse
工具来绘制它们,并制作比
matplotlib
更好看的东西:

it1 <- c(1406, 1504, 1623, 1405, 1447)
it2 <- c(1565, 1496, 1555, 1590, 1555)
it3 <- c(459, 534, 534, 626, 626)
it4 <- c(642, 643, 482, 661, 651)
it5 <- c(538, 558, 456, 393, 551)
it6 <- c(521, 517, 466, 456, 496)

library(tidyverse)

data <- tibble(index = 1:5, it1,it2,it3,it4,it5,it6) %>%
  gather(var, value, -index)

ggplot(data, aes(x = index, y = value, colour = var)) +
  geom_line()

it1您好,谢谢您的解决方案,但是我如何才能将图例置于图表之外?您好,谢谢您的解决方案,但是我如何才能将图例置于图表之外?