如何在R中使用ggplot2绘制相似的图? [英] how to make similar plots using ggplot2 in R?

查看:85
本文介绍了如何在R中使用ggplot2绘制相似的图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下数据集,我想为每个变量作图,并对每个第十个观察值进行不同的着色.我可以用R基来做.我想学习如何使用ggplot2吗?

For the following data set, I would like to plot for each variable and color each 10th observations differently. I can do it using the R base. I want to learn how to do it using the ggplot2?

dput(mydata)

structure(list(beta0_C1 = c(5.90722120539152, 5.89025566996191, 
5.88591520258904, 5.86911167649919, 5.93772460437405, 5.92985640353594, 
5.89150365752453, 5.99046628686212, 5.91548006074821, 5.91571832976612, 
5.88437484241154, 5.92092513223357, 5.98978050584774, 5.91152552752889, 
5.91235823292462, 5.87961960044268, 5.84048698713552, 5.85484766204026, 
5.94002829943904, 5.8844367778216, 5.90201348639369, 5.91220967575205, 
5.90010933186624, 5.9187781795242, 5.85506764080697, 5.90103565341373, 
5.88527143992961, 5.90218851192948, 5.90118162849608, 5.93147588185271
), beta1_C1 = c(0.389473200070741, 0.386495525456602, 0.401277295631578, 
0.400952009358693, 0.376727640651344, 0.380365338054745, 0.393444927288697, 
0.351041363714069, 0.393194356572458, 0.393448101768608, 0.398884551136789, 
0.399458966787235, 0.357436746423815, 0.393782316102096, 0.387154169967002, 
0.400838223362088, 0.404272252119662, 0.407427775176583, 0.379704250022161, 
0.388842664781329, 0.382202010301184, 0.401354531881688, 0.391184010553641, 
0.390280828053183, 0.402135923802544, 0.384344141458216, 0.405409447440106, 
0.391719398951194, 0.398025625260563, 0.361822915989445), beta2_C1 = c(-0.0214886993465096, 
-0.020723519439664, -0.0224612526333316, -0.0218187226687474, 
-0.0200324040063121, -0.0208421378685671, -0.0218756660346625, 
-0.0182499666400075, -0.0222765863213226, -0.022242845613047, 
-0.0222033291270054, -0.0231570312767931, -0.0189429585905841, 
-0.0221017468740293, -0.0209327798783444, -0.022409049257, -0.021698958175968, 
-0.0225601087054418, -0.020928341508875, -0.0214668830626075, 
-0.0205872002686706, -0.0233768022702472, -0.021755967293395, 
-0.0218442145294776, -0.0222514480818199, -0.0212195394692002, 
-0.0232109717283908, -0.0214814999754984, -0.0225124468437127, 
-0.0187033387452614), beta0_C2 = c(6.50537199380546, 6.43626630601952, 
6.44460360859128, 6.44788878017196, 6.49678676895955, 6.48474789770674, 
6.5459727637079, 6.37593806532098, 6.39492158034295, 6.44497331914909, 
6.3888816168562, 6.49660574813212, 6.45922901141938, 6.40080765767324, 
6.37918638201668, 6.49354321098856, 6.47057962920788, 6.55699741431025, 
6.56617313133218, 6.54271932949381, 6.44608000042182, 6.45333777656105, 
6.67458442747556, 6.48420983182487, 6.59919337271637, 6.46645685814734, 
6.46171236062657, 6.52625058117578, 6.51177045919728, 6.49897849935538
), beta1_C2 = c(-0.370455826326915, -0.338852275811034, -0.340671118342601, 
-0.339888681238265, -0.36934391822867, -0.357194169746804, -0.415966150286963, 
-0.349051278947586, -0.358209379291251, -0.371785518417424, -0.349725822847608, 
-0.368220986471866, -0.327425879655177, -0.336993142255552, -0.328859493371605, 
-0.347764105375218, -0.329761787134926, -0.37935820670654, -0.400211161919931, 
-0.408699321227288, -0.357590345066542, -0.376548827126353, -0.44672514669147, 
-0.353840422053319, -0.421912098450693, -0.371491468175642, -0.354864346664247, 
-0.39139246919467, -0.379006372881295, -0.372492936183765), beta2_C2 = c(0.039728365796445, 
0.0368393936404604, 0.0375019672690036, 0.0375019364609944, 0.0403444583999664, 
0.0378627636833333, 0.0446717245407897, 0.0377538641609231, 0.039662572899695, 
0.0408055348533836, 0.0386737104573771, 0.0397794302159846, 0.0352739962796708, 
0.0376756204317514, 0.0370614500426065, 0.0374731659969108, 0.035366001926832, 
0.0397165124506166, 0.0414814320660011, 0.0431083057931525, 0.0388672853038453, 
0.0403590048367136, 0.0461540000449275, 0.0379315295246309, 0.0440664419193363, 
0.0404593732981113, 0.0387390924290065, 0.0417832766420881, 0.0409598003097311, 
0.0394548129358408)), row.names = c(NA, 30L), class = "data.frame")

R基本代码

 par(mfrow=c(3,3))
col.set=c("green","blue","purple","deeppink","darkorchid","darkmagenta","black","khaki")
loop.vector=1:ncol(mydata)
for(b in loop.vector) {
  x.beta<-mydata[,b]
  beta <- substr(sub("^beta", '', names(mydata)[b]),1,1)
  Cn <- substr(sub("^beta", '',names(mydata)[b]),3,4)
  plot(x.beta, type = "n", ylab="", xlab="",
       main=bquote(beta[.(beta)]~.(Cn)), 
       cex.main=1) 
  mtext("plots of betas",line=-1.5, cex=1, outer = TRUE)
  for (k in 1:3){
    beta_k=mydata[((nrow(mydata)/3)*k-((nrow(mydata)/3)-1)):
                           ((nrow(mydata)/3)*k),b]
    lines(((nrow(mydata)/3)*k-((nrow(mydata)/3)-1)):
            ((nrow(mydata)/3)*k),beta_k,
          col=col.set[k])
    legend("topleft", bg="transparent",inset=0.05,legend=paste0("chain_",1:3),
           col=col.set, lty=1,box.lty=0, cex=0.8)
  }
}

我要为每个地块使用相同的主标题,并为所有地块使用一个主要标题.

I want the same main title for each plot and one main titile for all plots.

我如何使用ggplot2软件包来做到这一点?

how can I do it using the ggplot2 package?

推荐答案

ggplot2最适合包含x,y,color等变量的长数据框.这将形成一个长数据框:

ggplot2 works best with a long data frame containing variables for x, y, color, etc. This makes a long data frame:

library(tidyverse)
long_data = my_data %>%
  mutate(n=1:nrow(my_data), chain=paste0('Chain ', rep(1:3, each=nrow(my_data)/3))) %>% 
  pivot_longer(cols=c(-n, -chain)) %>% 
  mutate(name=str_replace(name, '(\\d)_', '[\\1]~~'))

这就是情节.

ggplot(long_data, aes(n, value, color=chain)) +
  geom_line() +
  facet_wrap(~name, scales='free_y', ncol=3, dir='v',
             labeller=label_parsed) +
  scale_color_manual('', values=c('Chain 1'='green', 'Chain 2'='blue', 'Chain 3'='purple')) +
  theme_minimal() 

这篇关于如何在R中使用ggplot2绘制相似的图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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