在R中创建非标准图形。

huangapple go评论53阅读模式
英文:

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.

在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.

在R中创建非标准图形。

答案1

得分: 5

以下是翻译好的内容:

这里有一种方法。如果数据类似下面的虚假数据,使用 geom_bargeom_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())

在R中创建非标准图形。

<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 &lt;- c(
  A = &quot;white&quot;,
  B = &quot;black&quot;,
  C = &quot;black&quot;,
  D = &quot;white&quot;
)
fill_clrs &lt;- c(
  A = &quot;#1f3560&quot;,
  B = &quot;#bfbfbf&quot;,
  C = &quot;#f2f3f3&quot;,
  D = &quot;#ff0000&quot;
)

library(ggplot2)

ggplot(df1, aes(Variants, fill = Ratio)) +
  geom_bar(position = &quot;fill&quot;) +
  geom_text(stat = &quot;count&quot;, 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 = &quot;reverse&quot;) +
  scale_fill_manual(values = fill_clrs) +
  scale_color_manual(values = color_clrs) +
  coord_flip() +
  theme_classic() +
  theme(legend.position = &quot;top&quot;,
        axis.ticks.x = element_blank(),
        axis.text.x = element_blank())

在R中创建非标准图形。<!-- -->

<sup>Created on 2023-06-13 with reprex v2.0.2</sup>


Test data

set.seed(2023)
n &lt;- 1000
df1 &lt;- data.frame(
  Ratio = sample(LETTERS[1:4], n, TRUE),
  Variants = sample(letters[17:26], n, TRUE)
)
head(df1)
#&gt;   Ratio Variants
#&gt; 1     A        z
#&gt; 2     D        s
#&gt; 3     C        y
#&gt; 4     A        s
#&gt; 5     D        v
#&gt; 6     C        r

<sup>Created on 2023-06-13 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月13日 16:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定