【问题标题】:Change the color of boxpoints and add a distribution curve in plotly boxplot更改 boxpoints 的颜色并在 plotly boxplot 中添加分布曲线
【发布时间】:2020-08-24 09:23:34
【问题描述】:

我想重现下图:

为此,我在 R 中使用带有以下脚本的 plotly 包:

library (plotly)

g <- plot_ly (type = 'box')

h <- g %>% add_boxplot (y = df$lmean, jitter = 0.3, pointpos = -1.8, boxpoints = 'all', marker = list (color = 'black'), line = list (color = 'black'), name = 'AB') %>% layout (showlegend = FALSE)

i <- h %>% add_boxplot (y = list (20.25453, 21.32184, 21.56097, 20.91322, 19.50758, 20.63540, 21.69172, 20.00605, 18.33375, 20.35927, 18.95515, 20.18885, 18.02808, 21.67389, 18.00610), marker = list (color = 'silver'), line = list (color = 'black'), boxpoints = FALSE, name = 'A')

j <- i %>% add_boxplot (y = list (23.42700, 23.44604, 24.38043, 23.38132, 23.42772, 23.44200, 22.95047, 22.99269, 23.40156, 23.56823, 22.06118, 22.75578, 19.48760, 25.21417, 21.41917), marker = list (color = 'silver'), line = list (color = 'black'), boxpoints = FALSE, name = 'B')

得到如下结果:

但是,我有两个问题:

1st) 如何绘制我的点的分布曲线?

2nd) 虽然我定义了命令 marker = list (color = 'silver'),但箱线图主体不会改变颜色。

我的数据库是:

df <- structure(list(Mean = c(14935404544, 15222448128, 38751113216,
14268438528, 14946194432, 15161123840, 9273937920, 9673856000,
14560262144, 17200879616, 3811098880, 7633272320, 290639968,
89202434048, 2005522432, 625789952, 1819531136, 2311044096, 1209199872,
296506752, 915887232, 2633880832, 488109632, 91674344, 694895872,
170653968, 586009152, 67529624, 2587323136, 66061892), type = c("gmean",
"gmean", "gmean", "gmean", "gmean", "gmean", "gmean", "gmean",
"gmean", "gmean", "gmean", "gmean", "gmean", "gmean", "gmean",
"mean", "mean", "mean", "mean", "mean", "mean", "mean", "mean",
"mean", "mean", "mean", "mean", "mean", "mean", "mean"), lmean = c(23.4270003752245,
23.4460370258537, 24.380425320488, 23.3813158390422, 23.4277225513355,
23.4420003463391, 22.9504739289101, 22.992692825977, 23.4015618839485,
23.5682263599233, 22.0611834044475, 22.7557824657935, 19.4875958357754,
25.2141741636937, 21.4191704283524, 20.2545253328222, 21.3218446872636,
21.560965248974, 20.9132247150145, 19.5075805484868, 20.6354038058895,
21.6917241970269, 20.0060505943238, 18.3337531161829, 20.3592725678438,
18.9551484853174, 20.1888459651668, 18.0280769336398, 21.673889639965,
18.0061026181465)), datalabel = "", time.stamp = " 1 May 2020 12:05", formats = c("%9s",
"%9.0g", "%9s"), types = c(5L, 254L, 5L), val.labels = c("",
"", ""), var.labels = c("Reporter.ISO", "", ""), row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30"), version = 12L, class = "data.frame")

【问题讨论】:

    标签: r plotly boxplot r-plotly


    【解决方案1】:

    对于配送线路,您可以使用histdnorm 制作自己的线路。然后您可以使用add_marker 添加它们(由于某种原因,我无法使其与add_linesadd_trace 一起使用)。

    对于点的颜色,您需要在您拥有boxpoints 的第一个add_boxplot 内将颜色设置为银色。您将第二个设置为银色(没有绘制任何点),而第一个设置为黑色。

    library (plotly)
    
    h <- hist(df$lmean, breaks = seq(18,25.5,0.5), density = 10, col = "lightgray") 
    xfit <- seq(min(df$lmean), max(df$lmean), length = 40) 
    yfit <- dnorm(xfit, mean = mean(df$lmean), sd = sd(df$lmean)) 
    
    plot_ly (type = 'box') %>% 
    add_boxplot (y = df$lmean, jitter = 0.5, pointpos = -1.8, boxpoints = 'all', 
                   marker = list(color = 'silver'), line = list(color = 'black'), name = 1) %>% 
    add_boxplot (y = list(20.25453, 21.32184, 21.56097, 20.91322, 19.50758, 20.63540, 21.69172,  
                          20.00605, 18.33375, 20.35927, 18.95515, 20.18885, 18.02808, 21.67389, 
                          18.00610), 
                 line = list(color = 'black'), boxpoints = FALSE, name = 2) %>% 
    add_boxplot (y = list(23.42700, 23.44604, 24.38043, 23.38132, 23.42772, 23.44200, 22.95047,  
                          22.99269, 23.40156, 23.56823, 22.06118, 22.75578, 19.48760, 25.21417, 
                          21.41917), 
                 line = list(color = 'black'), boxpoints = FALSE, name = 3) %>% 
    add_markers(x = yfit+.5, y = xfit, mode = 'lines', 
                line=list(width = 2), marker = list(size=1)) %>% 
    layout (showlegend = FALSE,
              xaxis = list(ticktext = list("AB", "A", "B"), tickvals = list(1, 2, 3)))
    

    reprex package (v0.3.0) 于 2020 年 5 月 28 日创建

    【讨论】:

      猜你喜欢
      • 2018-11-17
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 2018-02-27
      相关资源
      最近更新 更多