如何在R中从fviz_nbclust图中保存xy坐标?

huangapple go评论167阅读模式
英文:

How to save xy coordinates from a fviz_nbclust plot in R

问题

I have a graph that takes over 20 minutes to generate in R. It is an 'elbow plot' from the fviz_nbclust package. Is there a way to save the x and y coordinates from the graph to manipulate further in a ggplot graph instead of waiting 20 minutes every time I need to edit/format the graph?

e.g. using the USA Arrests data set in R

  1. #load data
  2. df <- USArrests
  3. #remove rows with missing values
  4. df <- na.omit(df)
  5. #scale each variable to have a mean of 0 and sd of 1
  6. df <- scale(df)
  7. library(cluster)
  8. library(factoextra)
  9. #create plot of number of clusters vs total within sum of squares
  10. fviz_nbclust(df, kmeans, method = "wss")
  11. #Save coordinates of graph to be plotted on a ggplot graph here...
英文:

I have a graph that takes over 20 minutes to generate in R. It is an 'elbow plot' from the fviz_nbclust package. Is there a way to save the x and y coordinates from the graph to manipulate further in a ggplot graph instead of waiting 20 minutes every time I need to edit/format the graph?

e.g. using the USA Arrests data set in R

  1. #load data
  2. df &lt;- USArrests
  3. #remove rows with missing values
  4. df &lt;- na.omit(df)
  5. #scale each variable to have a mean of 0 and sd of 1
  6. df &lt;- scale(df)
  7. library(cluster)
  8. library(factoextra)
  9. #create plot of number of clusters vs total within sum of squares
  10. fviz_nbclust(df, kmeans, method = &quot;wss&quot;)
  11. #Save coordinates of graph to be plotted on a ggplot graph here...

答案1

得分: 2

尝试这个 - 如果我理解正确,您想要访问图中的数据点(x:簇的数量,y:twss)。所以,只需将图保存在一个变量中,然后访问数据框架:

  1. library(ggplot2)
  2. library(cluster)
  3. library(factoextra)
  4. # 载入数据
  5. df <- USArrests
  6. # 删除包含缺失值的行
  7. df <- na.omit(df)
  8. # 将每个变量缩放到均值为0,标准差为1
  9. df <- scale(df)
  10. fcl <- fviz_nbclust(df, kmeans, method = "wss")
  11. fcl$data
  12. # clusters y
  13. #1 1 196.00000
  14. #2 2 102.86240
  15. #3 3 78.32327
  16. #4 4 56.40317
  17. #5 5 70.83569
  18. #6 6 45.30784
  19. #7 7 39.03188
  20. #8 8 39.00701
  21. #9 9 32.24437
  22. #10 10 28.69826
  23. # 使用 ggplot 创建简单的图
  24. ggplot(fcl$data, aes(clusters, y, group=1)) + geom_point() + geom_line() + labs(title="寻找最佳簇数的肘部图", x="簇的数量", y="总内部平方和")

图:
如何在R中从fviz_nbclust图中保存xy坐标?

英文:

Try this - If I get it correctly you want to access data points in the plot (x: number of clusters, and y: twss). So, just save the plot in a variable and access the data dataframe:

  1. library(ggplot2)
  2. library(cluster)
  3. library(factoextra)
  4. #load data
  5. df &lt;- USArrests
  6. #remove rows with missing values
  7. df &lt;- na.omit(df)
  8. #scale each variable to have a mean of 0 and sd of 1
  9. df &lt;- scale(df)
  10. fcl &lt;- fviz_nbclust(df, kmeans, method = &quot;wss&quot;)
  11. fcl$data
  12. # clusters y
  13. #1 1 196.00000
  14. #2 2 102.86240
  15. #3 3 78.32327
  16. #4 4 56.40317
  17. #5 5 70.83569
  18. #6 6 45.30784
  19. #7 7 39.03188
  20. #8 8 39.00701
  21. #9 9 32.24437
  22. #10 10 28.69826
  23. # simple plot using ggplot
  24. ggplot(fcl$data, aes(clusters, y, group=1)) + geom_point() + geom_line() + labs(title=&quot;Elbow plot to find the optimal number of clusters&quot;, x=&quot;number of clusters&quot;, y=&quot;total within sum of square&quot;)

plot:
如何在R中从fviz_nbclust图中保存xy坐标?

huangapple
  • 本文由 发表于 2023年6月29日 12:03:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定