0

I wish to compare the observed values to the fitted ones. To do so, I decided to use a plot in R. What I want to do is to plot X vs Y and X vs Y.fitted on the same plot. I have written some code, but it is incomplete. My plot needs to look like this one below. On the plot, circles and crosses represent the observed and fitted values respectively

enter image description here

set.seed(1) x <- runif(8,0,1) y <- runif(8,0,1) y.fitted <- runif(8,0,1) plot(x,y,pch=1) plot(x,y.fitted,pch=5) 

3 Answers 3

4

In your code, the second plot will not add points to the existing plot but create a new one. You can + use the function points to add points to the existing plot.

plot(x, y, pch = 1) points(x, y.fitted, pch = 4) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sven. That is exactly what I was after
1

running plot the second time will create a new one. You could use points

set.seed(1) x <- runif(8,0,1) y <- runif(8,0,1) y.fitted <- runif(8,0,1) plot(x,y,pch=1) points(x,y.fitted,pch=5) 

2 Comments

In what this answer is different from the other one?
It is not, I posted just after the first answer. Must have been quite close as I did a refresh just before answering.
1

A solution with ggplot2 giving a better and neat graph outlook:

library(ggplot2) df = data.frame(x=runif(8,0,1),y=runif(8,0,1),y.fitted=runif(8,0,1)) df = melt(df, id=c('x')) ggplot() + geom_point(aes(x=x,y=value, shape=variable, colour=variable), df) 

2 Comments

Unless there is a specific reason to put the data and aes in the geom_points layer, I think the better habit is to put it in the ggplot call, thereby making it accessible to any other layer you might want to add. In this case you get the same result though.
I guess for melt a library is needed, reshape2 ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.