Home > Mobile >  How to add custom axis labels with axis() for data containing NAs
How to add custom axis labels with axis() for data containing NAs

Time:12-08

I would like to create a boxplot in R, and I would like to add a custom axis using axis(). However, the data contains NA, which I think caused errors. Here is the code that I used.

mycolor6 <- c(#E74C3C", "#8E44AD", "#2980B9", "#1ABC9C", "#6495ED", "#922B21)
boxplot(TAM ~ grouping1, data = df, col=mycolor6, las=1, xaxt="n", xlab = "", ylab = "", main = "")
axis(1, at=df$grouping1, labels=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6"))

I got the following error message:

In axis(1, at = df$grouping1, labels=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6")) : NAs introduced by coercion

Is there a way to omit NAs?

Thanks a lot for your help.

This is the data that I used:

df <- structure(list(TAM = c(4, 3.76190476190476, 5, 3.38095238095238, 
                       3.19047619047619, 4.19047619047619, 3, 4.14285714285714, 4.14285714285714, 
                       4, 3.33333333333333, 4.66666666666667, 2.76190476190476, 4.09523809523809, 
                       3.71428571428571, 3.66666666666667, 4.71428571428571, 3.76190476190476, 
                       3.57142857142857, 4.38095238095238, 3.38095238095238, 4.80952380952381, 
                       4, 4.76190476190476, 2.38095238095238, 4, 4.33333333333333, 3.38095238095238, 
                       3, 4.90476190476191, 3.04761904761905, 4, 4.61904761904762, 3.19047619047619, 
                       3.0952380952381, 4.38095238095238, 4.23809523809524, 4.23809523809524, 
                       4.19047619047619, 3.28571428571429, 3.66666666666667, 3.23809523809524, 
                       3.85714285714286, 4.23809523809524, 4.71428571428571, 4.23809523809524, 
                       4.47619047619048, 3.76190476190476, 2.80952380952381, 3.04761904761905, 
                       2.80952380952381, 3.9047619047619, 4.14285714285714, 4, 4.19047619047619, 
                       3.19047619047619, 3.42857142857143, 4, 4, 4.19047619047619, 4.76190476190476, 
                       5, 4, 4.04761904761905, 4.14285714285714, 4.38095238095238, 4.23809523809524, 
                       3, 4.90476190476191, 3.95238095238095, 5, 2.80952380952381, 3.76190476190476, 
                       4, 3.80952380952381, 2.95238095238095, 3.42857142857143, 3.80952380952381, 
                       3.57142857142857, 4.09523809523809, 3.80952380952381, 5, 3.66666666666667, 
                       4.33333333333333, 4.33333333333333, 4, 4.28571428571429, 4.23809523809524, 
                       4.19047619047619, 4.28571428571429, 3, 4.47619047619048, 5, 4.85714285714286, 
                       4.71428571428571, 3.85714285714286, 4.52380952380952, 2.9047619047619, 
                       2.38095238095238, 4, 4.52380952380952, 3.71428571428571, 3, 3.80952380952381, 
                       3.61904761904762, 2.95238095238095, 3.61904761904762, 3.19047619047619, 
                       2.0952380952381, 3.66666666666667, 4, 4.04761904761905, 4.57142857142857, 
                       4.23809523809524, 4.33333333333333, 4.71428571428571, 1.95238095238095, 
                       2.04761904761905, 4.38095238095238, 5, 4, 3, 2.85714285714286, 
                       2.23809523809524, 4.23809523809524, 4.38095238095238, 3.80952380952381, 
                       3.9047619047619, 4.47619047619048, 4.23809523809524, 3.0952380952381, 
                       3.80952380952381, 3.9047619047619, 2.80952380952381, 5, 5, 3.76190476190476, 
                       3.66666666666667, 3.61904761904762, 4, 4.61904761904762, 3.9047619047619, 
                       3.76190476190476, 3.9047619047619, 4, 2.80952380952381, 4, 4.09523809523809, 
                       4, 4, 4.90476190476191, 4, 4.04761904761905, 3.47619047619048, 
                       3.66666666666667, 2.38095238095238, 4.38095238095238, 3.57142857142857, 
                       4, 4.19047619047619, 3.76190476190476, 3.57142857142857, 3.85714285714286, 
                       4.04761904761905, 3.42857142857143, 3.52380952380952, 4, 4, 4.28571428571429, 
                       3.38095238095238, 3.9047619047619, 4.85714285714286, 4, 4, 3.9047619047619, 
                       4.23809523809524, 3.9047619047619, 3.52380952380952, 4.23809523809524, 
                       2.95238095238095, 4, 4, 3.80952380952381, 4, 4.61904761904762, 
                       3.28571428571429, 3.19047619047619, 4.95238095238095, 4.23809523809524, 
                       3, 4, 3, 4, 4, 3.23809523809524, 3.52380952380952, 3.38095238095238, 
                       4, 4.95238095238095, 4.33333333333333, 3.04761904761905, 2.85714285714286, 
                       4.19047619047619, 3.95238095238095, 2.19047619047619, 4.28571428571429, 
                       3.57142857142857, 4, 5, 4.23809523809524, 3, 3.19047619047619, 
                       2.9047619047619, 4.04761904761905, 3.0952380952381, 3.14285714285714, 
                       2.23809523809524, 3.66666666666667, 3.9047619047619, 3.14285714285714, 
                       2.61904761904762, 4, 4.04761904761905, 4, 3.71428571428571, 3, 
                       3.9047619047619, 3.95238095238095, 4.14285714285714, 3.04761904761905, 
                       3.57142857142857, 2.9047619047619, 3.61904761904762), grouping1 = c("group1", 
                                                                                           "group1", "group1", "group2", NA, "group4", "group5", "group1", 
                                                                                           "group1", "group1", "group5", "group1", "group6", NA, "group1", 
                                                                                           "group1", "group1", NA, NA, "group1", "group1", "group1", "group1", 
                                                                                           "group1", "group5", "group1", "group1", "group5", "group8", "group1", 
                                                                                           "group4", "group1", "group1", "group1", "group4", "group1", "group1", 
                                                                                           "group1", "group1", "group1", NA, NA, "group1", "group1", "group1", 
                                                                                           "group6", "group1", "group5", "group5", "group5", NA, "group6", 
                                                                                           "group1", NA, "group4", NA, "group5", NA, NA, "group1", "group1", 
                                                                                           "group1", NA, "group1", "group1", "group1", NA, "group1", "group6", 
                                                                                           "group1", "group1", NA, "group5", "group1", "group2", "group5", 
                                                                                           NA, "group1", "group4", "group1", "group1", "group1", NA, "group6", 
                                                                                           "group1", "group1", "group1", "group1", "group1", "group1", "group5", 
                                                                                           "group1", NA, "group1", "group1", "group1", "group1", "group5", 
                                                                                           "group5", "group1", "group1", "group2", "group5", NA, "group4", 
                                                                                           "group8", "group4", "group8", "group5", "group6", "group1", "group1", 
                                                                                           "group1", "group1", "group1", "group1", "group5", "group5", "group1", 
                                                                                           "group1", "group5", "group5", NA, "group4", "group1", "group1", 
                                                                                           "group1", "group1", "group1", "group1", NA, NA, "group1", NA, 
                                                                                           "group8", "group1", "group1", "group1", "group6", "group1", "group1", 
                                                                                           "group2", NA, "group1", "group5", "group5", "group1", "group1", 
                                                                                           "group4", "group6", "group1", "group1", "group6", "group6", "group5", 
                                                                                           "group1", "group1", "group8", "group1", "group1", NA, "group1", 
                                                                                           "group1", "group1", "group8", NA, "group1", "group1", "group1", 
                                                                                           "group1", "group2", "group1", "group1", "group2", "group8", "group2", 
                                                                                           "group1", "group4", "group5", "group4", "group5", "group1", "group6", 
                                                                                           "group2", "group1", "group8", NA, "group1", "group4", "group5", 
                                                                                           "group2", "group1", "group1", "group1", NA, "group2", "group5", 
                                                                                           "group1", "group1", "group1", "group2", "group4", "group1", "group5", 
                                                                                           NA, "group1", "group6", "group1", "group1", "group8", NA, "group8", 
                                                                                           NA, "group1", NA, "group8", "group1", "group1", "group1", "group2", 
                                                                                           NA, "group1", "group1", "group1", NA, "group2", NA, "group4", 
                                                                                           "group1", "group5", "group4", "group5", "group8")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                   -233L))

CodePudding user response:

This may resolve the issue:

Axis tick marks seem to be set out as integers, check with:
boxplot(TAM ~ grouping1, data = df, col = mycolor6, las=1, xaxt = "n", xlab = "", ylab = "", main = ""); axis(1)

So changing at=df$grouping1 with at = 1:6 seems to do the trick.

mycolor6 <- c("#E74C3C", "#8E44AD", "#2980B9", "#1ABC9C", "#6495ED", "#922B21") # corrected version

  boxplot(TAM ~ grouping1, data = df, col=mycolor6, las=1, xaxt = "n", xlab = "", ylab = "", main = "")
  
  axis(1, at = 1:6, labels = c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6"))

Created on 2021-12-08 by the reprex package (v2.0.1)

  • Related