如何使用 axis() 为包含 NA 的数据添加自定义轴标签
How to add custom axis labels with axis() for data containing NAs
我想在 R 中创建一个箱形图,我想使用 axis() 添加自定义轴。但是,数据包含 NA,我认为这是导致错误的原因。这是我使用的代码。
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"))
我收到以下错误消息:
In axis(1, at = df$grouping1, labels=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6")) :
NAs introduced by coercion
有没有办法省略 NA?
非常感谢您的帮助。
这是我使用的数据:
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))
这可能会解决问题:
轴刻度线似乎设置为整数,请检查:
boxplot(TAM ~ grouping1, data = df, col = mycolor6, las=1, xaxt = "n", xlab = "", ylab = "", main = ""); axis(1)
因此将 at=df$grouping1
更改为 at = 1:6
似乎可以解决问题。
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"))
由 reprex package (v2.0.1)
于 2021-12-08 创建
我想在 R 中创建一个箱形图,我想使用 axis() 添加自定义轴。但是,数据包含 NA,我认为这是导致错误的原因。这是我使用的代码。
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"))
我收到以下错误消息:
In axis(1, at = df$grouping1, labels=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6")) : NAs introduced by coercion
有没有办法省略 NA?
非常感谢您的帮助。
这是我使用的数据:
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))
这可能会解决问题:
轴刻度线似乎设置为整数,请检查:
boxplot(TAM ~ grouping1, data = df, col = mycolor6, las=1, xaxt = "n", xlab = "", ylab = "", main = ""); axis(1)
因此将 at=df$grouping1
更改为 at = 1:6
似乎可以解决问题。
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"))
由 reprex package (v2.0.1)
于 2021-12-08 创建