英文:
Creating non standard plot in R
问题
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.
英文:
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.
答案1
得分: 5
以下是翻译好的内容:
这里有一种方法。如果数据类似下面的虚假数据,使用 geom_bar
和 geom_text
来显示带有计数的百分比条。然后,只需要像发布的图片中那样装饰图。
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())
<sup>创建于2023-06-13,使用 reprex v2.0.2 </sup>
测试数据
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
<sup>创建于2023-06-13,使用 reprex v2.0.2 </sup>
英文:
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())
<!-- -->
<sup>Created on 2023-06-13 with reprex v2.0.2</sup>
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
<sup>Created on 2023-06-13 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论