R语言 用石膏显示标签

z0qdvdin  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(111)

我试着给ggalluvial图添加标签。我只有轴1和2,当我试着显示标签时,它显示了两次。
下面是我使用dput的数据集:

structure(list(Mef_R21 = c("1S2TMD", "1S2TMD", "1-ST2S", "1-ST2S", 
"1STD2A", "1STD2A", "1STD2A", "1-STHR", "1-STHR", "1-STHR", "1STI2D", 
"1STI2D", "1STI2D", "1-STL ", "1-STL ", "1-STL ", "1-STL ", "1-STMG"
), Mef_R22 = c("1ERPRO", "PREMIE", "1ERPRO", "1-ST2S", "1ERPRO", 
"1STD2A", "PREMIE", "1STI2D", "1-STMG", "PREMIE", "1ERPRO", "1STI2D", 
"1-STMG", "1ERPRO", "1-STL ", "1-STMG", "PREMIE", "1-STMG"), 
    statut = c("changement filière", "changement filière", "changement filière", 
    "redoublement", "changement filière", "redoublement", "changement filière", 
    "changement filière", "changement filière", "changement filière", 
    "changement filière", "redoublement", "changement filière", 
    "changement filière", "redoublement", "changement filière", 
    "changement filière", "redoublement"), Effectifs = c(0.0476190476190476, 
    0.0476190476190476, 0.0066815144766147, 0.0144766146993318, 
    0.0161290322580645, 0.0161290322580645, 0.00537634408602151, 
    0.00689655172413793, 0.00689655172413793, 0.0206896551724138, 
    0.00579710144927536, 0.0104347826086957, 0.00811594202898551, 
    0.00865800865800866, 0.0216450216450216, 0.0173160173160173, 
    0.0173160173160173, 0.00921555405709148), freq = c(4.76190476190476, 
    4.76190476190476, 0.66815144766147, 1.44766146993318, 1.61290322580645, 
    1.61290322580645, 0.537634408602151, 0.689655172413793, 0.689655172413793, 
    2.06896551724138, 0.579710144927536, 1.04347826086957, 0.811594202898551, 
    0.865800865800866, 2.16450216450216, 1.73160173160173, 1.73160173160173, 
    0.921555405709148)), row.names = c(NA, -18L), class = "data.frame")

下面是我的代码:

ggplot(bdd_1, aes(axis1 = Mef_R21, axis2 = Mef_R22, y=freq, label = scales::percent(round(Effectifs, digits = 2))))+
  geom_alluvium(color= "black",aes(fill=statut)) +
  geom_stratum() +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)),size = 3,discern=FALSE)+
  geom_text(stat = "flow", nudge_x = 0.2, size = 2.5) +
  theme(legend.position = "bottom")

我的结果是

我想删除右边的标签百分比,在图表之外(在图片上的红圈内)。有没有可能这样做?提前感谢。

nuypyhwy

nuypyhwy1#

如果要删除两个百分比列(“from”和“to”),可以删除第二个geom_text()语句。如果只想删除右侧的百分比列(“to”),则可以使用以下方法覆盖图生成的数据点,从而有效地将流标签的“to”列中的值设置为“"。

## Create a base plot that generates the flow label
g <- ggplot(bdd_1, aes(axis1 = Mef_R21, axis2 = Mef_R22, y=freq, label = scales::percent(round(Effectifs, digits = 2)))) +
   geom_text(stat = "flow", nudge_x = 0.2, size = 2.5) 

## Isolate the data points that are generated for the plot
dat <- ggplot_build(g)$data[[1]] 

## Set the label values to "" where flow is equal to "to"
dat$label[dat$flow == "to"] <- ""

## Run your code for the plot including label = dat$label in the second geom_text().
ggplot(bdd_1, aes(axis1 = Mef_R21, axis2 = Mef_R22, y=freq, label = scales::percent(round(Effectifs, digits = 2))))+
    geom_alluvium(color= "black",aes(fill=statut)) +
    geom_stratum() +
    geom_text(stat = "stratum", aes(label = after_stat(stratum)),size = 3,discern=FALSE)+
    geom_text(stat = "flow",label = dat$label, nudge_x = 0.2, size = 2.5) +
    theme(legend.position = "bottom")

相关问题