简体   繁体   中英

How to draw a 2d heatmap in polar coordinates with ggplot2?

I have a tibble with x and y components of a planar velocity vector. If I draw a 2d heatmap with regular coordinates I get an expected result:

dyn_v %>% ggplot() + geom_bin2d(aes(vx, vy)) + scale_fill_viridis(trans = 'log10')

在此处输入图像描述

However, after applying coord_polar I get the following:

dyn_v %>% ggplot() + geom_bin2d(aes(vx, vy)) + scale_fill_viridis(trans = 'log10') + coord_polar() 

在此处输入图像描述

There's obviously a mistake here - the hotspot should be in the center. What am I doing wrong?

Got it. If you want to plot in the polar domain, you need to provide polar coordinates, simply applying coord_polar won't work.

So, if you have a tibble T with columns x and y and you want to plot a polar heatmap for them, then first transform x and y to r and phi , then pass those to geom_bin2d .


    (T
      %>% mutate(phi = (180/pi)*atan2(y, x))
      %>% mutate(r = sqrt(x*x + y*y))
      %>% ggplot(aes(phi, r))
      + geom_bin2d()
      + coord_polar()
      + scale_fill_viridis(trans = 'log10')
    )

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM