Created
July 20, 2020 08:49
-
-
Save IndrajeetPatil/c74b0e927e24e3f8b0717dfc46906e66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# setup | |
set.seed(123) | |
library(ggplot2) | |
library(ggrepel) | |
library(forcats) | |
# data | |
(df <- structure( | |
list( | |
cyl = structure( | |
c(1L, 3L, 2L), | |
.Label = c("8", "6", "4"), | |
class = "factor" | |
), | |
counts = c(14L, 11L, 7L), | |
perc = c(43.75, 34.375, 21.875), | |
label = c("44%", "34%", "22%") | |
), | |
row.names = c(NA, -3L), | |
class = c("tbl_df", "tbl", "data.frame") | |
)) | |
# function | |
foo <- function(df, x) { | |
df$y.label <- 0.5 * df$counts + rev(cumsum(lag(rev(df$counts), default = 0))) | |
ggplot2::ggplot( | |
data = df, | |
mapping = ggplot2::aes(x = "", y = counts, fill = forcats::fct_inorder({{ x }})) | |
) + | |
ggplot2::geom_col( | |
color = "black", | |
width = 1, | |
na.rm = TRUE | |
) + | |
coord_polar("y", start = 0) + | |
ggrepel::geom_label_repel( | |
aes(label = label, y = y.label), | |
show.legend = FALSE, | |
nudge_x = 1 | |
) + | |
guides(fill = guide_legend(title = rlang::as_name(rlang::ensym(x)))) | |
} | |
foo(df, cyl) |
sessioninfo::session_info(include_base = TRUE)
#> - Session info ---------------------------------------------------------------
#> setting value
#> version R Under development (unstable) (2020-06-27 r78747)
#> os Windows 10 x64
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United States.1252
#> ctype English_United States.1252
#> tz Europe/Berlin
#> date 2020-07-20
#>
#> - Packages -------------------------------------------------------------------
#> ! package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.0)
#> base * 4.1.0 2020-06-28 [?] local
#> cli 2.0.2 2020-02-28 [1] CRAN (R 4.1.0)
#> P compiler 4.1.0 2020-06-28 [2] local
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.1.0)
#> P datasets * 4.1.0 2020-06-28 [2] local
#> digest 0.6.25 2020-02-23 [1] CRAN (R 4.1.0)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.0)
#> fansi 0.4.1 2020-01-08 [1] CRAN (R 4.1.0)
#> glue 1.4.1 2020-05-13 [1] CRAN (R 4.1.0)
#> P graphics * 4.1.0 2020-06-28 [2] local
#> P grDevices * 4.1.0 2020-06-28 [2] local
#> highr 0.8 2019-03-20 [1] CRAN (R 4.1.0)
#> htmltools 0.5.0 2020-06-16 [1] CRAN (R 4.1.0)
#> knitr 1.29 2020-06-23 [1] CRAN (R 4.1.0)
#> magrittr 1.5 2014-11-22 [1] CRAN (R 4.1.0)
#> P methods * 4.1.0 2020-06-28 [2] local
#> rlang 0.4.7 2020-07-09 [1] CRAN (R 4.1.0)
#> rmarkdown 2.3 2020-06-18 [1] CRAN (R 4.1.0)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.1.0)
#> P stats * 4.1.0 2020-06-28 [2] local
#> stringi 1.4.6 2020-02-17 [1] CRAN (R 4.1.0)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.0)
#> P tools 4.1.0 2020-06-28 [2] local
#> P utils * 4.1.0 2020-06-28 [2] local
#> withr 2.2.0 2020-04-20 [1] CRAN (R 4.1.0)
#> xfun 0.15 2020-06-21 [1] CRAN (R 4.1.0)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.0)
#>
#> [1] C:/Users/inp099/Documents/R/win-library/4.1
#> [2] C:/Program Files/R/R-devel/library
#>
#> P -- Loaded and on-disk path mismatch.
Created on 2020-07-20 by the reprex package (v0.3.0)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output plot