r - How do I connect the endpoint & startpoint of geom_line in a polar plot (coord_polar)? -


i'm bit stuck getting endpoint of line in polar plot connect startpoint.

my data:

df <- structure(list(ri = 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, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360),                  n = c(329l, 315l, 399l, 700l, 919l, 757l, 656l, 918l, 1117l, 976l, 878l, 803l, 811l, 1072l, 1455l, 1642l, 1891l, 1688l, 1553l, 1841l, 2061l, 2321l, 2498l, 2080l, 1595l, 1080l, 1002l, 953l, 729l, 604l, 538l, 489l, 535l, 455l, 328l, 351l, 329l),                  d = c(0.008581340149717, 0.00821617673909074, 0.0104071572028483, 0.0182581705313128, 0.0239703695975378, 0.0197449072745768, 0.017110514097916, 0.0239442864967787, 0.0291348235478234, 0.0254571063408018, 0.022900962466418, 0.0209447299094916, 0.0211533947155638, 0.0279610840136675, 0.0379509116043715, 0.0428284514463079, 0.0493231435353035, 0.0440282740812228, 0.0405070554787553, 0.0480189884973526, 0.0537572706643366, 0.0605388768616813, 0.0651555856960275, 0.0542528495787579, 0.0416025457106341, 0.0281697488197397, 0.0261352669605363, 0.0248571950233444, 0.0190145804533243, 0.015754192858447, 0.0140327082083518, 0.0127546362711599, 0.0139544589060748, 0.0118678108453533, 0.00855525704895798, 0.0091551683664154, 0.008581340149717)),             .names = c("ri", "n", "d"), row.names = c(na, 37l), class = c("tbl_df", "tbl", "data.frame")) 

my first attempt @ making polar-plot code:

ggplot(df, aes(x=ri, y=d)) +   geom_line() +   scale_x_continuous(breaks=seq(0,360,10)) +   coord_polar() +   theme_bw() +   theme(panel.grid.minor=element_blank()) 

enter image description here

this produces more or less want. however, have y-axis starts @ 0. therefore, used following code:

ggplot(df, aes(x=ri, y=d)) +   geom_line() +   scale_x_continuous(breaks=seq(0,360,10)) +   scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +   coord_polar() +   theme_bw() +   theme(panel.grid.minor=element_blank()) 

unfortunately, line ends @ 350 , not connect 0/360: enter image description here

next tried:

ggplot(df, aes(x=ri, y=d)) +   geom_polygon(fill=na, color="black") +   scale_x_continuous(breaks=seq(0,360,10)) +   scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +   coord_polar() +   theme_bw() +   theme(panel.grid.minor=element_blank()) 

this code connect endpoint startpoint, creates circle: enter image description here

i tried geom_path, gave same result geom_polygon. analysing problem further, tried make normal plot with:

ggplot(df, aes(x=ri, y=d)) +   geom_line() +   scale_x_continuous(expand=c(0,0), breaks=seq(0,360,10)) +   scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +   theme_bw() +   theme(panel.grid.minor=element_blank()) 

which gives: enter image description here

as can see, there line between 350 , 360. doing same geom_plygon:

ggplot(df, aes(x=ri, y=d)) +   geom_polygon(fill=na, color="black") +   scale_x_continuous(expand=c(0,0), breaks=seq(0,360,10)) +   scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +   theme_bw() +   theme(panel.grid.minor=element_blank()) 

which results in: enter image description here

again, using geom_path instead of geom_polygon gives same result. so, problem seems result setting limits y-axis in combination coord_polar.

my questions:

  1. how connect endpoint , startingpoint in polar-plot y-axis starting @ 0 when using geom_line in combination coord_polar?
  2. or without getting circle in middel when using geom_polygon/geom_path in combination coord_polar?

note:
original dataset did not have row ri=0. added row myself. duplication of ri=360 row.

after trail , error, got wanted following steps:

  1. removing row ri=360 (which duplicate of ri=0) df <- df[df$ri!=360,]

  2. transforming ri factor & adding group=1 aes.

  3. adding start=-pi*1/36 coord_polar in order 0 @ top of polar plot

this results in following ggplot code:

ggplot(df, aes(x=factor(ri), y=d, group=1)) +   geom_polygon(fill=na, color="black") +   scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +   coord_polar(start=-pi*1/36) +   theme_bw() +   theme(panel.grid.minor=element_blank()) 

the resulting plot:

enter image description here


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -