Alexandre Loures
Alexandre Loures

Reputation: 211

Change the color of boxpoints and add a distribution curve in plotly boxplot

I want to reproduce the following graphic:

enter image description here

For that I am using the plotly package in R with the following script:

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')

Obtaining the following result:

enter image description here

However, I have two questions:

1st) How do I plot the distribution curve for my points?

2nd) Although I define the command marker = list (color = 'silver') the boxplot body does not change color.

My database is:

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")

Upvotes: 1

Views: 1498

Answers (1)

M--
M--

Reputation: 29143

For the distribution line, you can make your own line using hist and dnorm. Then you can add them using add_marker (for some reason I could not get it to work with add_lines or add_trace).

For the color of the points, you need to set the color to silver within the first add_boxplot which you have boxpoints. You had it set to silver in the second one (which does not have any points plotted), while the first one was set to black.

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)))

Created on 2020-05-28 by the reprex package (v0.3.0)

Upvotes: 3

Related Questions