如何绘制具有不同数据规模的多条折线图

How to plot multiple line graphs with different scales of data

我有一个问题,当我有不同比例的不同数据集时如何绘图。

我的数据如下图

  H.Q.2   A.D.q  C.D   total   q
1  3750 8424.00 5616 17790.0  50
2  7500 4212.00 5616 17328.0 100
3 11250 2808.00 5616 19674.0 150
4 15000 2106.00 5616 22722.0 200
5 18750 1684.80 5616 26050.8 250
6 22500 1404.00 5616 29520.0 300
7 26250 1203.43 5616 33069.4 350
8 30000 1053.00 5616 36669.0 400

我的代码如下所述

plot(s$A.D.q, type = "o",col="blue", axes = FALSE)
xticks <- seq(0, 30000, 1000)
axis(1,at=1:8, lab=s$q,las=2,)
axis(2,c(xticks),las=1)
lines(s$H.Q.2,type = "o", pch=22,lty=2,col="red")

我的输出图是:

如何绘制可以显示所有数据的图表

我的数据

structure(list(H.Q.2 = c(3750L, 7500L, 11250L, 15000L, 18750L, 
22500L, 26250L, 30000L), A.D.q = c(8424, 4212, 2808, 2106, 1684.8, 
1404, 1203.43, 1053), C.D = c(5616L, 5616L, 5616L, 5616L, 5616L, 
5616L, 5616L, 5616L), total = c(17790, 17328, 19674, 22722, 26050.8, 
29520, 33069.4, 36669), q = c(50L, 100L, 150L, 200L, 250L, 300L, 
350L, 400L)), .Names = c("H.Q.2", "A.D.q", "C.D", "total", "q"
), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8"))

matplotmatlines 相当于 plot 和 lines,只是它们绘制了矩阵中的所有列。

试试代码

matplot(s$q, s[, -5], type = 'l')

编辑

再看看,我看到你在搞乱轴刻度。如果您提供列 $q 作为 x 坐标

,则可以免除所有这些

ggplot 使得一次绘制所有线条并包含图例变得相对容易。在下面的代码中,reshape2 包中的 melt 函数用于将您的数据从宽格式转换为长格式:

library(ggplot2)
theme_set(theme_bw())
library(reshape2)

ggplot(melt(s, "q"), aes(q, value, colour=variable)) +
  geom_line() + geom_point()