2

I have data that is computed on a 2D polar mesh:

# The mesh created is in two dimensions: r and theta.
# Mesh steps in theta are regular, while mesh steps in r are more refined
# close to the origin
nb.theta <- 50
theta.max <- 130
theta <- seq(0, theta.max, length.out = nb.theta)

nb.r <- 80
# r goes from r0 to rMax
q0 <- 1.1
z <- seq(1, nb.r)
rMax <- 30
r0 <- rMax / (q0 ^ nb.r - 1)
r <- r0 * (q0 ^ z - 1)

# Now let's add some data
mesh <- as.data.frame(expand.grid(r = r, theta = theta))
mesh$value <- mesh$r * mesh$theta / theta.max

Now, I want to plot the mesh in R (preferably with ggplot2). I tried:

ggplot(mesh, aes(r, theta, color = value)) + geom_point() + coord_polar(theta = "y")

But the result is far from satisfactory:

enter image description here

Ideally, I would like to have cells filled and not just points. I also would like the plot not to be a full circle: I only have data from 0 to 130 degrees. Is this possible?

Ben
  • 6,321
  • 9
  • 40
  • 76

2 Answers2

2

This should solve the circle issue:

ggplot(mesh, aes(r, theta, color = value)) + 
    geom_point() + 
    coord_polar(theta = "y") + 
    scale_y_continuous(limits=c(0,360))
Brian D
  • 2,570
  • 1
  • 24
  • 43
1

We can use geom_tile rather than geom_point so that we fill the mesh. We need to calculate the width of each window. Here I've just set it to r/10 which is approximately correct. You will be able to calculate it exactly.

Adding ylim ensures that only part of the circle is filled.

mesh <- expand.grid(r = r, theta = theta)
mesh$value <- mesh$r * mesh$theta / theta.max
mesh$width <- mesh$r/10


ggplot(mesh, aes(r, theta, fill = value, width = width)) + 
    geom_tile() + 
    coord_polar(theta = "y") +
    ylim(0, 360)

NB expand.grid returns a data.frame, so we don't need to convert it.

enter image description here

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
  • That's nice. But the plot is still half empty. Would it be possible to cut the plot, say with a line going from 0 to 180 degrees? – Ben Mar 14 '17 at 20:06
  • Perhaps - see http://stackoverflow.com/questions/22398350/how-to-show-only-part-of-the-plot-area-of-polar-ggplot-with-facet/22418817#22418817 – Richard Telford Mar 14 '17 at 20:14
  • Thanks for the link. It's an ugly hack tough. – Ben Mar 14 '17 at 20:15
  • Can we just stay in Cartesian coordinates and change the shape of the tiles ? – Ben Mar 14 '17 at 20:21