分析两个数值向量的交集并创建饼图
Analyze the intersection of two numeric vectors and create pie chart
我有两个带有数值的向量:
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
现在我想创建一个饼图,以百分比形式可视化这两个向量的唯一值和重复值。我在想这样的事情:
我该怎么做?
你有这样的想法吗?
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")
我有两个带有数值的向量:
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
现在我想创建一个饼图,以百分比形式可视化这两个向量的唯一值和重复值。我在想这样的事情:
我该怎么做?
你有这样的想法吗?
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")