如何在R中制作华夫饼图?


11

如何在R中使用饼图来绘制华夫饼图呢?

help.search("waffle")
No help files found with alias or concept or title matching waffle
using fuzzy matching.

我发现在Google上搜索得最近的是镶嵌图。


我不知道,但是为什么不使用更好的方法呢?点状图要好得多。
彼得·弗洛姆

2
对于那些谁想要知道什么饼图表是,罗伯特·科萨拉对渴望的眼神博客有一段关于他们。还要注意乔恩·佩尔捷的评论。
安迪W

我能找到的最接近的东西是这个。FWIW,我同意Peter的观点,在可视化数据时避免使用馅饼和华夫饼。

Answers:


13

现在有一个名为waffle的软件包。

来自github页面的示例:

parts <- c(80, 30, 20, 10)
waffle(parts, rows=8)

结果:

结果

问候


我不知道这些被称为“华夫饼图”。我喜欢它们-很好的饼图替换
-Shadowtalker

7

我怀疑geom_tile包装ggplot2中的物品可以满足您的需求。Shane 对此StackOverflow问题答案应该可以帮助您入门。

编辑:这是一个示例,还有一些其他图表可供比较。

library(ggplot2)

# Here's some data I had lying around
tb <- structure(list(region = c("Africa", "Asia", "Latin America", 
"Other", "US-born"), ncases = c(36L, 34L, 56L, 2L, 44L)), .Names = c("region", 
"ncases"), row.names = c(NA, -5L), class = "data.frame")


# A bar chart of counts
ggplot(tb, aes(x = region, weight = ncases, fill = region)) +
    geom_bar()

# Pie chart.  Forgive me, Hadley, for I must sin.
ggplot(tb, aes(x = factor(1), weight = ncases, fill = region)) +
    geom_bar(width = 1) +
    coord_polar(theta = "y") +
    labs(x = "", y = "")

# Percentage pie.
ggplot(tb, aes(x = factor(1), weight = ncases/sum(ncases), fill = region)) +
    geom_bar() +
    scale_y_continuous(formatter = 'percent') +
    coord_polar(theta = "y") +
    labs(x = "", y = "")


# Waffles
# How many rows do you want the y axis to have?
ndeep <- 5

# I need to convert my data into a data.frame with uniquely-specified x
# and y coordinates for each case
# Note - it's actually important to specify y first for a
# horizontally-accumulating waffle
# One y for each row; then divide the total number of cases by the number of
# rows and round up to get the appropriate number of x increments
tb4waffles <- expand.grid(y = 1:ndeep,
                          x = seq_len(ceiling(sum(tb$ncases) / ndeep)))

# Expand the counts into a full vector of region labels - i.e., de-aggregate
regionvec <- rep(tb$region, tb$ncases)

# Depending on the value of ndeep, there might be more spots on the x-y grid
# than there are cases - so fill those with NA
tb4waffles$region <- c(regionvec, rep(NA, nrow(tb4waffles) - length(regionvec)))

# Plot it
ggplot(tb4waffles, aes(x = x, y = y, fill = region)) + 
    geom_tile(color = "white") + # The color of the lines between tiles
    scale_fill_manual("Region of Birth",
                      values = RColorBrewer::brewer.pal(5, "Dark2")) +
    opts(title = "TB Cases by Region of Birth")

华夫饼图示例

显然,要做正确的美学工作还有很多工作要做(例如,这些轴到底意味着什么?),但这就是它的原理。我把“漂亮”留给读者练习。


3

这是使用@jbkunst的数据在base r中的一个:

waffle <- function(x, rows, cols = seq_along(x), ...) {
  xx <- rep(cols, times = x)
  lx <- length(xx)
  m <- matrix(nrow = rows, ncol = (lx %/% rows) + (lx %% rows != 0))
  m[1:length(xx)] <- xx

  op <- par(no.readonly = TRUE)
  on.exit(par(op))

  par(list(...))
  plot.new()
  o <- cbind(c(row(m)), c(col(m))) + 1
  plot.window(xlim = c(0, max(o[, 2]) + 1), ylim = c(0, max(o[, 1]) + 1),
              asp = 1, xaxs = 'i', yaxs = 'i')
  rect(o[, 2], o[, 1], o[, 2] + .85, o[, 1] + .85, col = c(m), border = NA)

  invisible(list(m = m, o = o))
}


cols <- c("#F8766D", "#7CAE00", "#00BFC4", "#C77CFF")
m <- waffle(c(80, 30, 20, 10), rows = 8, cols = cols, mar = c(0,0,0,7),
            bg = 'cornsilk')
legend('right', legend = LETTERS[1:4], pch = 15, col = cols, pt.cex = 2,
       bty = 'n')

在此处输入图片说明


2
所有示例似乎都具有较高的墨水:信息比率。
Frank Harrell

1
我同意@Frank Harrell。这个例子令人信服。我喜欢无法衡量的图表,但是对于这个示例,可以期望读者理解具有四个频率的表格。如果首选图形,则点图或条形图会更简单(频率也可以添加为注释)。我可以想象对非常小的孩子有一些教学上的价值。
尼克·考克斯

1
因此,您说的是,当我在年度条形图大会上展示此图时,我应该期望人群中有很多仇恨者吗?感谢大家的注意
rawr 2015年

转过来:该图似乎在对读者说:看这里,您可以指望自己理解该图!如果数字很大,那是不可能的。如果数字很小,它仍然没有比其他图表有用的多。对于小孩来说,这是一种增强,因此他们可以理解图形。还有谁需要该消息?
尼克·考克斯

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.