I have data from several sources describing an y value in a 360 degrees space but I cannot plot them together with a fitted spline on a single polar plot.
我有来自几个来源的数据描述了360度空间中的y值,但是我不能将它们与单个极坐标图上的拟合样条曲线一起绘制。
Here's some simulated data:
这是一些模拟数据:
# Data for test
set.seed(35)
sim1 <- cbind(rnorm(6,0),seq(0,359,359/5))
sim2 <- cbind(rnorm(9,0),seq(0,359,359/8))
sim3 <- cbind(rnorm(7,0),seq(0,359,359/6))
If not doing a polar plot my procedure would be as follows:
如果不做极坐标图,我的程序如下:
# Create spline for points
total <- rbind(sim1,sim2,sim3)
fit= smooth.spline(total[,2],total[,1], cv=T)
# Classic solution if not polar plot
plot(sim1[,2],sim1[,1],ylim = c(-3,4), col = "darkgrey")
lines(sim1[,2],sim1[,1], pch=2, col = "darkgrey")
points(sim2[,2],sim2[,1], pch=2, col = "darkgrey")
lines(sim2[,2],sim2[,1], pch=2, col = "darkgrey")
points(sim3[,2],sim3[,1], pch=2, col = "darkgrey")
lines(sim3[,2],sim3[,1], pch=2, col = "darkgrey")
lines(fit, , col = "red")
Which would give me this kind of figure:
哪个会给我这样的数字:
情节
But trying to plot it in a polar plot. I cannot get further than plotting each individually:
但试图在极地情节中绘制它。我不能比单独绘制每个人更进一步:
# Plot
library(plotrix)
polar.plot(sim1[,1],sim1[,2],lwd=3,line.col="red", radial.lim=c(-3,3),clockwise=TRUE,rp.type = "s")
polar.plot(sim2[,1],sim2[,2],lwd=3,line.col="blue", radial.lim=c(-3,3),clockwise=TRUE,rp.type = "s")
polar.plot(sim3[,1],sim3[,2],lwd=3,line.col="darkgrey", radial.lim=c(-3,3),clockwise=TRUE,rp.type = "s")
情节不佳但360
I have also tried using ggplot2 as well as plotly but nothing yielded what I was hoping for.
我也试过使用ggplot2以及剧情,但没有产生我希望的东西。
2 个解决方案
#1
1
Use the add
parameter to add lines. Perhaps something like this?
使用add参数添加行。也许是这样的?
polar.plot(sim1[,1], sim1[,2], lwd=1, line.col = "grey20", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p")
polar.plot(sim2[,1], sim2[,2], lwd=1, line.col = "grey20", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p", add = TRUE)
polar.plot(sim3[,1], sim3[,2], lwd=1, line.col = "grey20", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p", add = TRUE)
polar.plot(fit$y, fit$x, lwd=2, line.col = "firebrick", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p", add = TRUE)
#2
0
GGplot alternative:
GGplot替代方案:
library(ggplot2,ggthemes)
# Data for test
set.seed(35)
sim1 <- cbind(rnorm(6,0),seq(0,359,359/5))
sim2 <- cbind(rnorm(9,0),seq(0,359,359/8))
sim3 <- cbind(rnorm(7,0),seq(0,359,359/6))
# Create spline for points
total <- rbind(sim1,sim2,sim3)
colnames(total)=c('Col1','Col2')
total=as.data.frame(total)
MyNames=c(rep('sim1',nrow(sim1)),rep('sim2',nrow(sim2)),rep('sim3',nrow(sim3)))
total=cbind(MyNames,total)
Radial=ggplot(total)+
theme_light()+
geom_line(aes(x=Col2,y=Col1,group=MyNames,colour=MyNames),alpha=0.4)+
geom_point(aes(x=Col2,y=Col1,group=MyNames,colour=MyNames))+
geom_line(aes(x=Col2,y=Col1),stat='smooth', method = "loess", span=0.5, alpha=0.4, size=1.2)+
scale_x_continuous(breaks=seq(0,360,by=60),expand=c(0,0),lim=c(0,360))+
coord_polar(theta='x',start=0)+
ggtitle('Sim')+
theme(axis.text=element_text(size=14),axis.title=element_text(size=16,face="bold"),legend.text=element_text(size=14),legend.title=element_text(size=14),title=element_text(size=16,face="bold"),plot.title = element_text(hjust = 0.5))
Radial
#1
1
Use the add
parameter to add lines. Perhaps something like this?
使用add参数添加行。也许是这样的?
polar.plot(sim1[,1], sim1[,2], lwd=1, line.col = "grey20", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p")
polar.plot(sim2[,1], sim2[,2], lwd=1, line.col = "grey20", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p", add = TRUE)
polar.plot(sim3[,1], sim3[,2], lwd=1, line.col = "grey20", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p", add = TRUE)
polar.plot(fit$y, fit$x, lwd=2, line.col = "firebrick", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p", add = TRUE)
#2
0
GGplot alternative:
GGplot替代方案:
library(ggplot2,ggthemes)
# Data for test
set.seed(35)
sim1 <- cbind(rnorm(6,0),seq(0,359,359/5))
sim2 <- cbind(rnorm(9,0),seq(0,359,359/8))
sim3 <- cbind(rnorm(7,0),seq(0,359,359/6))
# Create spline for points
total <- rbind(sim1,sim2,sim3)
colnames(total)=c('Col1','Col2')
total=as.data.frame(total)
MyNames=c(rep('sim1',nrow(sim1)),rep('sim2',nrow(sim2)),rep('sim3',nrow(sim3)))
total=cbind(MyNames,total)
Radial=ggplot(total)+
theme_light()+
geom_line(aes(x=Col2,y=Col1,group=MyNames,colour=MyNames),alpha=0.4)+
geom_point(aes(x=Col2,y=Col1,group=MyNames,colour=MyNames))+
geom_line(aes(x=Col2,y=Col1),stat='smooth', method = "loess", span=0.5, alpha=0.4, size=1.2)+
scale_x_continuous(breaks=seq(0,360,by=60),expand=c(0,0),lim=c(0,360))+
coord_polar(theta='x',start=0)+
ggtitle('Sim')+
theme(axis.text=element_text(size=14),axis.title=element_text(size=16,face="bold"),legend.text=element_text(size=14),legend.title=element_text(size=14),title=element_text(size=16,face="bold"),plot.title = element_text(hjust = 0.5))
Radial