这是我的数据:
head(pcf)
IID POP PC1 PC2 PC3 PC4 PC5 shape
1 HG01113 CLM -0.00284857 0.01432160 0.01585010 -0.024035900 -0.01479180 16
2 HG01121 CLM -0.00397075 0.00928773 0.00451518 -0.003877280 0.00327456 16
3 HG01122 CLM -0.00718415 0.01118450 0.00167478 0.000797229 0.00836530 16
4 HG01124 CLM -0.00986231 0.00227199 0.00715461 -0.011777700 -0.00514219 16
5 HG01125 CLM -0.00850170 0.00826662 0.01105800 -0.010627100 -0.00627586 16
6 HG01130 CLM -0.00680245 0.01178180 0.00824017 -0.012398000 -0.00414566 16
str(pcf)
'data.frame': 6385 obs. of 8 variables:
$ IID : Factor w/ 6385 levels "HG01113","HG01121",..: 1 2 3 4 5 6 7 8 9 10 ...
$ POP : Factor w/ 29 levels "CEU","CLM","MXL",..: 2 2 2 2 2 2 2 2 2 2 ...
$ PC1 : num -0.00285 -0.00397 -0.00718 -0.00986 -0.0085 ...
$ PC2 : num 0.01432 0.00929 0.01118 0.00227 0.00827 ...
$ PC3 : num 0.01585 0.00452 0.00167 0.00715 0.01106 ...
$ PC4 : num -0.024036 -0.003877 0.000797 -0.011778 -0.010627 ...
$ PC5 : num -0.01479 0.00327 0.00837 -0.00514 -0.00628 ...
$ shape: num 16 16 16 16 16 16 16 16 16 16 ...
我想用不同颜色和形状制作PC1与PC2的散点图。我设法改变了情节中的颜色和形状。但是,图例形状不会改变?我尝试手动更改它没有成功。我在这个网站上尝试了其他答案,但无法解决我的问题。谢谢。
这样可行但传奇形状不是我的形状列。
ggplot(pcf, aes(x = PC1, y = PC2, shape = shape, color = POP)) + geom_point(size=2.5,alpha=0.8) + scale_color_manual(values=colf) +
ggtitle("PC1 vs PC2") +theme(legend.title=element_blank()) + scale_shape_identity()
我尝试手动更改,但它不起作用:
ggplot(pcf, aes(x = PC1, y = PC2, shape = shape, color = POP)) + geom_point(size=2.5,alpha=0.8) + scale_color_manual(values=colf) +
ggtitle("PC1 vs PC2") +theme(legend.title=element_blank()) + scale_shape_manual(values=c(17,15,17,17,17,17,17,17,17,17,17,12))
数据样本:
如果我使用相同的功能,但没有添加颜色,我仍然无法解决图例上形状的问题:
ggplot(pcf2, aes(x = PC1, y = PC2, shape = shape, color = POP)) + geom_point(size=2.5,alpha=0.8) +
ggtitle("PC1 vs PC2") +theme(legend.title=element_blank()) + scale_shape_identity()
pcf2 <- read.table(header=TRUE, text="IID POP PC1 PC2 PC3 PC4 PC5 shape
HG01113 CLM -0.00284857 0.0143216 0.0158501 -0.0240359 -0.0147918 16
HG01121 CLM -0.00397075 0.00928773 0.00451518 -0.00387728 0.00327456 16
HG01122 CLM -0.00718415 0.0111845 0.00167478 0.000797229 0.0083653 16
HG01124 CLM -0.00986231 0.00227199 0.00715461 -0.0117777 -0.00514219 16
HG01125 CLM -0.0085017 0.00826662 0.011058 -0.0106271 -0.00627586 16
HG01130 CLM -0.00680245 0.0117818 0.00824017 -0.012398 -0.00414566 16
HG01131 CLM -0.00652902 0.0109067 0.00998472 -0.0121705 -0.00635762 16
HG01134 CLM -0.0113558 0.00757026 0.00777452 -0.0144193 -0.00243384 16
HG01136 CLM -0.00748575 0.00859226 0.00928788 -0.0136143 -0.00756104 16
HG01137 CLM -0.001601 0.00926993 0.0126941 -0.0147104 -0.0144014 16
ZAN0140 ZAN -0.00858195 0.00409118 0.00875769 -0.0106631 -0.00546549 16
ZAN0141 ZAN -0.0115329 -0.00297351 0.00911975 -0.0126809 -0.0120499 16
ZAN0142 ZAN -0.0117186 -0.0044713 0.0101049 -0.0141045 -0.0151751 16
ZAN0143 ZAN -0.0114532 -0.00415046 0.00547798 -0.00756357 -0.00506384 16
ZAN0144 ZAN -0.0213624 -0.00843936 0.00607225 -0.00874548 -0.00393357 16
ZAN0145 ZAN -0.00409835 0.00369836 0.0101785 -0.0147436 -0.00811818 16
ZAN0146 ZAN -0.0097486 -0.00643543 0.00687725 -0.0105866 -0.00621137 16
ZAN0147 ZAN -0.00915782 0.00459452 0.00728844 -0.0122016 -0.00862952 16
ZAN0148 ZAN -0.0104426 0.0130516 0.00894079 -0.0103534 -0.000177178 16
ZAN0149 ZAN -0.0114827 -0.00395054 0.00823912 -0.0115696 -0.0121063 16
ZAN0150 ZAN -0.00608103 0.0123405 0.0102158 -0.0120628 -0.00832722 16
NA12763 CEU -0.0105495 0.0276811 -0.00224035 0.010225 0.0299631 17
NA12775 CEU -0.010177 0.0281884 -0.00250204 0.00959566 0.0306824 17
NA12776 CEU -0.0101447 0.027379 -0.00347948 0.0116001 0.0279258 17
NA12777 CEU -0.0105071 0.028844 -0.00320231 0.00978598 0.0300142 17
NA12778 CEU -0.0102704 0.0283507 -0.00261262 0.0101554 0.0299162 17
NA12812 CEU -0.0104907 0.0280414 -0.00393934 0.0107501 0.0295738 17
NA12814 CEU -0.0106704 0.0283296 -0.0030067 0.00877782 0.0298366 17
NA12815 CEU -0.0101239 0.0277695 -0.00322395 0.0111492 0.0314943 17
NA12827 CEU -0.0101015 0.0283498 -0.00298194 0.00927265 0.0291947 17
NA12828 CEU -0.0102718 0.0276927 -0.00440554 0.010354 0.0273352 17
NA12829 CEU -0.0105987 0.0276975 -0.00251459 0.00949609 0.027011 17
NA12842 CEU -0.0104375 0.0279966 -0.00427633 0.0118914 0.0311316 17
NA12843 CEU -0.0100162 0.0282866 -0.00352916 0.0122782 0.0319997 17
NA12872 CEU -0.0105538 0.027088 -0.0018147 0.0104983 0.0302558 17
NA12873 CEU -0.0103432 0.0277881 -0.00359204 0.0108997 0.0311401 17
NA12874 CEU -0.0112648 0.0279512 -0.000582816 0.00909585 0.0292212 17
NA12889 CEU -0.0107102 0.0283009 -0.00292015 0.010161 0.0269977 17
NA12890 CEU -0.0104645 0.0284091 -0.00234357 0.00891193 0.0297733 17
NA18486 YRI -0.067202 -0.0580439 -0.0103843 0.0072099 -0.0025057 18
NA18488 YRI -0.0679582 -0.0575973 -0.0099637 0.00786681 -0.00176836 18
NA18489 YRI -0.0665639 -0.0572469 -0.0104585 0.00290702 -0.00247813 18
NA18498 YRI -0.0675714 -0.0578045 -0.00714871 0.00705127 -0.000810779 18
NA18499 YRI -0.0672439 -0.0581789 -0.00975973 0.00608173 -0.00354541 18
NA18501 YRI -0.0681507 -0.0589588 -0.00955431 0.00765419 0.00182204 18
NA18502 YRI -0.0677346 -0.0585321 -0.00946384 0.00801378 0.00215776 18
NA18504 YRI -0.0674863 -0.0588057 -0.0102074 0.008341 -0.000466679 18
NA18505 YRI -0.0683932 -0.0590951 -0.00959998 0.00761924 0.00080716 18
NA18507 YRI -0.0670518 -0.0582837 -0.00794506 0.00631916 -0.00260588 18
NA18508 YRI -0.0679485 -0.0585929 -0.00827961 0.00845302 0.000591506 18
NA18510 YRI -0.0674159 -0.0584644 -0.0101846 0.0078433 0.000852638 18
NA18511 YRI -0.0670849 -0.0581807 -0.0103638 0.00851522 -0.000592863 18
NA18516 YRI -0.0687822 -0.0592477 -0.00899918 0.00443706 -0.001659 18
NA18517 YRI -0.0670035 -0.058226 -0.00876939 0.00489847 -0.00112354 18
NA18519 YRI -0.0669615 -0.0578218 -0.00695668 0.00571593 -0.00287655 18
NA18520 YRI -0.0680311 -0.0581343 -0.00911587 0.00869178 -0.00377441 18
NA18522 YRI -0.0678606 -0.0583533 -0.0103429 0.00532382 -0.000140096 18
NA18523 YRI -0.0671548 -0.0578681 -0.0116837 0.00707126 -0.00172178 18
NA18853 YRI -0.0684294 -0.0587003 -0.01088 0.0066453 0.000847722 18
NA18856 YRI -0.0668358 -0.0572994 -0.00962481 0.00882953 0.000972082 18
NA18858 YRI -0.0672418 -0.0577705 -0.0111758 0.00790765 -0.000706462 18
NA18861 YRI -0.067371 -0.0577288 -0.00911194 0.00500546 -0.00104702 18
NA18864 YRI -0.0665593 -0.0579576 -0.00921739 0.00677675 0.00126966 18
NA18865 YRI -0.068028 -0.0587356 -0.00898244 0.00833009 0.000123099 18
NA18867 YRI -0.0671763 -0.0583929 -0.00788941 0.00667197 -0.00272322 18
NA18868 YRI -0.067829 -0.05892 -0.0075401 0.00683461 -0.00272168 18
NA18870 YRI -0.0687969 -0.059312 -0.00936288 0.00699998 -0.00129002 18
NA18871 YRI -0.0685796 -0.0598433 -0.00940723 0.0053345 -0.000116678 18
NA18873 YRI -0.0671831 -0.0577216 -0.00908974 0.00755018 -0.00379051 18
NA18874 YRI -0.0670375 -0.0577672 -0.008836 0.00841137 -0.00202849 18
NA18876 YRI -0.0675866 -0.0580677 -0.00881668 0.00660232 -0.000117921 18")
答案 0 :(得分:3)
您可以尝试使用guides()
和guide_legend()
:
ggplot(pcf2, aes(x = PC1, y = PC2, shape = shape, color = POP)) + geom_point(size=2.5,alpha=0.8) +
ggtitle("PC1 vs PC2") +theme(legend.title=element_blank()) + scale_shape_identity() +
guides(colour = guide_legend(override.aes = list(shape = tapply(pcf2$shape, pcf2$POP, mean, na.rm = TRUE))))