我试图生成一个3D散点图,标记的形状对应于"治疗"(IC,IT,YS),而 colored颜色 对应于"时间"(0,2,4,6,12,24,48,72).一切顺利,除了图例顺序是:自动生成图例顺序

enter image description here

这个顺序会令人困惑,我希望它在从0到72的顺序.我猜这个自动生成的图例顺序是由于时间从数字转换为因子而引起的.然而,我没有找到一个好的方法来手动设置顺序.有人能帮忙吗?

下面是我的代码:

# PCA Plot - 3D
pca_data_3D <- data.frame(Sample = rownames(pca$x),
                          PC1 = pca$x[,1],
                          PC2 = pca$x[,2],
                          PC3 = pca$x[,3],
                          Treatment = pca_data_2D$Treatment,   # already converted to factor
                          Time = pca_data_2D$Time)             # already converted to factor

treatment_groups <- c("YS" = "square", "IT" = "cross", "IC" = "circle")
time_groups <- c("0" = "#BC3C29", "2" = "#0072B5", "4" = "#E18727", "6" = "#20854E", 
                 "12" = "#7876B1", "24" = "#6F99AD", "48" = "#FFDC91", "72" = "#EE4C97")

hover_info <- pca_data_3D$Sample

pca_data_3D %>%
  plot_ly(
    type = "scatter3d", mode = "markers",
    x = ~PC1, y = ~PC2, z = ~PC3,
    symbol = ~Treatment, symbols = treatment_groups, 
    color = ~Time, colors = time_groups,
    hovertext = ~hover_info
  ) %>%
  layout(
    title = "PCA Graph PC1,2,3"
  )

我试着设置legendgroup = ~Timetraceorder = "normal"/"grouped"(分开),但它似乎没有工作.但我不确定我是否以正确的方式做这些.

推荐答案

下一次请举一个不好的例子.

传奇顺序是按顺序设置的.因此,正确设置图例顺序的一种方法是在变量"time"前面放一个零.

# Number of samples
num_samples <- 100

# Generating PC values
pc1 <- rnorm(num_samples, mean = 0, sd = 1)
pc2 <- rnorm(num_samples, mean = 0, sd = 1)
pc3 <- rnorm(num_samples, mean = 0, sd = 1)

# Generating treatment and time information
treatment <- sample(c("YS", "IT", "IC"), num_samples, replace = TRUE)
time <- sample(c("00", "02", "04", "06", "12", "24", "48", "72"), num_samples, replace = TRUE)

# Combining into a data frame
pca_data_3D <- data.frame(
  Sample = paste0("Sample", 1:num_samples),
  PC1 = pc1,
  PC2 = pc2,
  PC3 = pc3,
  Treatment = as.factor(treatment),
  Time = as.factor(time)
)

# Checking the first few rows of the dataset
head(pca_data_3D)

treatment_groups <- c("YS" = "square", "IT" = "cross", "IC" = "circle")
time_groups <- c("00" = "#BC3C29", "02" = "#0072B5", "04" = "#E18727", "06" = "#20854E", 
                 "12" = "#7876B1", "24" = "#6F99AD", "48" = "#FFDC91", "72" = "#EE4C97")
#time_groups <- factor(time_groups, levels = names(time_groups))
hover_info <- pca_data_3D$Sample

pca_data_3D %>%
  plot_ly(
    type = "scatter3d", mode = "markers",
    x = ~PC1, y = ~PC2, z = ~PC3,
    symbol = ~Treatment, symbols = treatment_groups, 
    color = ~Time, colors = time_groups,
    hovertext = ~hover_info
  ) %>%
  layout(
    title = "PCA Graph PC1,2,3"
  )

enter image description here

R相关问答推荐

删除facet_wrap标签之间的水平线

根据收件箱中的特定值提取列名

在值和NA的行顺序中寻找中断模式

根据R中两个变量的两个条件删除带有dspirr的行

使用tidyverse方法绑定行并从一组管道列表执行左连接

将非重复序列高效转换为长格式

在GGPLATE中将突出的点放在前面

制作等距离的线串副本

仅 Select 超过9行的CSV文件

R-按最接近午夜的时间进行筛选

将标识符赋给事件序列,避免错误观察

从多面条形图中删除可变部分

将项粘贴到向量中,并将它们分组为x的倍数,用空格分隔

如何创建累加到现有列累计和的新列?

减go R中列表的所有唯一元素对

防止在更新SHINY中的Reactive Value的部分内容时触发依赖事件

有没有办法将不等长的列表转换为R中的数据帧

R-如何在ggplot2中显示具有不同x轴值(日期)的多行?

通过比较来自多个数据框的值和R中的条件来添加新列

在分面的ggplot2条形图中对条形图进行排序,并省略每组未使用的系数级别