如何从直方图中的标签中删除 0 值
How to remove 0 values from labels in a histogram plot
我正在尝试创建一个样本大小高于条形的直方图,但我想删除 0 以稍微清理一下图。
数据代码如下所示:
ChiSumxE17 <- structure(list(repID = c("WE1_1_epilimnion_2017-08-09", "WE1_1_epilimnion_2017-09-19",
"WE1_1_epilimnion_2017-10-03", "WE1_2_epilimnion_2017-08-09",
"WE1_2_epilimnion_2017-09-19", "WE1_2_epilimnion_2017-10-03",
"WE1_3_epilimnion_2017-08-09", "WE1_3_epilimnion_2017-09-19",
"WE1_3_epilimnion_2017-10-03", "WE1_4_epilimnion_2017-08-09",
"WE1_4_epilimnion_2017-10-03", "WE1_5_epilimnion_2017-09-19",
"WE1_5_epilimnion_2017-10-03", "WE1_6_epilimnion_2017-08-09",
"WE1_6_epilimnion_2017-09-19", "WE1_6_epilimnion_2017-10-03",
"WE2_1_epilimnion_2017-08-09", "WE2_1_epilimnion_2017-09-19",
"WE2_2_epilimnion_2017-08-09", "WE2_2_epilimnion_2017-09-19",
"WE2_3_epilimnion_2017-08-09", "WE2_3_epilimnion_2017-09-19",
"WE2_4_epilimnion_2017-08-09", "WE2_4_epilimnion_2017-09-19",
"WE2_5_epilimnion_2017-08-09", "WE2_5_epilimnion_2017-09-19",
"WE2_6_epilimnion_2017-08-09", "WE2_6_epilimnion_2017-09-19"),
lakeID = c("WE1", "WE1", "WE1", "WE1", "WE1", "WE1", "WE1",
"WE1", "WE1", "WE1", "WE1", "WE1", "WE1", "WE1", "WE1", "WE1",
"WE2", "WE2", "WE2", "WE2", "WE2", "WE2", "WE2", "WE2", "WE2",
"WE2", "WE2", "WE2"), date = structure(c(1502236800, 1505779200,
1506988800, 1502236800, 1505779200, 1506988800, 1502236800,
1505779200, 1506988800, 1502236800, 1506988800, 1505779200,
1506988800, 1502236800, 1505779200, 1506988800, 1502236800,
1505779200, 1502236800, 1505779200, 1502236800, 1505779200,
1502236800, 1505779200, 1502236800, 1505779200, 1502236800,
1505779200), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
stationID = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6,
6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6), depth = c("epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion"), year = c(2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L), sumlength = c(28.1232499499803,
62.3709160934068, 28.4968907448127, 24.4057755479636, 31.4329869922317,
28.0611209435476, 89.6540294391291, 12.2182024093418, 11.8956786930396,
7.4504084127609, 7.97497386020496, 54.8934607962447, 6.17181689309221,
33.9145003867053, 65.7717809443488, 24.2804286931202, 13.866815347461,
15.5064492823138, 32.3986830679647, 36.1816798016504, 13.1493445281917,
17.9310288076699, 8.39065851778099, 5.87433082768543, 15.6459675913619,
9.88419817353383, 15.6459675913619, 24.9474046067361)), row.names = c(1L,
2L, 3L, 41L, 42L, 43L, 76L, 77L, 78L, 105L, 106L, 132L, 133L,
170L, 171L, 172L, 203L, 204L, 239L, 240L, 271L, 272L, 299L, 300L,
331L, 332L, 372L, 373L), class = "data.frame")
我现在的剧情代码是这样的:
hist(ChiSumxE17$sumlength, breaks = 10, xlim = c(0,100), ylim = c(0,32), xlab = "Length (mm)", main = "", labels = TRUE)
剧情是这样的
https://i.stack.imgur.com/OJXqr.png
关于如何删除 0 的任何想法?
您可能正在寻找条形图(经常与直方图混淆)。比较:
set.seed(246635)
x <- (sample(0:9, 20, replace=TRUE))
hist(x, breaks = 10, labels = TRUE)
tbl <- table(x)
b <- barplot(tbl, ylim=c(0, 4.5))
text(b, tbl + .125)
您可以在 text
命令中为您的柱形标签创建一个 ifelse
。您可以使用以下代码:
h <- hist(ChiSumxE17$sumlength, breaks = 10, xlim = c(0,100), ylim = c(0,32), xlab = "Length (mm)", main = "")
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
输出:
从图中可以看出,频率为 0 的条形标签已被删除。
我正在尝试创建一个样本大小高于条形的直方图,但我想删除 0 以稍微清理一下图。
数据代码如下所示:
ChiSumxE17 <- structure(list(repID = c("WE1_1_epilimnion_2017-08-09", "WE1_1_epilimnion_2017-09-19",
"WE1_1_epilimnion_2017-10-03", "WE1_2_epilimnion_2017-08-09",
"WE1_2_epilimnion_2017-09-19", "WE1_2_epilimnion_2017-10-03",
"WE1_3_epilimnion_2017-08-09", "WE1_3_epilimnion_2017-09-19",
"WE1_3_epilimnion_2017-10-03", "WE1_4_epilimnion_2017-08-09",
"WE1_4_epilimnion_2017-10-03", "WE1_5_epilimnion_2017-09-19",
"WE1_5_epilimnion_2017-10-03", "WE1_6_epilimnion_2017-08-09",
"WE1_6_epilimnion_2017-09-19", "WE1_6_epilimnion_2017-10-03",
"WE2_1_epilimnion_2017-08-09", "WE2_1_epilimnion_2017-09-19",
"WE2_2_epilimnion_2017-08-09", "WE2_2_epilimnion_2017-09-19",
"WE2_3_epilimnion_2017-08-09", "WE2_3_epilimnion_2017-09-19",
"WE2_4_epilimnion_2017-08-09", "WE2_4_epilimnion_2017-09-19",
"WE2_5_epilimnion_2017-08-09", "WE2_5_epilimnion_2017-09-19",
"WE2_6_epilimnion_2017-08-09", "WE2_6_epilimnion_2017-09-19"),
lakeID = c("WE1", "WE1", "WE1", "WE1", "WE1", "WE1", "WE1",
"WE1", "WE1", "WE1", "WE1", "WE1", "WE1", "WE1", "WE1", "WE1",
"WE2", "WE2", "WE2", "WE2", "WE2", "WE2", "WE2", "WE2", "WE2",
"WE2", "WE2", "WE2"), date = structure(c(1502236800, 1505779200,
1506988800, 1502236800, 1505779200, 1506988800, 1502236800,
1505779200, 1506988800, 1502236800, 1506988800, 1505779200,
1506988800, 1502236800, 1505779200, 1506988800, 1502236800,
1505779200, 1502236800, 1505779200, 1502236800, 1505779200,
1502236800, 1505779200, 1502236800, 1505779200, 1502236800,
1505779200), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
stationID = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6,
6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6), depth = c("epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion", "epilimnion", "epilimnion", "epilimnion",
"epilimnion", "epilimnion"), year = c(2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L), sumlength = c(28.1232499499803,
62.3709160934068, 28.4968907448127, 24.4057755479636, 31.4329869922317,
28.0611209435476, 89.6540294391291, 12.2182024093418, 11.8956786930396,
7.4504084127609, 7.97497386020496, 54.8934607962447, 6.17181689309221,
33.9145003867053, 65.7717809443488, 24.2804286931202, 13.866815347461,
15.5064492823138, 32.3986830679647, 36.1816798016504, 13.1493445281917,
17.9310288076699, 8.39065851778099, 5.87433082768543, 15.6459675913619,
9.88419817353383, 15.6459675913619, 24.9474046067361)), row.names = c(1L,
2L, 3L, 41L, 42L, 43L, 76L, 77L, 78L, 105L, 106L, 132L, 133L,
170L, 171L, 172L, 203L, 204L, 239L, 240L, 271L, 272L, 299L, 300L,
331L, 332L, 372L, 373L), class = "data.frame")
我现在的剧情代码是这样的:
hist(ChiSumxE17$sumlength, breaks = 10, xlim = c(0,100), ylim = c(0,32), xlab = "Length (mm)", main = "", labels = TRUE)
剧情是这样的 https://i.stack.imgur.com/OJXqr.png
关于如何删除 0 的任何想法?
您可能正在寻找条形图(经常与直方图混淆)。比较:
set.seed(246635)
x <- (sample(0:9, 20, replace=TRUE))
hist(x, breaks = 10, labels = TRUE)
tbl <- table(x)
b <- barplot(tbl, ylim=c(0, 4.5))
text(b, tbl + .125)
您可以在 text
命令中为您的柱形标签创建一个 ifelse
。您可以使用以下代码:
h <- hist(ChiSumxE17$sumlength, breaks = 10, xlim = c(0,100), ylim = c(0,32), xlab = "Length (mm)", main = "")
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
输出:
从图中可以看出,频率为 0 的条形标签已被删除。