r - 酒吧作为ggplot中的一堆正方形
问题描述
我试图获得一个条形图,其中每个条形由一堆正方形组成,以便更容易计算每个条形上的观察值。这是一个最小的例子
library(ggplot2)
d = data.frame(p = rbinom(20,10,.3))
d %>% ggplot(aes(x=p))+geom_bar(fill="white",color="black",position="stack",alpha=.5)+
theme_void()
这给出了类似的东西:
基本上,我想在条形图的每个单元上都有水平分隔线。
解决方案
This approach can be useful:
library(ggplot2)
library(dplyr)
#Data
d = data.frame(p = rbinom(20,10,.3))
#Plot
d %>%
group_by(p) %>%
mutate(col=row_number()) %>%
ggplot(aes(x=p,fill=factor(col)))+
geom_bar(position="stack",alpha=.5,color='black')+
theme_void()+
scale_fill_manual(values=rep('white',5))+
theme(legend.position = 'none')
Output:
推荐阅读
- sql-server - Delphi ORD、SHL 和 SHR 函数 SQL Server 的等效项是什么?
- python - 调试 c 扩展时从 gdb 中的 pdb 单步执行
- php - 如何从 WooCommerce 中的新订单电子邮件中获取电子邮件收件人
- ionic-framework - 有没有办法知道 Ionic App Android 用户使用的是 WebView 还是 Chrome?
- php - Laravel 迁移 x Flyway 迁移
- database - 如何使用 VB.Net 在 Ms Access 中减去?
- json - 字符串函数不适用于 Python 中的 JSON 字符串
- php - 无法在常规 php 文件中导入 PHP 类
- python - 如何在张量流中对张量的列执行相似度函数
- netty - Netty 4 ChannelGroup.find(ChannelId id)但没有找到(String/Integer id)