【问题标题】:Create several exposure combined density/ distribution plot in R在 R 中创建多个曝光组合密度/分布图
【发布时间】:2022-12-03 18:41:18
【问题描述】:
set.seed(42)
n=1000
db = data.frame(id=1:n,
                exp_1 = as.numeric(rnorm(n)),
                exp_2 = as.numeric(rnorm(n)),
                exp_3 = as.numeric(rnorm(n)), 
                exp_4=as.numeric(rnorm(n)))
label(db$exp_1)="Myx"
label(db$exp_2)="ff3"
label(db$exp_3)="poison-untitled"
label(db$exp_4)="NH3"

我想创建一个组合密度图 - 带有引用变量标签的图例 一个类似的情节

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    首先,您可以使用 tidyr 中的 pivot_longer 将数据更改为更长的格式。您提到的图表看起来像ggpubr,因此您可以使用ggdensityscale_fill_discrete来修改图例,如下所示:

    set.seed(42)
    n=1000
    db = data.frame(id=1:n,
                    exp_1 = as.numeric(rnorm(n)),
                    exp_2 = as.numeric(rnorm(n)),
                    exp_3 = as.numeric(rnorm(n)), 
                    exp_4=as.numeric(rnorm(n)))
    
    library(ggpubr)
    library(tidyr)
    library(dplyr)
    db %>%
      pivot_longer(cols = -id) %>%
      ggdensity(x = 'value', fill = 'name') +
      scale_fill_discrete('', labels = c('Myx', 'ff3', 'poison-untitled', 'NH3'))
    

    创建于 2022-12-03 reprex v2.0.2


    如果你想改变透明度,你可以在ggdensity中使用参数alpha。有关更多信息,请查看documentation

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 2020-08-29
      • 2021-07-17
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      相关资源
      最近更新 更多