更改点颜色和点周围框架/椭圆的颜色 [英] Change point colors and color of frame/ellipse around points
本文介绍了更改点颜色和点周围框架/椭圆的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先我想说我是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)
推荐答案
我也做了相同的主成分分析
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
另外,我仍然认为ggbilot应该可以处理scale_color_manual
和scale_fill_manual
,您能更新您失败的问题吗?
这篇关于更改点颜色和点周围框架/椭圆的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文