Reputation: 4996
I would like to create a polar/radar style graph in R that only shows a partial arc instead of a full circle.
I have looked at both the Plotrix and Pracma packages, but I've only been able to create a full circle polar chart like what I'm displaying here (created this is Pracma)
What I would like to do is create a 1/4 circle arc in the 0 to 270 degrees range of a polar arc chart and then plot data points that would radiate from the center towards the outer edges to indicate relative popularity. Data would indicate increasingly lower popularity/adoption rates as it approached the outer ring.
Upvotes: 1
Views: 455
Reputation: 93851
We create a polar plot layout from scratch in base graphics:
## Fake data
set.seed(493)
dat = data.frame(theta=runif(20, 3*pi/2, 2*pi), r=sample(1:10, 20, replace=TRUE))
# Calculate x and y values from r and theta
dat$x = dat$r * cos(dat$theta)
dat$y = dat$r * sin(dat$theta)
## 1:1 aspect ratio for plot
par(pty="s")
## Create plot layout
plot(NA,NA,
xlim=c(0, ceiling(max(c(dat$x,dat$y)))),
ylim=c(-ceiling(max(c(dat$x,dat$y))),0),
frame.plot=FALSE,
xaxt="n", yaxt="n", xlab="", ylab="")
## Add axes
axis(3, pos=0, at=seq(0,10,1))
axis(2, pos=0, at=seq(-10,0,1), las=1, labels=c(10:0))
## Add grid lines
angle = seq(0, -pi/2, length.out=100)
invisible(lapply(1:10, function(r) {
lines(r*cos(angle), r*sin(angle), lwd=0.5, col="grey40", lty="12")
}))
## Add data
invisible(lapply(1:nrow(dat), function(i) {
lines(c(0,dat$x[i]), c(0,dat$y[i]), col="blue")
}))
Using ggplot2:
library(ggplot2)
set.seed(493)
dat = data.frame(x=runif(20, 3*pi/2, 2*pi), value=sample(1:10, 20, replace=TRUE))
ggplot(dat, aes(x=x, xend=x, y=0, yend=value)) +
geom_segment(x=3*pi/2, xend=2*pi, y=0, yend=0 ,color="red") +
geom_segment(color="blue") +
scale_y_continuous(limits=c(-2,10), breaks=seq(0,10,2)) +
scale_x_continuous(limits=c(0,2*pi), breaks=seq(0, 2*pi, length.out=13)[-13],
labels=seq(0,360,length.out=13)[-13]) +
coord_polar(start=-pi/2, direction=-1)
Upvotes: 1