0

I am trying to make a topographic map using different density points for density cells in a retina. The example of volcano3d is really nice and it gave me the idea so I think I will phrase my question according to this example. My question is if I have the contour of an island with x,y coordinates plotted like geom_path is it possible to construct a contour map inside the path only? I tried to do it but r tells me that the problem is that my x and y axis are not continuous. This is the first points in my data to give you an example, x and y are coordinates and freq should be z axis. Sorry for not putting the whole data but the contour has over 3000 points and then I have around 250 samplings inside so it is a long data set.

p <- ggplot(data = contour.xy, aes(x = x, y = y)) + 
  geom_path(data = contour.xy, aes(x = x, y = y)) + 
  geom_path(data = opticnerve.xy, aes(x = x, y = y)) + 
  geom_contour(data = counts, aes(x = "x", y = "y", z = "freq")) + 
  coord_equal()

I tried to put an image but I am a new user so because anti spam regulations I can't attach pics.

beroe
  • 11,784
  • 5
  • 34
  • 79
Eduardo Garza
  • 87
  • 1
  • 8

2 Answers2

1

Finally after doing some research found a package that helps me do exactly what I was looking for. It is spatstat written by Adrian Baddeley and he very kindly help me with a part of the script. Basically what it does is to create a window with an irregular shape, in this case my "island" and then all the points that I need to analyse fall inside the window. To create the window I used the following code using x vector as xp and y vector as yp: map <- owin(poly=list(x=xp, y=yp)) plot(map)

Then I can create all the points as point pattern (ppp)

Really appreciate all your help but in special to Adrian Baddeley for his support

Eduardo Garza
  • 87
  • 1
  • 8
0

Here's the volcano3d example (to provide a reproducible problem; code is taken from example(direct.labels) from the directlabels package).

volcano3d <- melt(volcano)
colnames(volcano3d) <- c("x", "y", "z")

You can draw a contour plot of the data, in this case Maunga Whau, like so:

(p <- ggplot(volcano3d, aes(x, y, z = z)) + 
  geom_contour()
)

There's also a problem with your sample code in that aes takes expressions, not strings. So you should have either

aes(x = x, y = y, z = freq)

or

aes_string(x = "x", y = "y", z = "freq")

Usually, you don't need to explicitly give the argument name for x and y, so in fact aes(x, y, z = freq) will do.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • It isn't obvious to me from the question where `geom_path` becomes useful. Let me know if this is what you want. – Richie Cotton Dec 21 '11 at 11:23
  • Thank you for the correction of the aes, it makes sense. even with that, I can't run the command. It tells me: Error in if (empty(new)) return(data.frame()) : missing value where TRUE/FALSE needed. – Eduardo Garza Dec 21 '11 at 12:59
  • The path is important for me because it gives me the shape of the island, outside of that I don't have any value and that's why I need to limit the contour function. The volcano example is really nice and you can play with many things but the topographic map doesn't have a limit. Lets say that we want to see the same volcano but in an island where the shape is not regular, then you have many sections of the plot empty. I hope it makes sense, with a picture could be easier to explain but I can't post them here – Eduardo Garza Dec 21 '11 at 13:02
  • So you want to only plot a subset of your volcano and the subset is defined by the limits of your island? – Marco Dec 21 '11 at 14:13
  • when I try to run the command geom_path it tells me: Error in if (empty(new)) return(data.frame()) : missing value where TRUE/FALSE needed. More than a subset of the volcano is like not having a continuous set of coordinates for the whole plot, my coordinates are limited by the shape of the island. Is like having a real map of a continent and you want the contour lines of the mountains in the continent not in a square like is in the plot. – Eduardo Garza Dec 21 '11 at 15:37
  • @EduardoGarza: This sort of question is so much easier to answer when the example is reproducible. Read this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Richie Cotton Dec 22 '11 at 16:18