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

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

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

#load data
df <- USArrests

#remove rows with missing values
df <- na.omit(df)

#scale each variable to have a mean of 0 and sd of 1
df <- scale(df)

library(cluster)
library(factoextra)

#create plot of number of clusters vs total within sum of squares
fviz_nbclust(df, kmeans, method = "wss")

#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

#load data
df &lt;- USArrests

#remove rows with missing values
df &lt;- na.omit(df)

#scale each variable to have a mean of 0 and sd of 1
df &lt;- scale(df)

library(cluster)
library(factoextra)

#create plot of number of clusters vs total within sum of squares
fviz_nbclust(df, kmeans, method = &quot;wss&quot;)

#Save coordinates of graph to be plotted on a ggplot graph here...

答案1

得分: 2

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

library(ggplot2)
library(cluster)
library(factoextra)

# 载入数据
df <- USArrests

# 删除包含缺失值的行
df <- na.omit(df)

# 将每个变量缩放到均值为0,标准差为1
df <- scale(df)

fcl <- fviz_nbclust(df, kmeans, method = "wss")
fcl$data
#   clusters         y
#1         1 196.00000
#2         2 102.86240
#3         3  78.32327
#4         4  56.40317
#5         5  70.83569
#6         6  45.30784
#7         7  39.03188
#8         8  39.00701
#9         9  32.24437
#10       10  28.69826

# 使用 ggplot 创建简单的图
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:

library(ggplot2)
library(cluster)
library(factoextra)

#load data
df &lt;- USArrests

#remove rows with missing values
df &lt;- na.omit(df)

#scale each variable to have a mean of 0 and sd of 1
df &lt;- scale(df)

fcl &lt;- fviz_nbclust(df, kmeans, method = &quot;wss&quot;)
fcl$data
#   clusters         y
#1         1 196.00000
#2         2 102.86240
#3         3  78.32327
#4         4  56.40317
#5         5  70.83569
#6         6  45.30784
#7         7  39.03188
#8         8  39.00701
#9         9  32.24437
#10       10  28.69826

# simple plot using ggplot
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:

确定