根据数值列调整冲积图中的线宽

Posted

技术标签:

【中文标题】根据数值列调整冲积图中的线宽【英文标题】:Adjust the width of lines in alluvial chart based on numeric column 【发布时间】:2021-05-16 22:56:26 【问题描述】:

我创建了下面的冲积图但是没有考虑权重,不知道为什么;所以所有的线都有相同的宽度。怎么调整?

library(ggalluvial)
library(magrittr)

alpha <- .4

Data<-c("M","M","S","S","G","G")
Report<-c("C","O","C","S","C","O")
Weight<-c(1,1,5,2,2,1)

dr<-data.frame(Data,Report,Weight)

plot_01 <- dr %>%
    ggplot(aes(axis1 = Data, axis2 = Report)) +
    geom_alluvium(aes(fill = Report, color = Report, weight = Weight),
                      width = 1/12, alpha = alpha, knot.pos = 0.4) +
    geom_stratum(width = 1/6, color = "grey") +
    geom_label(stat = "stratum", aes(label = Data)) +
    geom_label(stat = "stratum", aes(label = Report)) +
    scale_x_continuous(breaks = 1:2, labels = c("Data", "Report")) +
    scale_fill_viridis_d() +
    scale_color_viridis_d() +
    theme_minimal() +
    theme(legend.position = "none",
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 12, face = "bold"))
plot_01

【问题讨论】:

【参考方案1】:

?geom_alluvium - 我看不到重量美学。你的意思可能是y

library(ggalluvial)
#> Loading required package: ggplot2
library(magrittr)

alpha <- .4

Data<-c("M","M","S","S","G","G")
Report<-c("C","O","C","S","C","O")
Weight<-c(1,1,5,2,2,1)

dr<-data.frame(Data,Report,Weight)

dr %>%
  ggplot(aes(axis1 = Data, axis2 = Report, y = Weight)) +
  geom_alluvium(aes(fill = Report, color = Report),
                alpha = alpha, knot.pos = 0.4) +
  geom_stratum(width = 1/6, color = "grey") +
  geom_label(stat = "stratum", aes(label = Data)) +
  geom_label(stat = "stratum", aes(label = Report)) +
  scale_x_continuous(breaks = 1:2, labels = c("Data", "Report"),
                     expand = c(0,0)) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(size = 12, face = "bold"))
#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.

#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.

#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.

#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.
#> Warning: Removed 2 rows containing missing values (geom_label).
#> Warning: Removed 3 rows containing missing values (geom_label).

由reprex package (v1.0.0) 于 2021-02-13 创建

【讨论】:

以上是关于根据数值列调整冲积图中的线宽的主要内容,如果未能解决你的问题,请参考以下文章

CAD图纸中的线宽显示不出来要怎么办

控制 seaborn 热图中的单个线宽

增加 matplotlib 中图例线的线宽

ad18自动布线怎么全是细线

CGContext 给出了意想不到的线宽

在缩放 UIScrollView 子视图时保持恒定的线宽