Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(barmekko)
export(marimekko)
59 changes: 59 additions & 0 deletions R/marimekko.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' Create a marimekko plot.
#'
#' A 100\% stacked bar chart with variable widths.
#'
#' @param data A data frame.
#' @param x A categorical variable defining the width categories.
#' @param fill A categorical variable defining the stacked segments.
#' @param width A numeric variable defining the bar widths.
#' @param values A boolean indicating whether to show percent labels in bars.
#' @return A marimekko constructed with ggplot2.
#' @export
#' @examples
#' library(ggplot2)
#' df <- data.frame(
#' region = rep(c('East', 'West'), each = 2),
#' result = rep(c('Good', 'Bad'), 2),
#' share = c(60, 40, 80, 20),
#' sales = c(300, 300, 200, 200)
#' )
#' marimekko(df, region, result, sales)
marimekko <- function(data, x, fill, width, values = FALSE) {
df <- data
xlabel <- as.character(substitute(x))
filllabel <- as.character(substitute(fill))
xval <- as.character(eval(substitute(x), df))
fillval <- as.character(eval(substitute(fill), df))
widthval <- eval(substitute(width), df)

x_levels <- unique(xval)
width_map <- tapply(widthval, xval, function(z) z[1])
widths <- as.numeric(width_map[x_levels])
pos <- positions(widths)

tbl <- as.data.frame(table(x = xval, fill = fillval), stringsAsFactors = FALSE)
tbl$prop <- tbl$Freq / ave(tbl$Freq, tbl$x, FUN = sum)
tbl$width <- width_map[tbl$x]
tbl$pos <- pos[match(tbl$x, x_levels)]
tbl$label_y <- ave(tbl$prop, tbl$x, FUN = function(z) cumsum(z) - z/2)

p <- suppressWarnings(
ggplot2::ggplot(tbl) +
ggplot2::geom_bar(
ggplot2::aes(x = pos, y = prop, width = width, fill = fill),
stat = "identity"
) +
ggplot2::scale_x_continuous(labels = x_levels, breaks = pos) +
ggplot2::xlab(xlabel) +
ggplot2::ylab("Proportion") +
ggplot2::guides(fill = ggplot2::guide_legend(title = filllabel))
)
if (values) {
labs <- round(tbl$prop * 100, 1)
p + ggplot2::geom_text(
ggplot2::aes(x = pos, y = label_y, label = labs), size = 3
)
} else {
p
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ bmx + theme(axis.text.x = element_text(angle = 90, hjust = 1))

### Appendix

- This package originally included a marimekko function as well, but the latter was discovered to be well covered in [other](https://CRAN.R-project.org/package=ggmosaic/vignettes/ggmosaic.html) [packages](https://www.semanticscholar.org/paper/Product-Plots-Wickham-Hofmann/0598a59354cb96161d68dab91fb0de21fb8671fd/figure/6), so it was removed to simplify maintenance
- The package now includes `marimekko()` to create 100% stacked mekko charts
- While the barmekko is a ggplot object, your mileage may vary when attempting operations that normally would work on a ggplot object, as the data frame is deconstructed in the process of creating the graph
35 changes: 35 additions & 0 deletions man/marimekko.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions vignettes/mekko-vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,5 @@ bmx + theme(axis.text.x = element_text(angle = 90, hjust = 1))

### Appendix

* This package originally included a marimekko function as well, but the latter
was discovered to be well covered in [other](https://CRAN.R-project.org/package=ggmosaic/vignettes/ggmosaic.html) [packages](https://www.semanticscholar.org/paper/Product-Plots-Wickham-Hofmann/0598a59354cb96161d68dab91fb0de21fb8671fd/figure/6), so it was removed to simplify maintenance
* While the barmekko is a ggplot object, your mileage may vary when attempting operations that normally would work on a ggplot object, as the data frame is deconstructed in the process of creating the graph
* The package now includes `marimekko()` to create 100% stacked mekko charts
* While the barmekko is a ggplot object, your mileage may vary when attempting operations that normally would work on a ggplot object, as the data frame is deconstructed in the process of creating the graph
2 changes: 1 addition & 1 deletion vignettes/mekko-vignette.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ bmx + theme(axis.text.x = element_text(angle = 90, hjust = 1))

### Appendix

- This package originally included a marimekko function as well, but the latter was discovered to be well covered in [other](https://CRAN.R-project.org/package=ggmosaic/vignettes/ggmosaic.html) [packages](https://www.semanticscholar.org/paper/Product-Plots-Wickham-Hofmann/0598a59354cb96161d68dab91fb0de21fb8671fd/figure/6), so it was removed to simplify maintenance
- The package now includes `marimekko()` to create 100% stacked mekko charts
- While the barmekko is a ggplot object, your mileage may vary when attempting operations that normally would work on a ggplot object, as the data frame is deconstructed in the process of creating the graph
3 changes: 1 addition & 2 deletions vignettes/second-mekko-vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,5 @@ state.x77 %>%

### Appendix

* This package originally included a marimekko function as well, but the latter
was discovered to be well covered in [other](https://CRAN.R-project.org/package=ggmosaic/vignettes/ggmosaic.html) [packages](https://www.semanticscholar.org/paper/Product-Plots-Wickham-Hofmann/0598a59354cb96161d68dab91fb0de21fb8671fd/figure/6), so it was removed to simplify maintenance
* The package now includes `marimekko()` to create 100% stacked mekko charts
* While the barmekko is a ggplot object, your mileage may vary when attempting operations that normally would work on a ggplot object, as the data frame is deconstructed in the process of creating the graph