Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有可变宽度元素的堆叠条形图?_Python_R_Graph_Ggplot2_Data Visualization - Fatal编程技术网

Python 具有可变宽度元素的堆叠条形图?

Python 具有可变宽度元素的堆叠条形图?,python,r,graph,ggplot2,data-visualization,Python,R,Graph,Ggplot2,Data Visualization,在Tableau中,我习惯于绘制下图。每天(或其他离散变量)都有一个不同颜色、高度和宽度类别的堆叠条 你可以想象这些类别是我展示给人们的不同广告。高度对应于我展示广告给的人的百分比,宽度对应于接受率 它让我可以很容易地看到哪些广告我应该经常展示(短而宽的条,如9月13日和14日的“C”类广告),哪些广告我应该少展示(高而窄的条,如9月16日的“H”类广告) 关于如何在R或Python中创建这样的图形,有什么想法吗 不幸的是,使用ggplot2(我认为)实现这一点并不是那么简单,因为geom\u

在Tableau中,我习惯于绘制下图。每天(或其他离散变量)都有一个不同颜色、高度和宽度类别的堆叠条

你可以想象这些类别是我展示给人们的不同广告。高度对应于我展示广告给的人的百分比,宽度对应于接受率

它让我可以很容易地看到哪些广告我应该经常展示(短而宽的条,如9月13日和14日的“C”类广告),哪些广告我应该少展示(高而窄的条,如9月16日的“H”类广告)

关于如何在R或Python中创建这样的图形,有什么想法吗


不幸的是,使用
ggplot2
(我认为)实现这一点并不是那么简单,因为
geom\u bar
实际上不支持更改相同x位置的宽度。但只要稍加努力,我们就能达到同样的结果:

制造一些假数据 为堆叠创建一个新变量 我想我们不能轻易地使用
position\u stack
,所以我们只能自己做这部分。基本上,我们需要计算每个酒吧的累积高度,按天分组。使用
dplyr
我们可以很容易地做到这一点

library(dplyr)
d2 <- d %>% group_by(day) %>% mutate(cum_height = cumsum(height))
如果您不想正确缩放宽度(小于1),可以使用镶嵌面:

ggplot(d2, aes(x = 1, y = cum_height - 0.5 * height, fill = adv)) +
  geom_tile(aes(width = width, height = height), show.legend = FALSE) +
  geom_text(aes(label = adv)) +
  facet_grid(~day) +
  scale_fill_brewer(type = 'qual', palette = 2) +
  labs(title = "Views and other stuff", y = "% of views", x = "")
结果

设置种子(1)

有没有可能提供一些样本数据?
library(dplyr)
d2 <- d %>% group_by(day) %>% mutate(cum_height = cumsum(height))
library(ggplot2)
ggplot(d2, aes(x = day, y = cum_height - 0.5 * height, fill = adv)) +
  geom_tile(aes(width = width, height = height), show.legend = FALSE) +
  geom_text(aes(label = adv)) +
  scale_fill_brewer(type = 'qual', palette = 2) +
  labs(title = "Views and other stuff", y = "% of views")
ggplot(d2, aes(x = 1, y = cum_height - 0.5 * height, fill = adv)) +
  geom_tile(aes(width = width, height = height), show.legend = FALSE) +
  geom_text(aes(label = adv)) +
  facet_grid(~day) +
  scale_fill_brewer(type = 'qual', palette = 2) +
  labs(title = "Views and other stuff", y = "% of views", x = "")
set.seed(1)
days <- 5
cats <- 8
dat <- prop.table(matrix(rpois(days * cats, days), cats), 2)

bp1 <- barplot(dat, col = seq(cats))
## some width for rect
rate <- matrix(runif(days * cats, .1, .5), cats)

## calculate xbottom, xtop, ybottom, ytop
bp   <- rep(bp1, each = cats)
ybot <- apply(rbind(0, dat), 2, cumsum)[-(cats + 1), ]
ytop <- apply(dat, 2, cumsum)

plot(extendrange(bp1), c(0,1), type = 'n', axes = FALSE, ann = FALSE)
rect(bp - rate, ybot, bp + rate, ytop, col = seq(cats))

text(bp, (ytop + ybot) / 2, LETTERS[seq(cats)])
axis(1, bp1, labels = format(Sys.Date() + seq(days), '%d %b %Y'), lwd = 0)
axis(2)
inv_col <- function(color) {
  paste0('#', apply(apply(rbind(abs(255 - col2rgb(color))), 2, function(x)
    format(as.hexmode(x), 2)), 2, paste, collapse = ''))
}

inv_col(palette())
# [1] "#ffffff" "#00ffff" "#ff32ff" "#ffff00" "#ff0000" "#00ff00" "#0000ff" "#414141"


plot(extendrange(bp1), c(0,1), type = 'n', axes = FALSE, ann = FALSE)
rect(bp - rate, ybot, bp + rate, ytop, col = seq(cats), xpd = NA, border = NA)

text(bp, (ytop + ybot) / 2, LETTERS[seq(cats)], col = inv_col(seq(cats)))
axis(1, bp1, labels = format(Sys.Date() + seq(days), '%d %B\n%Y'), lwd = 0)
axis(2)