r - 更改点颜色和点周围框架/椭圆的颜色

标签 r ggplot2 pca ggfortify

我想首先说我是 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

PCA GRAPH OF SPECIES DEPICTED IN DIFFERENT COLORS
autoplot(prcomp(df), data = iris, colour = 'Species', shape='Species', frame=T) 

PCA GRAPH WITH FRAMES AROUND POINTS

最佳答案

我做了同样的 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()

enter image description here

另外,检查 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_manualscale_fill_manual ,你能更新你失败的问题吗?

关于r - 更改点颜色和点周围框架/椭圆的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640492/

相关文章:

julia - 内存高效中心稀疏 SVD/PCA(在 Julia 中)?

r - 在 R 中创建嵌套命名列表

r - 如何从 R markdown 到 Latex 转换中删除紧凑标题?

r - ggplot2:一个图例,具有从公共(public)变量派生的两个视觉属性

R:为什么我的 ggplot geom_point() 符号不可见?

r - 从 ggplot 函数返回的绘图工具提示名称

apache-spark - PySpark PCA : how to convert dataframe rows from multiple columns to a single column DenseVector?

R:添加一列较短的长度,减去单列中的每一行,第 1 - 2,第 2 - 3

使用列从数据帧中随机采样

python - 在线性回归中使用 PCA