我想首先说我是 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
autoplot(prcomp(df), data = iris, colour = 'Species', shape='Species', frame=T)
最佳答案
我做了同样的 PCA
data<-iris
df<-iris[c(1, 2, 3, 4)]
PC<-prcomp(df)
PCi<-data.frame(PC$x,Species=data$Species)
现在您执行正常绘图并更改
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()
另外,检查
scale_fill_manual
对于框架编辑
我认为添加框架应该更容易,请在此处查看 https://stats.stackexchange.com/questions/22805/how-to-draw-neat-polygons-around-scatterplot-regions-in-ggplot2
和这里
ggplot2: geom_polygon with no fill
另外,我仍然相信 ggbiplot 应该处理
scale_color_manual
和 scale_fill_manual
,你能更新你失败的问题吗?
关于r - 更改点颜色和点周围框架/椭圆的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640492/