如何绘制具有计算值的绘图箱图?

时间:2019-12-19 13:46:27

标签: r ggplot2 r-plotly ggplotly

我有以下代码用于在ggplot2中创建箱形图:

throughput <- c(1, 2, 3, 4, 5)
response_time_min <- c(9, 19, 29, 39, 49)
response_time_10  <- c(50, 55, 60, 60, 61)
response_time_med <- c(100, 100, 100, 100, 100)
response_time_90  <- c(201, 201, 250, 200, 230)
response_time_max <- c(401, 414, 309, 402, 311)
df <- data.frame(throughput, response_time_min, response_time_10, response_time_med,response_time_90, response_time_max)
df

library(ggplot2)
g <- ggplot(df) +
  geom_boxplot(aes(x=factor(throughput),ymax = response_time_max,upper = response_time_90,
                   y = response_time_med,
                   middle = response_time_med, 
                   lower = response_time_10, 
                   ymin = response_time_min), stat = "identity")
g

但是现在当我要应用ggplotly(g)时,图形无法正确呈现。我该怎么做才能使这项工作成功?

1 个答案:

答案 0 :(得分:0)

我认为不能做到90%和10%。假设它们分别是q3和q1,下面的代码

bp <- plot_ly(color=c("orange")) %>% 
    add_trace(lowerfence = response_time_min, q1 = response_time_10, 
              median = response_time_med, q3 = response_time_90, 
              upperfence = response_time_max, type = "box") %>% 
    layout(xaxis=list(title="throughput"),
           yaxis=list(title="response_time"))
  bp

给出以下输出:

output