r - 向图例标签添加计数
问题描述
我正在尝试将计数/百分比添加到饼图的图例标签中。饼图很糟糕,我知道,但这不是这篇文章的重点。我想将“Count”的值粘贴到图例上的“Wound.Type”标签,但无法弄清楚如何访问以下代码每次迭代的计数。目标将类似于“Laceration 5”或任何计数。我尝试过“.~Count”和“.~Wound.Type”、“.$Count”和“.$Wound.Type”,但我不明白如何访问我想要的特定值。
p1 <- DF %>%
split(.$ServiceSite) %>%
imap(function(data, site) {
data %>%
group_by(ServiceSite, Wound.Type) %>%
summarise(Count = n()) %>%
mutate(share = round(Count / sum(Count), digits = 2)) %>%
ggplot(aes(x = "", y = Count, fill = Wound.Type)) +
geom_col(width = 1) +
scale_fill_discrete(labels = paste(.$Wound.Type, .$Count))+
facet_grid(facets = .~ServiceSite, labeller = label_value)+
geom_text(aes(label = Count, y = ), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y")+
labs(caption = "Visits from 1/1/18-6/30/18")+
ggtitle("Count of Unique Wound Occurrences")+
theme(plot.caption = element_text(size= 8, hjust = .5))+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.subtitle = element_text(hjust = 0.5))+
ylab("")+
xlab("")+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
})
p1
数据:
ServiceSite.x InitialType
2 Dermatitis
2 Diabetic
2 Pressure Injury
2 Pressure Injury
3 Pressure Injury
3 Other
3 Laceration
3 Other
4 Pressure Injury
4 MASD
4 Blister (Non-Pressure)
4 Skin Tear
4 Pressure Injury
5 Skin Tear
5 Other
5 Contusion
5 Skin Tear
5 Surgical(Non-Healing)
5 Pressure Injury
6 Pressure Injury
1 Pressure Injury
6 Pressure Injury
6 MASD
1 Surgical(Non-Healing)
1 Pressure Injury
1 Skin Tear
1 Contusion
解决方案
通常,您可以为此使用函数参数。由于data
参数引用数据集,您可以直接引用原始数据集中的变量。在你的例子中,那将是data$Wound.Type
.
但是,您Count
在函数中动态添加变量,因此它不在您传递给data
参数的数据集中。ggplot()
您可以在函数中创建一个新对象,而不是将数据集直接传递给您。这将允许您引用此“变异”数据集中的变量。
这是一个示例,我在其中创建了一个名为的新数据集,该数据集dat2
在内部使用ggplot()
并可用于fill
名称。
该函数的关键更改是在函数中创建一个新对象:
dat2 = data %>%
group_by(ServiceSite, Wound.Type) %>%
summarise(Count = n()) %>%
mutate(share = round(Count / sum(Count), digits = 2))
并使用此对象作为fill
标签:
scale_fill_discrete(labels = paste(dat2$Wound.Type, dat2$Count))
更改以及您的其余部分:
DF %>%
split(.$ServiceSite) %>%
imap(function(data, site) {
dat2 = data %>%
group_by(ServiceSite, Wound.Type) %>%
summarise(Count = n()) %>%
mutate(share = round(Count / sum(Count), digits = 2))
ggplot(dat2, aes(x = "", y = Count, fill = Wound.Type)) +
geom_col(width = 1) +
scale_fill_discrete(labels = paste(dat2$Wound.Type, dat2$Count))+
facet_grid(facets = .~ServiceSite, labeller = label_value)+
geom_text(aes(label = Count, y = ), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y")+
labs(caption = "Visits from 1/1/18-6/30/18")+
ggtitle("Count of Unique Wound Occurrences")+
theme(plot.caption = element_text(size= 8, hjust = .5))+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.subtitle = element_text(hjust = 0.5))+
ylab("")+
xlab("")+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
})
推荐阅读
- python - 在 python 中使用任务队列实现服务器
- amazon-web-services - 在没有 CloudFront 的情况下保护 S3 资产
- python - 如何在一个 lambda 函数中执行多项操作?
- rust - proc_macro_attribute 函数内部是否有一致的编译上下文?
- c++ - 为什么当 char *s = "123" 时 s[s[3]] = '1'?
- nginx - Nginx 正在更改授权标头 X-Forwarded-Authorization
- c# - DataLink 参数传递
- flask - Next.js Cookie 不持久
- python - 图像增强技术
- clojure - 了解 Clojure 惰性序列的新手问题