How to Plot in 3D clusters using plotly package
R-Language is widely used in Data Science, Data Visualization Data Analysis many more, etc. Plotly package is highly rich in plotting various graphs, These graphs/charts are highly interactive and user-friendly.
The 3D cluster visualizes the similarity between variables as 3-D spatial relationships. Each point in the graph represents an individual property. the points which are closer together were more frequently sorted into the same category or class.
Using Example:
Plotting the following dataset in 3-D plot using plotly library in R Programming Language.
x |
y |
z |
1
|
10
|
2
|
2
|
20
|
4
|
3
|
30
|
8
|
4
|
40
|
16
|
5
|
50
|
32
|
Syntax:
plot_ly(data_object, x, y, z)
where:
- data_object: Represents the dataset or dataframe object.
- x : Represent x-data vector.
- y : Represent y-data vector.
- z : Represent z-data vector.
Example 1: In this example, we will create an exemplary dataset and then plot a 3D Cluster graph using that.
R
library (plotly)
x = c (1, 2, 3, 4, 5)
y = c (10, 20, 30, 40, 50)
z = c (2, 4, 8, 16, 32)
df = data.frame (x, y, z)
df
plt <- plot_ly (df, x = ~x, y = ~y, z = ~z)
plt <- plt %>% add_markers ()
plt <- plt %>% layout (scene = list (xaxis = list (title = 'x-axis' ),
yaxis = list (title = 'y-axis' ),
zaxis = list (title = 'z-axis' )))
plt
|
Output:
Example 2: In this example, we will use the iris dataset and then plot it with the original labels using any three independent features.
R
library (plotly)
data = iris[, 1:4]
data$cluster = factor ( kmeans (data, 3)$cluster)
clust<- plot_ly (data, x=~Sepal.Length,
y=~Sepal.Width,
z=~Petal.Width,
color=~cluster) %>%
add_markers (size=1.5)
clust
|
Output:
Last Updated :
13 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...