Function

コメント・参照等
  • plotlyで水平タイプのバーチャートを表示する関数

FUN = f_plotly_horizontal_bar.r
function (obj0, col_x = 2, col_y = 1, round_digits = 2, xaxistitle = NULL, yaxistitle = NULL, x_tickangle = 0, y_tickangle = 0, x_tickfontsize = 13, y_tickfontsize = 11, x_titlefontsize = 13, y_titlefontsize = 13, maintitle_add = NULL) 
{
    obj <- obj0[, c(col_x, col_y)]
    maintitle0 <- paste0(maintitle_add, colnames(obj)[1])
    colnames(obj) <- c("value", "item")
    obj <- obj[order(obj$value), ]
    obj$item <- factor(x = obj$item, levels = obj$item)
    marker_color <- ifelse(obj$value < 0, "#dc143c", "#b0c4de")
    titlefont <- list(size = 13, family = "Meiryo", color = "Black")
    x_tickfont <- list(family = "Meiryo", size = x_tickfontsize, color = "black")
    y_tickfont <- list(family = "Meiryo", size = y_tickfontsize, color = "black")
    xaxis <- list(title = xaxistitle, titlefont = "Meiryo", size = x_titlefontsize, color = "Black", showticklabels = T, tickangle = x_tickangle, tickfont = x_tickfont)
    yaxis <- list(title = yaxistitle, titlefont = "Meiryo", size = y_titlefontsize, color = "Black", showticklabels = T, tickangle = y_tickangle, tickfont = y_tickfont)
    maintitle <- paste0(maintitle0, "\nPositive:", sum(obj$value > 0), ",Negative:", sum(obj$value < 0), ",Zero:", sum(obj$value == 0))
    plotlydata <- plot_ly(data = obj, x = obj$value) %>% add_trace(y = obj$item, type = "bar", marker = list(color = marker_color), hoverinfo = "y") %>% add_text(x = 0, y = obj$item, text = format(f_round_not_IEEE754(obj$value, digits = round_digits)), textposition = "right", hoverinfo = "none") %>% layout(showlegend = F, title = maintitle, titlefont = titlefont, plot_bgcolor = "rgba(0,0,0,0)", paper_bgcolor = "rgba(0,0,0,0)", xaxis = xaxis, yaxis = yaxis)
    return(plotlydata)
}