分析两个数值向量的交集并创建饼图

发布于 2025-01-16 22:41:23 字数 431 浏览 3 评论 0原文

我有两个带有数值的向量:

v.typA <- c(1,2,110,111,20,22,46,80,89,100,210,320,999,1000)            # length = 14
v.typB <- c(1,2,10,11,20,22,40,44,60,66,80,88,100,210,320,430,540,666)  # length = 18

现在我想创建一个饼图,它以百分比形式可视化这两个向量的唯一值和重复项。我正在考虑这样的事情:

在此处输入图像描述

我该怎么做?

I'm having two vectors with numeric values:

v.typA <- c(1,2,110,111,20,22,46,80,89,100,210,320,999,1000)            # length = 14
v.typB <- c(1,2,10,11,20,22,40,44,60,66,80,88,100,210,320,430,540,666)  # length = 18

Now I want to create a pie chart, which visualizes the unique values and duplicates of these two vectors in percentage. I'm thinking of something like this:

enter image description here

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日落 2025-01-23 22:41:23

你心里有这样的想法吗?

library(dplyr)
library(ggplot)

v.typA <- c(1,2,110,111,20,22,46,80,89,100,210,320,999,1000)      
v.typB <- c(1,2,10,11,20,22,40,44,60,66,80,88,100,210,320,430,540,666)
vec <- c(v.typA,v.typB)

df <- tibble(var = c("Unique", "Duplicated"),
             val = c(length(unique(vec)), length(vec) - length(unique(vec)))) %>% 
  arrange(desc(var)) %>%
  mutate(prop = val / sum(val) *100) %>%
  mutate(ypos = cumsum(prop)- 0.5*prop )

ggplot(df, aes(x="", y=prop, fill=var)) +
  geom_bar(stat="identity", width=1, color="white") +
  coord_polar("y", start=0) +
  theme_void() + 
  theme(legend.position="none") +
  geom_text(aes(y = ypos, label = paste0(var,", ", prop,"%")), color = "white", size=6) +
  scale_fill_brewer(palette="Set1")

Do you have something like this in mind?

library(dplyr)
library(ggplot)

v.typA <- c(1,2,110,111,20,22,46,80,89,100,210,320,999,1000)      
v.typB <- c(1,2,10,11,20,22,40,44,60,66,80,88,100,210,320,430,540,666)
vec <- c(v.typA,v.typB)

df <- tibble(var = c("Unique", "Duplicated"),
             val = c(length(unique(vec)), length(vec) - length(unique(vec)))) %>% 
  arrange(desc(var)) %>%
  mutate(prop = val / sum(val) *100) %>%
  mutate(ypos = cumsum(prop)- 0.5*prop )

ggplot(df, aes(x="", y=prop, fill=var)) +
  geom_bar(stat="identity", width=1, color="white") +
  coord_polar("y", start=0) +
  theme_void() + 
  theme(legend.position="none") +
  geom_text(aes(y = ypos, label = paste0(var,", ", prop,"%")), color = "white", size=6) +
  scale_fill_brewer(palette="Set1")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文