[R] ggplot: Pie Chart with correct labels

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Tue May 30 15:30:00 CEST 2017


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



More information about the R-help mailing list