首页 文章

如何用R绘制极坐标密度图?

提问于
浏览
0

我想构建一个像density map in polar coordinates这样的图(我是一个极坐标中的密度图 . 我不熟悉R,所以即使我发现this post,我仍然不知道如何得到我想要的东西 .

现在,我有笛卡尔坐标系中的散点图数据 . 如果有人能帮助我,我将不胜感激 . 非常感谢 .

=========================更新:我的解决方案===================== =========

cart2pol <- function(x){
# x: (x,y)
y <- x[2]
x <- x[1]
r <- sqrt(x^2 + y^2)
t <- atan2(y,x)/pi*180
c(r,t)
}

angle <- apply(cockpit.data[c('x1','y1')],1,cart2pol)[2,]
r <- apply(cockpit.data[c('x1','y1')],1,cart2pol)[1,]
observations <-table(cut(angle,breaks=c(seq(-180,180,by=15))),cut(r,breaks=c(seq(0,sight,by=25))))

mm <- melt(observations,c('angle','r'))

labels <- seq(-172.5,172.5,length.out = 24) - 90
labels[labels<=0] <- labels[labels<=0] + 360
labels.y <- as.vector(rbind('', seq(0,sight,by=50)[-1]))
rosedensity <- ggplot(mm,aes(angle,r,fill=value))+geom_tile()+
    coord_polar(start=pi/2, direction = -1) + ggtitle('Rose Density') +
    scale_fill_gradientn(name="Frequency", colours = rev(rainbow(32)[1:23])) + #terrain.colors(100) , brewer.pal(10,'Paired')
    scale_x_discrete(labels = labels) + scale_y_discrete(labels = labels.y) +
    xlab('Angle') + ylab('R') +
    theme(
        plot.title = element_text(color="red", size=28, face="bold.italic"),
        axis.title.y = element_text(color="black", size=24, face="bold"),
        axis.title.x = element_text(color="black", size=24, face="bold"),
        axis.text=element_text(size=20),
        legend.justification=c(1,0), legend.position=c(1,0),
        legend.background = element_rect(fill="gray90", size=.1, linetype="dotted")
    )
ggsave(rosedensity, file=paste(dirOutput,'rosedensity.png',sep=''), width=8, height=8)

这是我的Output Figure .

我找到了this answer的解决方案 .

1 回答

  • 2

    你可以这样试试:

    library(ggplot2)
    ggplot(faithful, aes(x = eruptions, y = waiting)) + 
      stat_density_2d(
        geom = "tile", 
        aes(fill = ..density..),
        n=c(40, 10), 
        contour = F
      ) + 
      scale_fill_gradientn(colours=rev(rainbow(32)[1:23])) + 
      coord_polar()
    

    enter image description here

相关问题