Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

r - Convert to tidy format and Plot four density plots in the same plot

Suppose I have data where x, y, z are three different data generating methods, and the columns refer to beta_0, beta_1, beta_2, and beta_3. I want to convert this to tidy format and then plot the density of beta_0 in one plot (with 3 curves), ..., the density of beta_3 in one plot (with 3 curves), all four plots in one output plot (so in a 4x4 grid or 4x1 grid, whatever, possibly using facet_wrap?

My question is: what's the tidy format of this data, if it can be put in tidy format, and then the code to generate the four densities (with each plot having three density curves) using ggplot2?

Toy data (the "real" data has 200 observations for each method, not just 6):

x=matrix(c(4.790584, 2.654499, 1.3987716, 3.504921,
           4.126791, 2.280143, 1.1348528, 3.084783,
           4.555479, 2.226738, 1.7021107, 3.500337,
           4.954719, 2.592864, 1.2658565, 4.213174,
           3.608878, 2.059524, 0.5363011, 2.993644,
           4.399778, 2.001757, 1.0602126, 3.377298), nrow=6, ncol=4, byrow=TRUE)
y=matrix(c(6.850924, 3.694619, 2.549025, 6.045664,
7.740951, 4.504154, 2.513180, 6.255492,
5.026915, 2.540771, 1.726427, 4.743082,
4.835503, 2.842332, 1.666898, 4.308631,
8.186425, 5.331871, 2.919130, 6.724499,
4.641722, 2.646673, 1.486447, 3.931251), nrow=6, ncol=4, byrow=TRUE)
z=matrix(c(4.651634, 2.611540, 1.5819549, 4.107449,
3.716444, 2.097695, 1.0136387, 3.051602,
4.184544, 1.986585, 1.0731731, 3.442379,
6.309591, 2.720760, 2.1551536, 4.572733,
6.614165, 3.253548, 2.6207540, 5.157300,
4.408838, 1.920230, 0.9850924, 3.320052), nrow=6, ncol=4, byrow=TRUE)
tidy=cbind(x, y, z)
question from:https://stackoverflow.com/questions/65944385/convert-to-tidy-format-and-plot-four-density-plots-in-the-same-plot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One option is to use as.data.frame.table and map_dfr to convert the matrices into a long format "tidy" dataframe.

The first step is to give the matrices meaningful names. Several ways to do this, but one easy option is provideDimnames. Then feed the named matrices into map_dfr.

library(tidyverse)

# name the mats
mats_named <- list(x,y,z) %>%
  purrr::set_names(c('x','y','z')) %>%
  map(function(x) provideDimnames(x, base = list('row','beta'), sep= '_'))

#------
$x
          beta   beta_1    beta_2   beta_3
row   4.790584 2.654499 1.3987716 3.504921
row_1 4.126791 2.280143 1.1348528 3.084783
row_2 4.555479 2.226738 1.7021107 3.500337
row_3 4.954719 2.592864 1.2658565 4.213174
row_4 3.608878 2.059524 0.5363011 2.993644
row_5 4.399778 2.001757 1.0602126 3.377298

$y
          beta   beta_1   beta_2   beta_3
row   6.850924 3.694619 2.549025 6.045664
row_1 7.740951 4.504154 2.513180 6.255492
...

Convert the matrices into a long format df and make the figure.

mats_named %>%
  map_dfr(as.data.frame.table, .id = 'method') %>%
  ggplot() +
  geom_density(aes(Freq, fill = method), alpha = 0.5) +
  facet_wrap(~Var2)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...