我想首先说我是R的新手用户,特别是本网站的用户,所以如果有必要在此澄清任何内容,请告诉我!我还不太了解一切,所以请尽可能"愚蠢".
问题:我想创建描绘两组(在本例中为物种)的PCA图.我还想在它们周围绘制椭圆或框架.
谢天谢地,我已经使用ggplot2完成了这项任务!但是,我无法更改超出默认值的点或椭圆/帧的颜色.
你能就此事提供一些帮助吗?
请参阅下面的示例代码,它只是PCA示例中经常使用的传统虹膜数据集.
###load in plackages###
library(ggbiplot)
library(ggfortify)
library(cluster)
#my actual data is very similar to the iris data, though in my data the "Species" column is first
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
df <- iris[c(1, 2, 3, 4)]
autoplot(prcomp(df))
autoplot(prcomp(df), data = iris, colour = 'Species') #pca graph with species depicted in different colors
Run Code Online (Sandbox Code Playgroud)
autoplot(prcomp(df), data = iris, colour = 'Species', shape='Species', frame=T)
Run Code Online (Sandbox Code Playgroud)
我做了同样的PCA
data<-iris
df<-iris[c(1, 2, 3, 4)]
PC<-prcomp(df)
PCi<-data.frame(PC$x,Species=data$Species)
Run Code Online (Sandbox Code Playgroud)
现在,您可以ggplot像往常一样进行正常绘图和更改参数
ggplot(PCi,aes(x=PC1,y=PC2,col=Species))+
geom_point(size=3,alpha=0.5)+ #Size and alpha just for fun
scale_color_manual(values = c("#FF1BB3","#A7FF5B","#99554D"))+ #your colors here
theme_classic()
Run Code Online (Sandbox Code Playgroud)
另外,检查scale_fill_manual框架
编辑
我认为添加一个框架应该更容易,请点击这里https://stats.stackexchange.com/questions/22805/how-to-draw-neat-polygons-around-scatterplot-regions-in-ggplot2
和这里
ggplot2:geom_polygon没有填充
另外,我仍然认为,ggbiplot应该处理scale_color_manual与scale_fill_manual,你可以更新你的问题失败了?
| 归档时间: |
|
| 查看次数: |
19762 次 |
| 最近记录: |