change aspect ratio of scatterplot3d

85 views Asked by At

I tried to create a perfect square 3d scatter plot just like figure 1, using methods such as png(width=500, height=500), or par(pty = "s"), or by adjusting parameters "angle" and "asp" in scatterplot3d. However, still plot something like figure2. My goal was to establish specific aspect ratios: x/y=2, x/z=2, and y/z=1. Unfortunately, the 'asp' parameter did not allow multiple inputs. enter image description here

library(scatterplot3d)
set.seed(123)
pc1 <- rnorm(100)
pc2 <- rnorm(100)
pc3 <- rnorm(100)
groups <- sample(c('Bread', 'Durum', 'Spelt'), 100, replace = TRUE)

# Create a data frame with the PC values and group labels
data <- data.frame(PC1 = pc1, PC2 = pc2, PC3 = pc3, Group = groups)

# Define colors for each group
colors <- c('Bread' = 'pink', 'Durum' = 'lightgreen', 'Spelt' = 'skyblue')

# Set up a square plotting window
png("test.png", width = 500, height = 500)
par(c(0.5, 0.5, 0.5, 0.1), pty = "s")

# Create a 3D scatter plot with square size points
scatterplot3d(data$PC1, data$PC2, data$PC3, color = colors[data$Group],
          pch = 19, type = "p", grid = TRUE,
          xlab = "PC1 (11.63%)", ylab = "PC2 (7.03%)", zlab = "PC3 (4.09%)",
          mar=c(5,3,4,3)+0.1,
          angle=45,
          asp = 0.5)

 # Manually add group annotations without outside box
 legend("topright", legend = unique(data$Group), col = unique(colors), pch = 19, bty = "n",  inset = c(0.1,0), xpd=TRUE, ncol=1)

 dev.off()

enter image description here

0

There are 0 answers