Creating a 3D scatterplot using ggplot2 in R

gene_x 0 like s 346 view s

Tags: genes

library(ggplot2)
library(plotly)

# Create some random data
set.seed(123)
x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)

# Perform PCA
data <- data.frame(x, y, z)
pca <- prcomp(data, scale = TRUE)
scores <- as.data.frame(pca$x)

# Create 3D scatterplot using ggplot2 and plotly
ggplot(scores, aes(x = PC1, y = PC2, z = PC3)) +
  geom_point(size = 3, color = "blue") +
  labs(x = "PC1", y = "PC2", z = "PC3") +
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black", size = 0.5),
        panel.border = element_blank()) +
  scale_x_continuous(limits = c(-4, 4), expand = c(0, 0)) +
  scale_y_continuous(limits = c(-4, 4), expand = c(0, 0)) +
  scale_z_continuous(limits = c(-4, 4), expand = c(0, 0)) +
  coord_cartesian(xlim = c(-4, 4), ylim = c(-4, 4), zlim = c(-4, 4)) +
  ggtitle("3D Scatterplot using PCA") +
  theme(plot.title = element_text(size = 12, face = "bold"))

This code creates a 3D scatterplot using ggplot2 and the plotly package. It first generates some random data and performs principal component analysis (PCA) on it. The resulting PCA scores are then plotted in a 3D scatterplot. The labs() function is used to set the axis labels, and the ggtitle() function is used to set the plot title. The scale_x_continuous(), scale_y_continuous(), and scale_z_continuous() functions are used to set the axis limits, and the coord_cartesian() function is used to ensure that the plot is displayed with the specified limits. The theme() function is used to adjust the appearance of the plot.

like unlike

点赞本文的读者

还没有人对此文章表态


本文有评论

没有评论

看文章,发评论,不要沉默


© 2023 XGenes.com Impressum