微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在 R Plotly 中将所有小提琴设置为相同的宽度

如何解决无法在 R Plotly 中将所有小提琴设置为相同的宽度

我试图在 R Plotly 中重现一个在 ggplot2 中工作正常的 2 个分类变量小提琴图。但是当我将各个小提琴的宽度设置为相同时,使用 scalemode = "width",如参考 (https://plotly.com/r/reference/violin/) 中所述,它根本不起作用。相反,它显示了与每个类别中的计数成正比的宽度(小提琴最大值)。

这是一个例子:


# Paths:
path_data = "data/"
path_lib = "renv/library/R-4.1/x86_64-pc-linux-gnu/"

# Packages:
require(dplyr,lib = path_lib)
require(readr,lib = path_lib)
require(RColorBrewer,lib = path_lib)
require(plotly,lib = path_lib)

# Dataset:
df = readr::read_csv(paste0(path_data,"nasa_exoplanets.csv")) %>%
         as.data.frame()
attr(df,"spec") = NULL
df_varnames = readr::read_csv(paste0(path_data,"nasa_exoplanets_var_names.csv")) %>%
                  as.data.frame()
attr(df_varnames,"spec") = NULL

# Variables:
cat_var1 = "st_metratio"
cat_var2 = "disc_locale"
cat_var_name1 = (df_varnames %>%
                    dplyr::filter(var == cat_var1))$var_name
cat_var_name2 = (df_varnames %>%
                     dplyr::filter(var == cat_var2))$var_name
num_var = "sy_dist"
num_var_name = (df_varnames %>%
                    dplyr::filter(var == num_var))$var_name

# Adapt the data:
df_plot = df %>%
              dplyr::select(cat_var1,cat_var2,num_var)

# Deal with NA:
df_plot[which(is.na(df_plot[,cat_var1])),cat_var1] = "NA"
df_plot[which(is.na(df_plot[,cat_var2])),cat_var2] = "NA"
df_plot = df_plot[which(!is.na(df_plot[,num_var])),]

# Levels order:
sorted_levels1 = sort(unique(df_plot[,cat_var1]))
df_plot[,cat_var1] = factor(x = df_plot[,cat_var1],levels = sorted_levels1)
sorted_levels2 = sort(unique(df_plot[,cat_var2]))
df_plot[,cat_var2] = factor(x = df_plot[,cat_var2],levels = sorted_levels2)

# Plot:
my_palette = colorRampPalette(c("#111539","#97A1D9"))
n_levels2 = length(unique(df_plot[,cat_var2]))
p = plot_ly(
    data = df_plot,type = "violin",x = ~eval(parse(text = cat_var1)),y = ~eval(parse(text = num_var)),color = ~eval(parse(text = cat_var2)),colors = my_palette(n_levels2),spanmode = "hard",alpha = 1,Box = list(visible = FALSE),meanline = list(visible = FALSE),points = FALSE,scalemode = "width"                           ### this doesn't work ###
) %>%
    layout(
        xaxis = list(
            title = paste0("<b>",cat_var_name1,"</b>"),titlefont = list(size = 20),tickfont = list(size = 18),categoryorder = "array"
        ),yaxis = list(
            title = paste0("<b>",num_var_name,type = "log"
        ),margin = list(
            l = 10,r = 10,t = 10,b = 10
        ),legend = list(
            title = list(
                text = paste0("<br><b>",cat_var_name2,font = list(size = 18)
            )
        ),hoverlabel = list(font = list(size = 16)),showlegend = TRUE,violinmode = "group"
    )

p

数据文件https://github.com/rafael747cardoso/Data_Visualization_Gallery/blob/main/data/nasa_exoplanets.csv

它应该如何,在 ggplot2 中绘制:

How it should be,plotted in ggplot2

R Plotly 的情况:

How it is with R Plotly

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。