Home > Enterprise >  How to remove 0 values from labels in a histogram plot
How to remove 0 values from labels in a histogram plot

Time:04-06

I'm trying to create a histogram plot with sample size above the bars but i would like to remove the 0s to clean up the plots a bit.

The data code looks like this:

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")

My current plot code looks like this:

hist(ChiSumxE17$sumlength, breaks = 10, xlim = c(0,100), ylim = c(0,32), xlab = "Length (mm)", main = "", labels = TRUE)

and the plot looks like this enter image description here

tbl <- table(x)
b <- barplot(tbl, ylim=c(0, 4.5))
text(b, tbl   .125)

enter image description here

CodePudding user response:

You can create an ifelse to the labels of your bars in the text command. You can use the following code:

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))

Output:

enter image description here

As you can see from the plot, the labels of the bars with 0 frequency are removed.

  • Related