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())
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
:
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:
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:
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:
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:
- how connect endpoint , startingpoint in polar-plot y-axis starting @
0
when usinggeom_line
in combinationcoord_polar
? - or without getting circle in middel when using
geom_polygon
/geom_path
in combinationcoord_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:
removing row
ri=360
(which duplicate ofri=0
)df <- df[df$ri!=360,]
transforming
ri
factor & addinggroup=1
aes
.adding
start=-pi*1/36
coord_polar
in order0
@ 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:
Comments
Post a Comment