Function

コメント・参照等
  • ggplotで散布図を描く関数

FUN = f_ggplot_scatter.r
function (obj, x_col = 1, y_col = 2, color_col = NULL, point_size = 2, lab_title = NULL, lab_subtitle = NULL, default_point_col = "blue", x_breaks = 10, y_breaks = 10, smooth_method = "lm", smoothed_size = 1, smooth_col = "red", need_fit = T, smooth_txt_size = 6) 
{
    color_col_null <- F
    if (is.null(color_col)) {
        color_col <- ncol(obj) + 1
        obj[, color_col] <- default_point_col
        color_col_null <- T
    }
    manual_col <- unique(obj[, color_col])
    data_set <- obj[, c(x_col, y_col, color_col)]
    g <- ggplot(data = data_set, mapping = aes_string(x = colnames(data_set)[1], y = colnames(data_set)[2], col = factor(data_set[, 3], levels = manual_col)))
    g <- g + geom_point(size = point_size)
    g <- g + xlab(label = paste0("X:", colnames(data_set)[1]))
    g <- g + ylab(label = paste0("Y:", colnames(data_set)[2]))
    g <- g + scale_x_continuous(breaks = pretty_breaks(x_breaks), labels = function(x) format(x, big.mark = ",", scientific = F))
    g <- g + scale_y_continuous(breaks = pretty_breaks(y_breaks), labels = function(x) format(x, big.mark = ",", scientific = F))
    g <- g + labs(title = lab_title, subtitle = lab_subtitle)
    if (color_col_null) {
        g <- g + scale_color_manual(values = manual_col)
    }
    return(g)
}