0

Which function in R ggplot2 should I use to get this graph?

Here, the number "2" and "6" is about the number of cells that have different Division angle.

xiaoyong
  • 9
  • 5

1 Answers1

1

This might be a better answer. It's possible to do it using base graphics too but may need more work. Here's an example.

d <- structure(list(Angle = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 
100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 
230, 240, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 250
), Frequency = c(0, 0, 0.001, 2, 4, 18.03, 11, 12, 5, 7, 10, 
13, 2, 0.003, 0.01, 0, 1, 0.05, 2, 3, 3.7, 6, 0, 0, 0, 0, 0.2, 
0.006, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Angle", "Frequency"
), row.names = c(NA, 36L), class = "data.frame")

d$radian = d$Angle*pi/180
d$x = d$Frequency*cos(d$radian)
d$y = d$Frequency*sin(d$radian)
m = max(d$x, d$y)
graphics.off()
windows(width = 6, height = 6)

plot(x = m,
    y = m,
    xlim = c(-m,m),
    ylim = c(-m,m),
    type = "n",
    asp = 1,
    axes = FALSE,
    xlab = "",
    ylab = "")
par(xpd = TRUE)

symbols (x =c(0,0,0,0,0), y = c(0,0,0,0,0), circles=c(3,6,9,12,15), fg = "grey",
                                  add = TRUE, inches = FALSE, lty = 2)
lines(x = c(-15,15), y = c(0,0), lty = 2)
lines(x = c(0,0), y = c(-15,15), lty = 2)
text(x = c(0,0,-15,15), y = c(-15,15,0,0),
                           labels = c("180","0","270","90"), pos = c(1,3,2,4))

for (i in 1:nrow(d)){
    lines(x = c(0,d$x[i]), y = c(0,d$y[i]), lwd = 3)
}

enter image description here

NOTE: I screwed up when labeling angles

Community
  • 1
  • 1
d.b
  • 32,245
  • 6
  • 36
  • 77
  • This is really cool! But how could the "lines" become "histo-" just like small sectors ? – xiaoyong Mar 01 '17 at 04:06
  • 1
    I'm sorry @d.b. I mean that this graph can really represent the frequency of cells in different angles. But this is a line graph not a histogram. How to make a histogram? I just changed the lwd = 8 to make the line look like a bar, but it's not a histogram. – xiaoyong Mar 02 '17 at 01:54
  • @xiaoyong, I agree. If you wanted, you could use `polygon` but it would even more complicated. – d.b Mar 02 '17 at 01:56