-1

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")
Jaap
  • 81,064
  • 34
  • 182
  • 193
MikiBV
  • 43
  • 4
  • 3
    you can add points with ggplot. + `geom_point(data.frame(x = x1, y=y1), aes(x=x, y=y), col = "red")` instead of `plot(...` – adibender Jul 01 '14 at 14:00
  • 1
    Just do everything in ggplot2. Is it so difficult to just use `+ geom_point(data.frame(x=x1, y=y1), shape=0, colour="red")`? – Roland Jul 01 '14 at 14:01
  • No way!ggplot(temp, aes(x,y)) + geom_hex(bins=100) + geom_point(data.frame(x=y1, y=x1), shape=0, colour="red") Error in get(x, envir = this, inherits = inh)(this, ...) : Mapping should be a list of unevaluated mappings created by aes or aes_string – MikiBV Jul 01 '14 at 14:18
  • 1
    `+ geom_point(data=data.frame(x=x1, y=y1), shape=0, colour="red")` It's difficult to test solutions without a reproducible example. -1 – Roland Jul 01 '14 at 14:25
  • It might be a good idea to read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). This will make it much easier for others to help you. – Jaap Jul 01 '14 at 14:28

2 Answers2

2

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")
baptiste
  • 75,767
  • 19
  • 198
  • 294
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 1
    You're thinking of the **gridBase** package, not **gridExtra**. `grid.arrange` will only work on grid objects. – joran Jul 01 '14 at 14:12
  • Yes,joran,it was impossible with geidExtra. – MikiBV Jul 01 '14 at 14:25
  • @joran thanks for pointing me to that misinterpretation of the `gridExtra` package functionality, I deleted the corresponding part of the answer. – Jaap Jul 01 '14 at 14:30
  • @Jaap not working again,Error: ggplot2 doesn't know how to deal with data of class uneval.Vectors length is not the same! – MikiBV Jul 01 '14 at 14:38
  • 1
    @MikiBV as pointed out in my comment to your question: give us some data. This will make it much easier for us to help you. Follow the link in my other comment to get some info on how provide that data. – Jaap Jul 01 '14 at 14:41
  • @Jaap ,yes it works now,I have created data frame form x2 and y2. – MikiBV Jul 01 '14 at 14:56
1

It can be done using the package gridBase. I still prefer to use sp.plot than ggplot for plotting maps. So this feature might be useful. I should mention this page as the information that lead me to the answer.

A dummy example:

library(gridBase)
library(grid)
library(ggplot2)

data  <- data.frame(x=1:10,y=rnorm(10))

par(mfrow = c(1,2))

vp <- viewport(height = unit(1,"npc"), width=unit(0.5, "npc"), 
              just = c("left","top"),
              y = 0, x = 0)


print(plot(data$x,data$y,xlab="x",ylab="y",pch = 21,bg="black"),vp=vp)

vp <- viewport(height = unit(1,"npc"), width=unit(0.5, "npc"), 
              just = c("left","top"),
              y = 1, x = 0.5)


print(ggplot(data,aes(x,y)) + geom_point(),vp=vp)
DJJ
  • 2,481
  • 2
  • 28
  • 53