I'm studying PCA method with the package PCAmixdata because I have a dataset with numerical and categorical variable. This is my example code:
library(dplyr)
library(PCAmixdata)
data <- starwars
db_quali <- as.data.frame(starwars[,4:6])
db_quanti <- as.data.frame(starwars[,2:3])
pca_table <- PCAmix(X.quanti = db_quanti,
X.quali = db_quali,
rename.level=TRUE,
graph = TRUE)
Gender <- factor(data$gender)
par(xpd=TRUE,mar=rep(8,4))
plot(pca_table ,choice="ind",label=FALSE,
posleg=xy.coords(2,-10), main="Observations", coloring.ind = Gender)
I know that the function ggplot
can be used only with data.frame
and at the moment I have a list
. Is there a way to convert pca_table
into a data.frame
to use the function ggplot
?
I want to do this because in my original dataset I have many classes and with the function plot I have a limited set of colors, so a color could correspond to more than one class.
Hi there,
You will see that
library(dplyr)
library(PCAmixdata)
data <- starwars
db_quali <- as.data.frame(starwars[,4:6])
db_quanti <- as.data.frame(starwars[,2:3])
pca_table <- PCAmix(X.quanti = db_quanti,
X.quali = db_quali,
rename.level=TRUE,
graph = TRUE)
Gender <- factor(data$gender)
par(xpd=TRUE,mar=rep(8,4))
plot(pca_table ,choice="ind",label=FALSE,
posleg=xy.coords(2,-10), main="Observations", coloring.ind = Gender)
#extract specific list
df_standard_scores <- pca_table$scores.stand
str(df_standard_scores)
#can convert it into dataframe like so
df_standard_scores2 <- pca_table$scores.stand %>% as.data.frame()
str(df_standard_scores2)
You will see that pca_table consists of 25 lists. You can call up the specific list you need and convert it into a data.frame. You can see how I did that with the creation of df_standard_scores2
. Let me know if this helps you.
ok this work, now I have a dataframe and I can use ggplot. I set the function in this way:
ggplot(df_standard_scores2, aes(x = `dim 1`, y = `dim 2`, color = Gender))
but the result is this graph:
what is wrong? why my graph is empty?
Because you didn't say what to plot, you need to add the
geom_point()!
ggplot(df_standard_scores2, aes(x = `dim 1`, y = `dim 2`, color = Gender)) +
geom_point()
1 Like
You can run the code I provided at the bottom. You have to realise that df_standard_scores2 also doesn't contain Gender so you will have to add that to the data.frame. The example below you will see will run the plot but not with colour added at the moment as you will have to provide that variable too.
library(tidyverse)
library(PCAmixdata)
data <- starwars
db_quali <- as.data.frame(starwars[,4:6])
db_quanti <- as.data.frame(starwars[,2:3])
pca_table <- PCAmix(X.quanti = db_quanti,
X.quali = db_quali,
rename.level=TRUE,
graph = TRUE)
Gender <- factor(data$gender)
# par(xpd=TRUE,mar=rep(8,4))
# plot(pca_table ,choice="ind",label=FALSE,
# posleg=xy.coords(2,-10), main="Observations", coloring.ind = Gender)
#
#extract specific list
df_standard_scores <- pca_table$scores.stand
str(df_standard_scores)
#> num [1:87, 1:5] -1.11 0.4 0.407 1.051 -1.119 ...
#> - attr(*, "dimnames")=List of 2
#> ..$ : chr [1:87] "1" "2" "3" "4" ...
#> ..$ : chr [1:5] "dim 1" "dim 2" "dim 3" "dim 4" ...
#can convert it into dataframe like so
df_standard_scores2 <- pca_table$scores.stand %>% as.data.frame()
str(df_standard_scores2)
#> 'data.frame': 87 obs. of 5 variables:
#> $ dim 1: num -1.11 0.4 0.407 1.051 -1.119 ...
#> $ dim 2: num -0.395 0.338 1.345 -0.347 0.147 ...
#> $ dim 3: num 0.3964 -1.1875 -0.3501 -0.8861 -0.0699 ...
#> $ dim 4: num 0.304 0.73 -1.529 1.114 -0.358 ...
#> $ dim 5: num 1.0755 -0.0775 0.626 -0.08 -0.4614 ...
df_standard_scores2
#> dim 1 dim 2 dim 3 dim 4 dim 5
#> 1 -1.11025074 -0.395259716 0.396406309 0.304342350 1.0754590266
#> 2 0.40008447 0.337738861 -1.187526378 0.729533618 -0.0775374874
#> 3 0.40650579 1.344758169 -0.350139954 -1.529167399 0.6259859896
#> 4 1.05118517 -0.346713502 -0.886110135 1.114033239 -0.0800464540
#> 5 -1.11931790 0.146932254 -0.069935947 -0.358191916 -0.4614056383
#> 6 -1.03049513 -0.352923785 0.328589540 0.005262297 -0.0833328854
#> 7 -1.06472426 -0.097876289 0.214977625 -0.046866631 -0.0272765874
#> 8 0.25135438 1.573297337 -0.460209334 -1.706794094 0.6494758104
#> 9 -1.02194945 -0.097980434 -0.271347842 -0.314045561 -0.9651605110
#> 10 -1.38819697 -1.036961609 0.731752839 1.152734858 6.4703842754
#> 11 -1.08940798 -0.513233827 0.431537897 0.394037742 1.0628837281
#> 12 -1.08682581 -0.506886418 0.439832460 0.356032453 1.0402288273
#> 13 -0.70246898 0.390714637 1.681682305 1.479518454 -0.3362047589
#> 14 -1.09919502 -0.209442711 0.089506445 -0.022277028 0.0707340522
#> 15 0.66779049 -0.026872554 -0.004072481 -0.276821287 0.3129854523
#> 16 1.69345752 -3.894437899 2.318180023 -1.211419292 -2.9335291671
#> 17 -0.97369718 -0.199981708 0.274227027 0.046363084 0.5214677807
#> 18 -1.05918596 -0.359087247 0.350338906 0.201366896 0.5055721922
#> 19 -0.37937681 0.933531309 -0.659084778 -0.874041920 0.0077384860
#> 20 0.96261359 0.251950161 -1.827916335 1.261169505 0.0009390464
#> 21 -1.05201410 -0.198553759 -0.197624402 -0.138258507 -0.3846066332
#> 22 1.04028776 0.112019268 0.098616808 -0.791410692 0.5035281911
#> 23 0.89228689 0.202082788 -0.058933809 -0.629710203 0.3829723873
#> 24 -0.91657355 -0.070819069 -0.466854285 -0.411430686 -1.3386016962
#> 25 -0.33972150 -0.121401180 0.185573667 -0.052673257 0.0690138895
#> 26 1.29198542 -0.736250599 0.521661537 -0.466216137 -0.2916302834
#> 27 -1.13863872 -0.158816498 0.343290130 0.042326162 1.0586729559
#> 28 -1.09726457 -0.196750381 0.093700382 -0.060632280 0.0544185804
#> 29 -1.04951260 0.633704750 -0.367854885 -0.613181881 -0.4559516686
#> 30 1.19862522 -0.217011351 0.244922789 -0.262108004 0.4870627125
#> 31 -1.05477194 -0.415938369 0.356510660 0.283319872 0.5233504317
#> 32 0.98782596 0.342341797 -0.004097627 -0.854626208 0.6076729873
#> 33 -1.10291604 -0.411476810 0.409776230 0.285925527 1.0548055355
#> 34 1.28962868 -0.753789108 0.483379122 -0.263997018 -0.0786821601
#> 35 1.33399967 -0.981649700 0.541179444 -0.009099158 0.0092838848
#> 36 1.11110685 -0.593052441 0.281974138 -0.257623703 -0.0950769714
#> 37 -1.06184648 -0.360745956 0.345189068 0.222945900 0.5179012434
#> 38 0.01656894 0.738776855 -1.560844444 0.112049638 -0.7488114135
#> 39 1.14279341 0.160983220 0.239405764 -1.150712850 -0.1650176526
#> 40 -0.90139786 -0.137985162 -0.440529329 -0.383129458 -1.3597732372
#> 41 -1.06499586 -0.091805256 -0.218181732 -0.259852576 -0.3981151090
#> 42 0.94889270 0.238039404 -1.086346189 0.715160291 0.0380611089
#> 43 1.12094218 -0.116262715 -0.561066766 0.426806747 0.1928395056
#> 44 0.17762047 -0.134081596 0.244822691 -0.006214558 0.4464948773
#> 45 0.61225629 1.125991014 -1.443281852 -0.045750740 -0.1198693181
#> 46 0.95332318 0.680420488 -0.051823903 -1.068792776 0.5712073316
#> 47 1.24933432 -0.493686419 0.440403592 -0.632860341 -0.2456784499
#> 48 -0.23473738 -0.130999704 -0.202482215 -0.277964821 -0.7874393624
#> 49 0.50153145 0.012304609 -1.203790734 0.877014926 0.1012524665
#> 50 1.03672310 -0.197439691 0.087573123 -0.201130783 0.3611743547
#> 51 -0.87742238 -0.009744796 -0.442347938 -0.295687277 -1.0204429889
#> 52 -0.22914423 -0.160174524 -0.002618063 -0.055575733 -0.2841560236
#> 53 1.19057829 -0.531697736 -0.046335285 0.059909242 -0.1300236740
#> 54 1.10107392 -0.707679597 -0.805147691 1.483983589 -0.0597365679
#> 55 1.22884593 -0.407175275 0.313017146 -0.204262219 0.3514115596
#> 56 0.10527976 -0.360668026 0.343372566 0.191660541 0.4182148888
#> 57 -0.90497927 -0.133459787 -0.447192691 -0.367433151 -1.3475118906
#> 58 -1.08896999 0.029188223 -0.016632435 -0.334337882 -0.5143747319
#> 59 -1.06184648 -0.360745956 0.345189068 0.222945900 0.5179012434
#> 60 0.86663436 0.114553466 -0.676838690 0.293805657 0.1011835240
#> 61 -1.04320347 -0.060907346 -0.190658041 -0.113099568 -0.5465745393
#> 62 -1.05050192 -0.024910299 -0.203171461 -0.134010130 -0.5387625843
#> 63 -1.08019068 -0.024683705 -0.002000761 -0.288299961 -0.5169908179
#> 64 -0.85450400 -0.185782642 -0.198438834 0.066990851 0.2181446054
#> 65 -1.02028375 -0.209652930 -0.483758183 -0.351549654 -1.5110956641
#> 66 -1.03765426 -0.129018855 -0.513741058 -0.391360362 -1.4892701016
#> 67 0.90810208 1.358950725 -5.047295383 4.113202380 -0.3227618435
#> 68 0.42815536 -0.054701037 -0.694205183 0.522377547 -0.2284872055
#> 69 1.28373001 -0.710886032 0.387887379 0.128167779 0.4435186092
#> 70 1.27054014 -0.616751138 0.366430351 0.032925302 0.4389824673
#> 71 -0.84898048 -0.141800146 0.004203970 0.145326315 0.6747951084
#> 72 0.67175147 3.771861377 1.818619280 0.342434959 -0.3230354751
#> 73 0.98194744 3.282133625 -0.754057760 -5.203827465 1.3688571258
#> 74 0.89300637 2.366100658 2.234156279 1.514127259 -0.4582067915
#> 75 1.31478518 -0.640004983 0.414806618 0.030528797 0.4399195392
#> 76 1.17715451 -0.107945000 0.182214504 -0.270214321 0.5921779813
#> 77 1.57507797 -2.303908318 1.230088712 1.448084425 -0.2608070934
#> 78 -0.80893326 -0.624788463 0.232101146 0.421408568 -0.1590726519
#> 79 -1.06354135 -0.152803353 0.024714104 -0.149713722 -0.5053025888
#> 80 1.08107815 0.031110753 -0.640239634 0.436364148 0.2913235094
#> 81 1.25473620 -0.544312519 0.339114756 -0.001470729 0.4594320010
#> 82 -0.88601703 -0.128108562 -0.628571415 -0.540279524 -1.9775549551
#> 83 -0.93204158 -0.149963863 0.220639821 -0.109279639 -0.0757344259
#> 84 -1.06992105 -0.087700560 0.015114686 -0.234446960 -0.5200509926
#> 85 1.20062908 -0.167196126 0.229955744 -0.333207814 0.5149127685
#> 86 0.23052115 4.062091043 4.542524050 3.361381613 -1.4685262626
#> 87 -1.10473333 0.051768247 -0.045854837 -0.270510556 -0.4621147274
ggplot() +
geom_point(data = df_standard_scores2, aes(x = `dim 1`, y = `dim 2`))
Created on 2022-03-03 by the reprex package (v2.0.1)
system
Closed
March 10, 2022, 10:08am
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.