I'm not very experienced with R visualizations. Maybe somebody can give some hints on creating a plot similar to the image below with R (what functions, and package to use). In the image is a pseudo-example, using actual data would be more such "bars" in the plot. Here we have a column called "Ratio" with values A B C D and another column called "Variants" with values x, y, z, q, etc.
Asked
Active
Viewed 62 times
0
-
2Use ggplot with `geom_bar` and `geom_text` with `position = position_fill()`. If you need a more specific answer than that, then we will need a more specific question, including data, as @zx8754 suggests – Allan Cameron Jun 13 '23 at 08:36
1 Answers
5
Here is a way. If the data resembles the fake data below, use geom_bar
and geom_text
to display the percent bars with the counts in them. Then it's just a matter of embellishing the plot as in the posted picture.
color_clrs <- c(
A = "white",
B = "black",
C = "black",
D = "white"
)
fill_clrs <- c(
A = "#1f3560",
B = "#bfbfbf",
C = "#f2f3f3",
D = "#ff0000"
)
library(ggplot2)
ggplot(df1, aes(Variants, fill = Ratio)) +
geom_bar(position = "fill") +
geom_text(stat = "count", aes(label = after_stat(count), color = Ratio),
position = position_fill(vjust = 0.5), show.legend = FALSE) +
scale_x_discrete(limit = rev) +
scale_y_continuous(trans = "reverse") +
scale_fill_manual(values = fill_clrs) +
scale_color_manual(values = color_clrs) +
coord_flip() +
theme_classic() +
theme(legend.position = "top",
axis.ticks.x = element_blank(),
axis.text.x = element_blank())
Created on 2023-06-13 with reprex v2.0.2
Test data
set.seed(2023)
n <- 1000
df1 <- data.frame(
Ratio = sample(LETTERS[1:4], n, TRUE),
Variants = sample(letters[17:26], n, TRUE)
)
head(df1)
#> Ratio Variants
#> 1 A z
#> 2 D s
#> 3 C y
#> 4 A s
#> 5 D v
#> 6 C r
Created on 2023-06-13 with reprex v2.0.2

Rui Barradas
- 70,273
- 8
- 34
- 66