如何创建多个变量的图分布? [英] How to create a plot distributions of multitple variables?

查看:62
本文介绍了如何创建多个变量的图分布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为包含多个变量的数据框创建清晰的密度/分布图?

How is it possible to create a clear density/distribution plot of a dataframe of many variables?

以下是输入内容:

structure(list(b1_previous = c(0.26981640312419, 0.302252978236613, 
    0.27519244423907, 0.278573602172958), b2_previous = c(0.165541492443112, 
    0.162543532408399, 0.150484069110868, 0.212810080358854), b3_previous = c(0.698096408083222, 
    0.625412783031095, 0.699099484936941, 0.610794910230257), b4_previous = c(0.156164414439798, 
    0.189265950612553, 0.151656203861282, 0.211930979296043), b5_previous = c(0.384820854982136, 
    0.364443743167243, 0.352744936715994, 0.397252245652394), b1_next = c(0.290892287578753, 
    0.279948606399405, 0.262591995672118, 0.327138300630022), b2_next = c(0.170072244074521, 
    0.190821283262141, 0.136632592108377, 0.185400160041476), b3_next = c(0.637122860008791, 
    0.595805110056691, 0.713976579846045, 0.594306130039334), b4_next = c(0.154789410213351, 
    0.185512865305938, 0.136271935262096, 0.18347290001916), b5_next = c(0.359935532588727, 
    0.391256325582968, 0.352913994612688, 0.312475345723399), before = c(2L, 
    1L, 2L, 1L), after = c(2L, 1L, 2L, 1L)), row.names = c(NA, -4L
    ), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000000003d1ef0>)

推荐答案

您可以在ggplot2中使用 facet_wrap 在同一图形上绘制不同的分布:

You can use facet_wrap in ggplot2 to plot different distributions on the same figure:

a = structure(list(b1_previous = c(0.26981640312419, 0.302252978236613, 
    0.27519244423907, 0.278573602172958), b2_previous = c(0.165541492443112, 
    0.162543532408399, 0.150484069110868, 0.212810080358854), b3_previous = c(0.698096408083222, 
    0.625412783031095, 0.699099484936941, 0.610794910230257), b4_previous = c(0.156164414439798, 
    0.189265950612553, 0.151656203861282, 0.211930979296043), b5_previous = c(0.384820854982136, 
    0.364443743167243, 0.352744936715994, 0.397252245652394), b1_next = c(0.290892287578753, 
    0.279948606399405, 0.262591995672118, 0.327138300630022), b2_next = c(0.170072244074521, 
    0.190821283262141, 0.136632592108377, 0.185400160041476), b3_next = c(0.637122860008791, 
    0.595805110056691, 0.713976579846045, 0.594306130039334), b4_next = c(0.154789410213351, 
    0.185512865305938, 0.136271935262096, 0.18347290001916), b5_next = c(0.359935532588727, 
    0.391256325582968, 0.352913994612688, 0.312475345723399), before = c(2L, 
    1L, 2L, 1L), after = c(2L, 1L, 2L, 1L)), row.names = c(NA, -4L
    ), class = c("data.table", "data.frame"))

a %>% 
   dplyr::select(-before, -after) %>% 
   tidyr::pivot_longer(everything()) %>% 
   ggplot(aes(y=value)) + geom_density() + coord_flip() + facet_wrap(~name)

或者,如果您希望它们在同一面板上:

Or, if you want them on the same panel:

a %>% 
  dplyr::select(-before, -after) %>% 
  tidyr::pivot_longer(everything()) %>% 
  ggplot(aes(y=value, fill=name)) + geom_density(alpha=0.3) + coord_flip()

这篇关于如何创建多个变量的图分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆