# Issue

i have a dataset with three columns, small example:

```
A B
1 sety NA
2 NA bety
3 NA bety
4 sety bety
5 sety NA
```

how can i plot a pieplot where i have percentage of people having sety, percentage of people having bety, and percentage of people having both sety and bety, in the example above , the percents are respectively 60%?, 60%, 20%, The third percent must show as overlap between the first two.

may be something like this :

# Solution

- Arrange and summarize your data to find “start” and “stop” positions for each set, and convert to percentages. You’ll use these positions as
`ymin`

and`ymax`

for`geom_rect()`

. - Take the median of
`ymin`

and`ymax`

as the position for your % labels. - Create a separate dataframe with the value and position for the overlap label.
- Dodge the rects and labels for each group using
`position_dodge()`

, and add transparency using`alpha`

to emphasize the overlap. - Circularize using
`coord_polar()`

.

```
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
plot_data <- dat %>%
arrange(A, !is.na(B)) %>%
summarize(across(
A:B,
list(
ymin = ~ (first(which(!is.na(.x))) - 1) / n(),
ymax = ~ last(which(!is.na(.x))) / n(),
pct = ~ sum(!is.na(.x)) / n()
)
)) %>%
pivot_longer(everything(), names_to = c("set", ".value"), names_sep = "_") %>%
mutate(
xmin = .1,
xmax = 1,
x = xmin + ((xmax - xmin) / 2),
y = ymin + ((ymax - ymin) / 2)
)
overlap <- with(
plot_data,
tibble(x = median(x), y = median(c(max(ymin), min(ymax))), pct = sum(pct) - 1)
)
ggplot(plot_data, aes(x, y)) +
geom_rect(
aes(xmin = .1, xmax = 1, ymin = ymin, ymax = ymax, fill = set),
color = "black",
alpha = .5,
position = position_dodge(width = .2)
) +
geom_text(
aes(label = percent(pct), group = set),
position = position_dodge(width = .2),
size = 16 / .pt,
fontface = "bold"
) +
geom_text(
data = overlap,
aes(label = percent(pct)),
size = 16 / .pt,
fontface = "bold"
) +
scale_fill_manual(values = c("#E41A1C", "#377EB8")) +
scale_x_continuous(limits = 0:1) +
coord_polar(theta = "y") +
theme_void()
```

Answered By - zephryl Answer Checked By - Marie Seifert (PHPFixing Admin)

## 0 Comments:

## Post a Comment

Note: Only a member of this blog may post a comment.