MikiBV MikiBV - 17 days ago 6
R Question

How to combine ggplot and plot?

I have tried:

ggplot(temp, aes(x,y)) + geom_hex(bins=100) + plot(y1,x1,pch = 0, col="red")


The image is without the second part(red points).What should I change?

> str(x)
num [1:1397] 1.62 1.62 1.62 1.63 1.63 1.63 1.63 1.63 1.63 1.63 ...
> str(y)
int [1:1397] 2998 3001 3005 2993 2998 2999 3002 3003 3004 3006 ...
> str(x2)
num [1:6] 1.7 1.9 2.18 2.7 2.6 3
> str(y2)
num [1:6] 3000 3600 4000 4800 5100 5600


The important point is that vectors length is not the same(1397 versus 6).

It works perfect.I have made data.frame from x2 and y2.

ggplot(temp, aes(x,y)) +
+ geom_hex(bins=100) +
+ geom_point(data=mk , aes(x=x2, y=y2), col = "red")

Answer

If you want to combine them in one plot, you can do everything in ggplot2 (supposing that x1 and y1 are in dataframe df):

ggplot(temp, aes(x,y)) +
  geom_hex(bins=100) +
  geom_point(aes(x=x1, y=y1), data=df, shape=20, size=2, color="red")