Siva Sharan K
Siva Sharan K

Reputation: 43

How to plot multiple line graphs with different scales of data

I have a question on how to plot when I have different sets of data with different scales.

My data is as shown below

  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

My code would be as mentioned below

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

and my output graph is:

enter image description here

How can I plot a graph that can show all my data

My data

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

Upvotes: 3

Views: 1692

Answers (2)

eipi10
eipi10

Reputation: 93871

ggplot makes it relatively easy to plot all the lines at once and include a legend. In the code below, the melt function from the reshape2 package is used to convert your data from wide to long format:

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

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

enter image description here

Upvotes: 1

user7396508
user7396508

Reputation:

matplot and matlines are comparable to plot and lines, only they plot all columns in a matrix.

try the code

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

Edit

Looking again, I saw you were messing about with the axis ticks. You can dispense with all that if you provide column $q as the x coord

Upvotes: 2

Related Questions