2018年6月29日 星期五

用ggplot2畫盒鬚圖(boxplot)

下圖是用ggplot2這個套件來畫boxplot

使用的程式碼如下:
speeds <- list("Default" = c(203,182,172,181,185,202,190,186,171,174,185,208,183,194,163),
               "Default\n40c"=c(246,245,242,245),
               "IntelMPI" = c(195,107,196,207,199,198,208,199,202),
               "Cuda8" = c(196,184,167,167,204),
               "AVX512" = c(151,154,177,177,183),
               "E7-8850\n40c" = c(117,115,116,120),
               "E5-2640v4\n1gx" = c(223,225,223,218))
library(plyr)
performance <- ldply(speeds, data.frame)    # convert list to data.frame
names(performance) <- c("Type", "Speed")    # Rename the column of the data frame "performance"

library(ggplot2)
p <- ggplot(performance, aes(x=Type, y=Speed, fill=Type)) +     # Source data is "performance", fill the box  
    labs(title="GROMACS on Taiwania",x="Configuration", y = "Performance (ns/day)") +
    theme(legend.position="none",                                 # Don't show legend
          plot.title=element_text(size=rel(2.0), face="bold"),    # Font of plot title
          axis.title=element_text(size=rel(1.5), face="bold"),    # Font of axis title
          axis.text.x=element_text(size=rel(1.5), angle=0),       # Font of x-axis label
          axis.text.y=element_text(size=rel(1.5), angle=15),      # Font of y-axis label
          axis.line=element_line(size = 3, colour = "grey80")) +  # Axis with gray 
    geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=2) +  # Boxplot, dealting with outlier color, shape and size
    coord_cartesian(ylim = c(0, 250)) +                                       # the range of y-axis
    geom_jitter(shape=20, size=3, position=position_jitter(0.2)) +            # data points show in black dot
    stat_summary(fun.y=mean, geom="point", shape=8, size=3, color="red") +    # mean points shown in red asterisk
    scale_x_discrete(limits=c("Default","Default\n40c","IntelMPI","Cuda8","AVX512","E7-8850\n40c","E5-2640v4\n1gx")) + 
    scale_fill_brewer(palette="Greens")      # the above change the order of x-axis, and using gradient green
p   # Plot the Boxplot!!


參考資料

_EOF_

沒有留言:

張貼留言