R IN ACTION SELF-TUTORIAL-42 用plotly实现带误差棒的柱状图Bar Chart with Error Bars的绘制 2020-07-13

1 首先放一下最终的结果


image.png

2具体实现过程:
2.1数据
用的是plotly中的ToothGrowth数据,格式如下:


image.png

代码实现为:

#devtools::install_github("ropensci/plotly")#如果没安装的话  先安装
library(plotly)
library(plyr)

data_mean <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = mean(len))
data_sd <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = sd(len))
data <- data.frame(data_mean, data_sd$length)
data <- rename(data, c("data_sd.length" = "sd"))
data$dose <- as.factor(data$dose)

fig <- plot_ly(data = data[which(data$supp == 'OJ'),], x = ~dose, y = ~length, type = 'bar', name = 'OJ',
               error_y = ~list(array = sd,
                               color = '#000000'))
fig <- fig %>% add_trace(data = data[which(data$supp == 'VC'),], name = 'VC')

fig

得到的结果为


image.png

你可能感兴趣的:(R IN ACTION SELF-TUTORIAL-42 用plotly实现带误差棒的柱状图Bar Chart with Error Bars的绘制 2020-07-13)