我想复制以下图形:
为此,我使用以下脚本在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)尽管我定义了命令标记=列表(颜色='银色'),但箱线图主体不会改变颜色。
我的数据库是:
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")
对于配电线路,您可以使用hist
和dnorm
制作自己的线路。然后,您可以使用add_marker
添加它们(由于某些原因,我无法使其与add_lines
或add_trace
一起使用)。
对于点的颜色,需要在具有add_boxplot
的第一个boxpoints
内将颜色设置为银色。您已在第二个中将其设置为银色(没有绘制任何点),而第一个中则将其设置为黑色。
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)))
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9NSXIxNlJMLnBuZyJ9” alt =“”>
由reprex package(v0.3.0)在2020-05-28创建