作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现一个很好的 PC 绘图以及解释的累积方差。
我正在处理的数据框位于 https://www.kaggle.com/miroslavsabo/young-people-survey?select=responses.csv
df.responses <- read.csv("Data/responses.csv")
pref <- colnames(df.responses[0:63]) #columns for Music, Movies and Hobbies preferences
for(i in 1:length(pref)){
df.responses[is.na(df.responses[,i]), i] <- median(df.responses[,i], na.rm = TRUE)
}
df.movies <- data.frame(df.responses[20:31])
library(ggplot2)
library(factoextra)
pca.movies <- prcomp(df.movies, scale = TRUE,)
pca.movies$rotation <- -pca.movies$rotation
pca.movies$x <- -pca.movies$x
fviz_pca_var(pca.movies,
col.var = "contrib",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE
)
pv.movies <- pca.movies$sdev^2
pvp.movies <- pv.movies/sum(pv.movies)
pvp.movies
fviz_eig(pca.movies,
addlabels = T,
barcolor = "#E7B800",
barfill = "#E7B800",
linecolor = "#00AFBB",
choice = "variance",
ylim=c(0,25))
plot(cumsum(pvp.movies),xlab = "Cumulative proportion of Variance Explained", ylim=c(0,1),type = 'b')
最佳答案
fviz_eig
返回的对象是 ggplot
对象,因此您可以按如下方式合并两个图:
p <- fviz_eig(pca.movies,
addlabels = T,
barcolor = "#E7B800",
barfill = "#E7B800",
linecolor = "#00AFBB",
choice = "variance",
ylim=c(0,25))
df <- data.frame(x=1:length(pvp.movies),
y=cumsum(pvp.movies)*100/4)
p <- p +
geom_point(data=df, aes(x, y), size=2, color="#00AFBB") +
geom_line(data=df, aes(x, y), color="#00AFBB") +
scale_y_continuous(sec.axis = sec_axis(~ . * 4,
name = "Cumulative proportion of Variance Explained") )
print(p)
关于r - 有没有办法将总和添加到 fviz_eig 图中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62117449/
我是一名优秀的程序员,十分优秀!