[R] ggplot: Pie Chart with correct labels

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Tue May 30 16:16:47 CEST 2017


>1. get the order of the labels right

You need to order your labels in the Land factor correctly when you create it with the factor function, which was (not) done here but rather before you used dput to generate this code. 

>2. Why I need to reference "breaks" and "labels" completely?

Read the documentation for the magrittr package. Non-standard evaluation (that allows one to use a data frame as an implied variable evaluation context) is not handled by the %>% operator, but by the functions you are typically using in your pipeline. (scale_y_continuous does not do so.) The %>% operator supports the implicit passing of the first argument to the function, as well as use of the dot  (.) for referencing that hidden argument in other arguments to the function (which is why you have to use the dot when you call the dplyr::do function).
-- 
Sent from my phone. Please excuse my brevity.

On May 30, 2017 6:30:00 AM PDT, G.Maubach at weinwolf.de wrote:
>Hi All,
>
>I would like to do the following pie chart using ggplot from an
>official 
>data source (
>http://www.deutscheweine.de/fileadmin/user_upload/Website/Service/Downloads/Statistik_2016-2017-neu.pdf
>, Tab 8, Page 14):
>
>-- cut --
>
>cat("# weinimport_piechart.R\n")
>
>
># -- Input --------------------------------------------
>
>d_wine_import_DE <- structure(list(Land = structure(1:24, .Label = 
>c("Italien", "Frankreich", 
>                                                 "Spanien", "USA", 
>"Südafrika", "Chile", "Österreich", "Australien", 
>                                                 "Portugal", 
>"Griechenland", "Argentinien", "Neuseeland", "Ungarn", 
>                                              "Mazedonien", "Schweiz", 
>"Dänemark", "Moldawien", "Türkei", "Belgien/Luxemburg", 
>                                                "Rumänien", "Ukraine", 
>"Kroatien", "Israel", "Georgien"), class = "factor"), 
>               Menge_hl_2015 = c(5481000, 2248000, 3824000, 493000, 
>845000, 
>                                539000, 308000, 446000, 153000, 99000, 
>64000, 43000, 123000, 
>                               186000, 5000, 9000, 28000, 7000, 10000, 
>15000, 4000, 4000, 
>                                 2000, 2000)), .Names = c("Land", 
>"Menge_hl_2015"), class = "data.frame", row.names = c(NA, 
>                                              -24L))
>names(d_wine_import_DE)
>
># -- Data ---------------------------------------------
>
>d_result <- data.frame(
>  country = d_wine_import_DE$Land,
>  abs = d_wine_import_DE$Menge_hl_2015) %>%
>  mutate(rel = round(abs / sum(abs) * 100, 1)) %>%
>  dplyr::arrange(desc(abs)) %>%
>  dplyr::mutate(rel_labs = paste(rel, "%")) %>%  # rev() does not work
> dplyr::mutate(breaks = cumsum(abs) - (abs / 2))  # rev() does not work
>
># -- Plot ---------------------------------------------
>
>d_result %>%
>   ggplot() +
>   geom_bar(
>     aes(x = "", y = abs, fill = country),
>     stat = "identity") +
>   # %SOURCE%
>   # coord_polar(): Wickham: ggplot2, Springer, 2nd Ed., p. 166
>   coord_polar(theta = "y", start = 0) +
>   guides(
>     fill = guide_legend(
>       title = "Länder",
>       reverse = FALSE)
>   ) +
>   scale_y_continuous(
>     breaks = d_result$breaks,  # simply "breaks" does not work
>     labels = d_result$rel_labs,  # simply "breaks" does not work
>     trans = "reverse"
>   ) +
>   # %SOURCE%
>   # Kassambra: Guide to Create Beautiful Graphics
>   # in R, sthda.com, 2nd Ed., 2013, p. 136ff
>   theme_minimal() +
>   theme(
>     panel.border = element_blank(),
>     panel.grid = element_blank(),
>     axis.title.x = element_blank(),
>     axis.title.y = element_blank()
>     # axis.text.x = element_text(size = 15)
>   ) +
>   labs(
>     title = paste0("Weinimport nach Deutschland 2015"))
>
>-- cut --
>
>I can not figure out how to align the labels (values in %) with the 
>reverse printed countries. Also the breaks and labels do need the
>dataset 
>name although I thought "breaks" and "rel_labs" is sufficient due to
>the 
>piping operator.
>
>Can you help me by telling how to
>
>1. get the order of the labels right
>2. Why I need to reference "breaks" and "labels" completely?
>
>Kind regards
>
>Georg
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list