8

I have input data in below format.

  x      y       z
  0      2.2     4.5
  5      3.8     6.8
  10     4.6     9.3
  15     7.6     10.5

How can i plot the xy scatter plot like excel (show below) in R?

enter image description here

marbel
  • 7,560
  • 6
  • 49
  • 68
Manish
  • 3,341
  • 15
  • 52
  • 87
  • Please review your terminology. Your title mentions "line plot", but your question and tags refer to "scatter plot". – A5C1D2H2I1M1N2O1R2T1 Jul 10 '13 at 04:46
  • Can you say what you are having trouble with? I think the downvotes are due to this info being missing. Also, I could be more specific / helpful w/ my answer, if I knew what exactly you were confused about. – gung - Reinstate Monica Jul 10 '13 at 04:49

2 Answers2

15

There at least four ways of doing this:

(1) Use a "horizontal" or "wide" data.frame called df here

df <- data.frame(x = c(0, 5, 10, 15), y = c(2.2, 3.8, 4.6, 7.6),z = c(4.5, 6.8, 9.3, 10.5))
    
ggplot(df, aes(x)) + 
  geom_line(aes(y = y, colour = "y")) +   
  geom_line(aes(y = z, colour = "z"))

(2) Using lattice

library(lattice)
xyplot(x ~ y + z, data=df, type = c('l','l'), col = c("blue", "red"), auto.key=T)

(3) Turn your original df into a "long" data.frame. This is how it's usually how you would work with data in ggplot2

library(reshape)
library(ggplot2)

mdf <- melt(df, id="x")  # convert to long format
ggplot(mdf, aes(x=x, y=value, colour=variable)) +
    geom_line() + 
    theme_bw()

enter image description here

(4) Using matplot() I haven't really explored much this option but here is an example.

matplot(df$x, df[,2:3], type = "b", pch=19 ,col = 1:2)
marbel
  • 7,560
  • 6
  • 49
  • 68
6

It might help if you could say what you're stuck on here. This is really quite trivial in R. You should look up the documentation for ?plot, and ?lines. For a simple overview, Quick R is great. Here's the code:

windows()
  plot(x, y, type="l", lwd=2, col="blue", ylim=c(0, 12), xaxs="i", yaxs="i")
  lines(x,z, lwd=2, col="red")
  legend("topleft", legend=c("y","z"), lwd=c(2,2), col=c("blue","red"))

Note that if you use a Mac, you need quartz() instead of windows(). Here's the plot:

enter image description here

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
  • The `windows` is not required - `plot` will invoke a new graphics device anyway. – thelatemail Jul 10 '13 at 04:34
  • 1
    These days I work exclusively in R Studio. If you don't add the `windows()` code, the plot will go into the plot quadrant, which I hate & where the aspect ratio gets distorted b/c I don't have it sized for a perfect square. Before I used R Studio, I also used the `windows()` code, b/c I always made multiple plots & I didn't want them overwritten. IMO, it's just a good habit to get into. – gung - Reinstate Monica Jul 10 '13 at 04:37
  • good to know - does `dev.new` work the same as `windows` inside RStudio? – thelatemail Jul 10 '13 at 04:41
  • It didn't seem to do anything. Certainly you can move between devices w/ `dev.set()`, eg. – gung - Reinstate Monica Jul 10 '13 at 04:47
  • 1
    @gung Why this graph does not start with 0? it starts with some value less than 0? – Manish Jul 10 '13 at 07:53
  • R automatically adds a sliver of whitespace between the plotting area (where the data are presented) and the border so that the border doesn't overlap / make it hard to see any of the data. The graph *does* start w/ 0. However, you can eliminate this added whitespace by adding the arguments `xaxs="i"` & `yaxs="i"` to your `plot()` function call (see more here: [remove-spacing-around-plotting-area-in-r](http://stackoverflow.com/questions/12300622/)). – gung - Reinstate Monica Jul 10 '13 at 14:19