0

I am trying a simple plot from PCA

I have plotted this data using ggplot2. But size is showing up on legend. How to stop that. Here is my code.

 > dput(colData)
    structure(list(Sample = c("S21_D2", "S21_D10", "S21_D10", "S21_D15", 
    "S131_D2", "S131_D5", "S131_D10", "S131_D15", "S131_D25", "S131_D30", 
    "S136_D1", "S136_D10", "S136_D15", "S136_D25", "S136_D30", "S251_D2", 
    "S251_D5", "S251_D7", "S251_D15", "S251_D25", "S251_D30", "S1101_D1", 
    "S1101_D2", "S1101_D5", "S1101_D7", "S1101_D10", "S1101_D25", 
    "S1101_D30", "S31_D1", "S31_D2", "S31_D5", "S31_D15", "S31_D25", 
    "S31_D30", "S42_D1", "S42_D2", "S42_D5", "S42_D10", "S42_D15", 
    "S102_D5", "S102_D10", "S102_D15", "S252_D1", "S252_D2", "S252_D5", 
    "S252_D7", "S252_D10", "S252_D15", "S252_D25", "S252_D30", "S22_D1", 
    "S22_D5", "S22_D30", "S41_D1", "S41_D1", "S41_D2", "S41_D15", 
    "S51_D1", "S51_D2", "S51_D5", "S51_D5", "S51_D10", "S51_D15", 
    "S51_D30", "S91_D1", "S91_D2", "S132_D5", "S132_D10", "S132_D25", 
    "S132_D30", "S141_D1", "S141_D2", "S141_D5", "S141_D10", "S143_D1", 
    "S143_D5", "S143_D10", "S143_D15", "S143_D30", "S144_D1", "S144_D2", 
    "S144_D5", "S231_D1", "S231_D2", "S231_D5", "S231_D10", "S231_D15", 
    "S32_D1", "S32_D2", "S32_D5", "S32_D10", "S32_D25", "S32_D30", 
    "S133_D2", "S133_D5", "S133_D15", "S135_D1", "S135_D2", "S142_D1", 
    "S142_D2", "S142_D5", "S142_D10", "S142_D15", "S142_D30"), X = c(-0.223651132267436, 
    -3.57720249744647, -0.290185838025935, 0.382052225732902, -3.91100802161457, 
    -1.62617311694135, -2.74006689481061, -0.622649528873042, -3.9743724035101, 
    -1.77718986052425, -3.07414459033738, -0.418759788748071, 1.25372310154224, 
    1.09496019044704, 0.238266819554441, -3.53808988553327, -2.7363997320598, 
    -8.54578780941788, -4.4762969807406, 1.27139855523864, 0.183947758951385, 
    -0.517583910554412, -0.304410190148505, 0.218109441197244, -0.372963654081768, 
    -0.081296926537109, 0.448045528871169, 0.483347388653263, 0.263389743279879, 
    0.157450088938842, -0.170451093462672, -2.10281204607714, -0.885262489177447, 
    0.0800816356183377, 0.263812820320778, -0.133717685439692, -0.90124437935087, 
    -1.97413329303089, -2.43395987641502, -2.95691653148526, -2.37583143620413, 
    -4.76550463023001, 4.6045098611166, 1.27921839607507, 1.58696436338456, 
    2.75925650511809, 1.24899094205102, 1.46628978694761, 2.39929525580681, 
    -0.456228852703781, 1.05469001487084, 0.265317735487105, 0.55193053063666, 
    1.08802397785104, 0.0548494759755792, -0.173808055618209, -0.807685068586555, 
    0.505558599256045, 1.39615230207476, 1.32618120778712, 0.827256508680758, 
    0.986336568944253, 0.991845184017345, 1.4666614191244, -0.121252078902438, 
    -0.40351865259122, 0.6022352297614, 0.439691367125281, 0.700684465441008, 
    0.671423500850393, 0.754191148626221, 0.0349318887352966, -0.290916374438812, 
    0.636628169009934, 2.08228437192422, 0.224339786691489, 0.090149640294263, 
    0.61160260631243, -0.876607799174393, 2.4993692212104, 1.84175154083908, 
    0.598531717562065, 2.3382687873108, 3.7393203669262, 0.566829766432481, 
    -1.09405844081872, 1.37301025894591, -0.672911618324755, 4.07232035002423, 
    0.264862068719949, 0.674347794898317, 0.0636446734452111, 1.20089589604514, 
    -1.1021774838764, -0.910745669805048, -0.194275879595093, 3.21724100667631, 
    0.0472027413888906, 1.68467275055506, 1.97000208834046, 0.808565762606384, 
    0.688421578389897, 0.644207286065043, 1.27271043277551), Y = c(-0.579839937573458, 
    -2.15512323663576, -0.273884152738988, 0.643371035787867, -1.76950626056927, 
    -0.565203517459582, -1.36949188989911, -0.65200897608952, -0.0318705882862642, 
    -0.111091129373697, -1.72315889030126, -0.614475888757652, 0.379718154981066, 
    0.563094720275958, -0.775055809582619, 0.158338048987629, 0.963661140233892, 
    9.33289391067559, 3.3230211813203, 1.15470304374527, -0.328730868081787, 
    -1.73354574905696, -1.27285400110782, 0.228398086556545, 0.0536641879360032, 
    0.0996760980284058, -0.805605947004645, -0.634464067640303, -0.57882552726797, 
    -0.221462780027063, -1.15119260535424, -4.47998849879228, -2.72724745278307, 
    -0.232274442555513, 1.58845062292781, -0.0514613201994664, -0.753853322460675, 
    0.243282792252653, 1.088775140269, -0.553068064304604, -0.606295097738268, 
    1.3883738408613, 1.45516263329313, 0.36599660675178, -0.0474984383291127, 
    -0.799072343629595, -0.702984163393113, -0.398929109013493, 1.43105614619551, 
    0.0764111195739305, 0.161472327278009, -0.0301078981611765, -0.378237123471118, 
    0.0358853373623658, -0.565057226355628, -0.52258441522509, 0.611815282795243, 
    -0.116306209278117, 1.98909149682229, 0.861438341477269, 0.150655146182104, 
    -1.22268327393422, 0.0952940364639085, -0.129098430978509, -0.858459253118616, 
    -1.1007456065898, -1.20413886175035, -0.837188779014039, -0.842990634928036, 
    0.294351603313769, -0.547886182595126, -1.06016691262797, -0.966644285706046, 
    -0.654633854443642, -1.38169407970147, -0.475697861112972, -0.0555881492221886, 
    1.53103281100899, -0.369733181047836, 0.517365121223683, -0.0503671114860659, 
    -0.469454336968186, 0.393645373207904, -0.316898613837062, -0.395030228935386, 
    0.187678412765698, -0.572666895934754, -0.643297209357882, 11.2460472096459, 
    -0.0397617419377608, 0.162431749388997, 0.844922266006946, -0.412534500135866, 
    -1.03901129611704, -0.647111057394775, 0.372737277859115, 3.27053324484176, 
    0.0587158350346983, 0.411045176265961, -0.692390917255549, -0.478150201304166, 
    -0.473190377127571, 0.0428326775338271, 0.472531545929091), Group = c("A", 
    "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
    "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", 
    "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
    "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "A", "A", "A", 
    "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
    "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
    "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", 
    "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"), 
        Timepoint = c("D2", "D10", "D10", "D15", "D2", "D5", "D10", 
        "D15", "D25", "D30", "D1", "D10", "D15", "D25", "D30", "D2", 
        "D5", "D7", "D15", "D25", "D30", "D1", "D2", "D5", "D7", 
        "D10", "D25", "D30", "D1", "D2", "D5", "D15", "D25", "D30", 
        "D1", "D2", "D5", "D10", "D15", "D5", "D10", "D15", "D1", 
        "D2", "D5", "D7", "D10", "D15", "D25", "D30", "D1", "D5", 
        "D30", "D1", "D1", "D2", "D15", "D1", "D2", "D5", "D5", "D10", 
        "D15", "D30", "D1", "D2", "D5", "D10", "D25", "D30", "D1", 
        "D2", "D5", "D10", "D1", "D5", "D10", "D15", "D30", "D1", 
        "D2", "D5", "D1", "D2", "D5", "D10", "D15", "D1", "D2", "D5", 
        "D10", "D25", "D30", "D2", "D5", "D15", "D1", "D2", "D1", 
        "D2", "D5", "D10", "D15", "D30"), Sex = c("M", "M", "M", 
        "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
        "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
        "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
        "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "F", 
        "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
        "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
        "F", "F", "F", "F", "M", "M", "M", "M", "M", "M", "M", "M", 
        "F", "F", "F", "F", "F", "F", "F", "F", "F", "M", "M", "F", 
        "F", "F", "F", "F", "F")), class = "data.frame", row.names = c(NA, 
    -104L))

ggplot(data=colData, aes(x=X, y=Y))+
 # geom_point(aes(colour = Timepoint,  shape=Group))+
  geom_point(aes(colour = Timepoint,  shape=Sex, size=2))+
  scale_shape_manual(values = c(M = "\u2642", F = "\u2640"))+
  #geom_text(size=1,nudge_y = 0.05,  check_overlap = FALSE) +
  xlab(paste("PC1 - ", pca.var.per[1], "%", sep="")) +
  ylab(paste("PC2 - ", pca.var.per[2], "%", sep="")) +
  theme_bw() +ggtitle("My PCA Graph")+ 
  theme(plot.title = element_text(hjust = 0.5)) 

Beside the problem that size appearing I have another problem that the male female size shape is not visible when I save the plot. With size 2 I can see it fine within R. Attached both figures: one from screenshot within R enter image description here and another when I save it enter image description here Please help. Thanks, Mitra

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • 7
    Try setting `size = 2` *outside* the `aes()` function. The symbol replacement probably depends on the exact way you export the plot. – teunbrand May 27 '22 at 14:16
  • Thank you for your help. Where to set the size option? If I am doing like geom_point(aes(colour = Timepoint, shape=Sex), size=2), then it is not working and having the default size. And for the save option...I am using R Studio and using the export > save as pdf option. – user3042163 May 29 '22 at 07:29
  • The code in your comment seems correct to me. The default size is 1.5, so it might not be very noticible. Typically, Cairo's PDF has better support for exotic letters than base's PDF, so you might try exporting with `ggsave(...your_options_here..., device = cairo_pdf)`. – teunbrand May 29 '22 at 07:40
  • Unfortunately the code is not working I can see hige difference if I put size=2 inside aes rather than geom_point(aes(colour = Timepoint, shape=Sex), size=2). So outside seems like it is not working. Further ggsave also gives me similar output like blocks and not male female symbol. – user3042163 May 31 '22 at 09:43

0 Answers0