3. 2016.8.29 R boxplot in group

3.1. Sample Code

  • Draw a boxplot in groups with legends and customize xtick

3.2. Draw boxplot in group

boxplot(c(1:3), c(2:4), c(3:5), 
    main = 'Group Plot Boxplot',        # Title
    boxwex = 0.25,                      # Width
    at = 1:3 - 0.2,                     # Plot position
    col='yellow',                       # Color
    xlim = c(0.5, 3.5),ylim = c(0.5, 6),# x,y limits
    outline = FALSE                     # outlier not drawn
    )
boxplot(c(1:4), c(2:5), c(3:6), 
    boxwex = 0.25,                      # Width
    at = 1:3 + 0.2 ,                    # Plot position
    add = TRUE,                         # Plot on top of the previous
    col = 'orange',                     # Color
    outline = FALSE                     # outlier not drawn
    )

## Alter Tick to Text
axis(1,c(1,2,3),labels=c("1st feature","2nd feature","3rd feature"))

## Add Legends
legend(
    2.8, 2,                             # Plot Position
    c("A group", "B group"),            # Label
    fill = c("yellow", "orange")        # Color
)