- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我坚持在 ggplot2 中创建图表。我正在尝试创建一个带有百分比的堆叠条形图,类似于此页面上的图表,但我正在努力在条形中添加百分比标签:How to draw stacked bars in ggplot2 that show percentages based on group?
我找到的所有尝试添加百分比标签的答案都使用类似于代码的东西
geom_text(aes(label = label), position = position_stack(vjust = 0.5), size = 2)
但它对我不起作用。
我的数据是这样的:
County Group Plan1 Plan2 Plan3 Plan4 Plan5 Total
County1 Group1 2019 597 513 5342 3220 11691
County2 Group1 521 182 130 1771 731 3335
County3 Group1 592 180 126 2448 1044 4390
County4 Group1 630 266 284 2298 937 4415
County5 Group1 708 258 171 2640 1404 5181
County6 Group1 443 159 71 1580 528 2781
County7 Group1 492 187 157 1823 900 3559
County8 Group1 261 101 84 1418 357 2221
我没有百分比的图表如下所示:
代码:
melt(df[df$Group=="Group1",],measure.vars = c("Plan1","Plan2","Plan3","Plan4", "Plan5"),variable.name = "Counties",value.name = "value") %>%
ggplot(aes(x=County,y=value,fill=Counties))+
geom_bar(stat = "identity",position="fill", color="black", width=0.9) +
labs(y="Percent", fill="Plan Type") + ylab("Percentage") + coord_flip() + scale_y_continuous(labels=scales::percent)
在使用了上面的geom_text()
代码后,变成了这个乱七八糟的样子:
代码:
melt(df[df$Group=="Group1",],measure.vars = c("Plan1","Plan2","Plan3","Plan4", "Plan5"),variable.name = "Counties",value.name = "value") %>%
ggplot(aes(x=County,y=value,fill=Counties))+
geom_bar(stat = "identity",position="fill", color="black", width=0.9) +
labs(y="Percent", fill="Plan Type") + ylab("Percentage") + coord_flip() + scale_y_continuous(labels=scales::percent)+
geom_text(aes(label=paste0(round(value/100),"%")), position=position_stack(vjust=0.5))
有什么建议吗?非常感谢任何建议/指导!谢谢!!
最佳答案
您的方法无效,因为标签不是以 % 而是以原始值表示。你必须自己做统计:
df <- read.table(text="County Group Plan1 Plan2 Plan3 Plan4 Plan5 Total
County1 Group1 2019 597 513 5342 3220 11691
County2 Group1 521 182 130 1771 731 3335
County3 Group1 592 180 126 2448 1044 4390
County4 Group1 630 266 284 2298 937 4415
County5 Group1 708 258 171 2640 1404 5181
County6 Group1 443 159 71 1580 528 2781
County7 Group1 492 187 157 1823 900 3559
County8 Group1 261 101 84 1418 357 2221", header = TRUE)
library(tidyverse)
df %>%
filter(Group == "Group1") %>%
select(-Total) %>%
gather(key = `Plan Type`, value = value, -County, -Group) %>%
group_by(County, Group) %>%
mutate(Percentage = value/sum(value)) %>%
ggplot(aes(x = County, y = Percentage, fill = `Plan Type`, label = paste0(round(Percentage*100), "%"))) +
geom_col(position = position_stack(), color = "black") +
geom_text(position = position_stack(vjust = .5)) +
coord_flip() +
scale_y_continuous(labels = scales::percent_format())
编辑:
上面的代码同样适用于更多计划和更多组,但情节不会说明这一点。只需添加 facet_wrap
即可生成关于组的灵活绘图:
df %>%
filter(Group == "Group1") %>%
select(-Total) %>%
gather(key = `Plan Type`, value = value, -County, -Group) %>%
group_by(County, Group) %>%
mutate(Percentage = value/sum(value)) %>%
ggplot(aes(x = County, y = Percentage, fill = `Plan Type`, label = paste0(round(Percentage*100), "%"))) +
geom_col(position = position_stack(), color = "black") +
geom_text(position = position_stack(vjust = .5)) +
coord_flip() +
scale_y_continuous(labels = scales::percent_format()) +
facet_wrap(~Group)
关于r - 将百分比标签添加到堆积条形图ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47936123/
我使用 Dojo fadeIn 和 fadeOut 以及一个计时器旋转了三个堆叠图像。只有最后一个的 href 可用。是否也可以旋转 href? 这是它的 CSS: #main-slides
给定一个 numpy 数组,我想总结统一的元素 block 以形成一个新的、更小的数组。它与分箱类似,但不是按频率分箱。除了通过示例(如下)之外,我不确定如何描述它。 问题:是否有用于此的函数或更清晰
我正在尝试实现某种按钮控制的幻灯片放映,其中包括用于页面顶部全 Angular 图片的 div,用于页面顶部的 div页面底部的另一张全 Angular 图片和中央内容的最终 div(包括控制“幻
嘿,我正在使用 D3JS 作为图表库,我真的很想利用气泡图中的超酷功能。上主D3JS chart站点下面的Bubble Chart用来比较两组数据: Bubble Chart . 我想知道是否有人真的
我是一名优秀的程序员,十分优秀!