[R] [External] Re: how to order variables on correlation plot

Richard M. Heiberger rmh @end|ng |rom temp|e@edu
Fri Nov 6 18:24:07 CET 2020


My guess is that the "%>% data.frame %>%" step turned something into a
character that you thought would be a factor.
See this example.  Remember that the stringsAFactors argument to
data.frame was recently changed.



> tmp <- data.frame(A=c("A","F","B","G","C"), B=1:5, CC=6:10)
> tmp
  A B CC
1 A 1  6
2 F 2  7
3 B 3  8
4 G 4  9
5 C 5 10
> sapply(tmp,class)
          A           B          CC
"character"   "integer"   "integer"
> tmp[order(tmp$A),]
  A B CC
1 A 1  6
3 B 3  8
5 C 5 10
2 F 2  7
4 G 4  9
> tmp$A <- factor(tmp$A, levels=unique(tmp$A))
> sapply(tmp,class)
        A         B        CC
 "factor" "integer" "integer"
> tmp[order(tmp$A),]
  A B CC
1 A 1  6
2 F 2  7
3 B 3  8
4 G 4  9
5 C 5 10
>

On Fri, Nov 6, 2020 at 9:18 AM Ana Marija <sokovic.anamarija using gmail.com> wrote:
>
> sorry forgot to attach the plot.
>
> On Fri, Nov 6, 2020 at 8:07 AM Ana Marija <sokovic.anamarija using gmail.com> wrote:
> >
> > Hello
> >
> > I have data like this:
> >
> > > head(my_data)
> >       subjects DIABDUR HBA1C ESRD SEX AGE PHENO          C1           C2
> > 1 fam0110_G110      38   9.4    1   2  51     2 -0.01144980  0.002661140
> > 2 fam0113_G113      30  12.5    1   2  40     2 -0.00502052 -0.000929061
> > 3 fam0114_G114      23   8.4    2   2  45     2 -0.00251578 -0.003450950
> > 4 fam0117_G117      37   9.0    2   2  46     2 -0.00704917 -0.000573325
> > 5 fam0119_G119      22   9.4    1   1  46     1  0.00263433  0.001002370
> > 6 fam0119_G120      NA    NA    1   1  71     1 -0.00354795 -0.002045940
> >             C3          C4          C5           C6           C7          C8
> > 1  0.006028150 -0.00176795 -0.00148375  0.004543550 -0.006272170 -0.00535077
> > 2 -0.000453402 -0.00192162  0.00416229  0.007868230 -0.001957670 -0.00473148
> > 3 -0.001680860 -0.00620438 -0.00235092  0.000672831 -0.000278318  0.00647337
> > 4  0.001436740  0.00155568 -0.00556147 -0.000386401 -0.006885350  0.00135539
> > 5 -0.007396920  0.00326229  0.00355575 -0.011149400  0.009156510  0.00120833
> > 6  0.004532050  0.00869862 -0.00113207  0.002244520 -0.002119220  0.00657587
> >            C9         C10
> > 1  0.00328111 -0.00113515
> > 2 -0.00495790  0.00320201
> > 3  0.00208591 -0.00874752
> > 4 -0.00967934  0.00607760
> > 5  0.00611030  0.00876190
> > 6 -0.00990661  0.00635349
> >
> > I am plotting it with:
> >
> > library(dplyr)
> > library(magrittr)
> > library(corrplot)
> > d=my_data %>% data.frame %>% set_rownames(.$subjects) %>% select(-subjects)
> > res <- cor(d, use = "complete.obs")
> > pdf("correlation.pdf")
> > corrplot(res, type = "upper", order = "hclust",
> >          tl.col = "black", tl.srt = 45)
> > dev.off()
> >
> > and I am getting the plot in attach. How to make it so that my
> > variables are shown on the plot in the order they are in my_data data
> > frame?
> >
> > Thanks
> > Ana
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list