Function

コメント・参照等
  • plotlyでチャートを作成する関数

FUN = f_plotly.r
function (datadf, col_x = 1, col_y = 2, col_y2 = NULL, y2_color = 6, col_text = NULL, type = c("scatter", "bar", "box", "heatmap", "histogram", "histogram2d", "histogram2dcontour", "pie", "contour", "scatterternary", "violin", "scatter3d", "surface", "mesh3d", "cone", "streamtube", "scattergeo", "choropleth", "scattergl", "splom", "pointcloud", "heatmapgl", "parcoords", "scattermapbox", "sankey", "table", "carpet", "scattercarpet", "contourcarpet", "ohlc", "candlestick", "scatterpolar", "scatterpolargl", 
    "area"), mode = "lines+markers", line_width = 1, maintitle = NULL, xaxistitle = NULL, yaxistitle = NULL, showlegend = T, hoverstyle = NULL, legend_x = 0, legend_y = 1, x_tickangle = 0, y_tickangle = 0, color_palette, maker_size = 5) 
{
    type <- match.arg(type)
    titlefont <- list(size = 13, family = "Meiryo", color = "Black")
    tickfont <- list(family = "Meiryo", size = 13, color = "black")
    title <- list(title = maintitle, titlefont = "Meiryo", size = 13, color = "Black")
    xaxis <- list(title = xaxistitle, titlefont = "Meiryo", size = 13, color = "Black", showticklabels = T, tickangle = x_tickangle, tickfont = tickfont)
    yaxis <- list(title = yaxistitle, titlefont = "Meiryo", size = 13, color = "Black", showticklabels = T, tickangle = y_tickangle, tickfont = tickfont)
    lydata <- plot_ly(x = datadf[, col_x])
    cnt <- 1
    for (ccc in col_y) {
        if (type == "bar") {
            marker_color <- ifelse(0 < datadf[, ccc], "#4169e1", "#db7093")
        }
        else {
            marker_color <- color_palette[cnt]
        }
        if (is.null(col_text)) {
            lydata <- lydata %>% add_trace(y = datadf[, ccc], name = colnames(datadf)[ccc], type = type, mode = mode, line = list(width = line_width, color = marker_color), marker = list(color = marker_color, size = maker_size))
        }
        else {
            lydata <- lydata %>% add_trace(y = datadf[, ccc], name = colnames(datadf)[ccc], type = type, mode = mode, line = list(width = line_width, color = marker_color), text = datadf[, col_text], marker = list(color = marker_color, size = maker_size))
        }
        cnt <- cnt + 1
    }
    if (!is.null(col_y2)) {
        y2 <- list(overlaying = "y", side = "right")
        lydata <- lydata %>% add_trace(y = datadf[, col_y2], name = colnames(datadf)[col_y2], type = type, mode = mode, line = list(width = line_width, color = color_palette[y2_color]), marker = list(color = color_palette[y2_color], size = maker_size), yaxis = "y2") %>% layout(yaxis2 = y2)
    }
    lydata <- lydata %>% layout(title = maintitle, titlefont = titlefont, xaxis = xaxis, yaxis = yaxis)
    lydata <- lydata %>% layout(showlegend = showlegend) %>% layout(plot_bgcolor = "rgba(0,0,0,0)") %>% layout(paper_bgcolor = "rgba(0,0,0,0)")
    if (!is.null(hoverstyle)) {
        lydata <- lydata %>% style(hoverinfo = hoverstyle)
    }
    lydata <- lydata %>% layout(legend = list(x = legend_x, y = legend_y))
    return(lydata)
}