Help With Removing NA values from Count on Boxplot

Hello would anyone be able to help me figure out how to remove NA values from the count of values used to make a boxplot?

Loxo <- ggplot(Ch2_Raw_Data, aes(x=Species, y=Loxo_Prev)) +
  geom_boxplot(alpha = 0.80) + geom_boxplot(fill="gray")+  
  stat_boxplot(geom ='errorbar', width=.10) +
  labs(title="Loxothylacus panopaei Infection Prevalence \n in Eurypanopeus depressus and \n Rhithropanopeus harrisii",x="Species", y = "L. panopaei Infection Prevalence")+
  theme_classic()  +theme(plot.title = element_text(hjust = 0.5)) + 
  geom_boxplot(lwd=0.1) + geom_boxplot(fatten=2) +  
  stat_summary(geom = "errorbar", fun.min = mean, fun = mean, fun.max = mean, width = .75, linetype = "dashed") +
  scale_x_discrete(limits = c("EURY", "RHITHRO"), 
                   labels = c("E. depressus", "R. harrisii"))


Loxo +                                        # Add counts by group to boxplot
  annotate("text",
          x = 1:length(table(Ch2_Raw_Data$Species)),
          y = aggregate(Loxo_Prev ~ Species, Ch2_Raw_Data, median)[ , 2],
          label = table(Ch2_Raw_Data$Species),
          col = "black",
          size = 3,
          vjust = 1.5)

Currently it is including all possible lines of data that have either EURY or RHITHRO as the species (ie all values) however I did not dissect Juveniles and so the prevalence data for some of the EURY and RHITHRO is NA instead of a number value. How would I get R to give an accurate count of how many values are getting used in the plot per group?

THANK YOU!

na.omit may do what you want. It will remove any row with missing data. Depending on how your data looks this can work well or be the the equivalent of using a sledge hammer to shell an almond. It might help if we saw a bit of your data.

new_data  <- na.omit(Ch2_Raw_Data)

See FAQ

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

1 Like
 ID        Old_ID Species    Site Month_Collected Year_Collected Day_Collected Crate_ID Size  Sex Tissue_Type Number_Slides
1      RHPOCOCT16-1 RHITHRO Pin_Oak         October           2016            30       P8  8.4 <NA>          HG             1
2      RHPOCOCT16-2 RHITHRO Pin_Oak         October           2016            30       P8  6.3    M          HG             1
3      RHPOCOCT16-3 RHITHRO Pin_Oak         October           2016            30       P8  5.3    M          HG             1
4      RHPOCOCT16-4 RHITHRO Pin_Oak         October           2016            30       P8  8.5    M          HG             1
5      RHPOCOCT16-5 RHITHRO Pin_Oak         October           2016            30       P8  8.8    M          HG             1
6      RHPOCOCT16-6 RHITHRO Pin_Oak         October           2016            30       P8  5.3    F        <NA>             0
7      RHPOCOCT16-7 RHITHRO Pin_Oak         October           2016            30       P8  5.3    F          HG             1
8      RHPOCOCT16-8 RHITHRO Pin_Oak         October           2016            30       P8  4.5    J        <NA>             0
9      RHPOCOCT16-9 RHITHRO Pin_Oak         October           2016            30       P8  4.4    J        <NA>             0
10    RHPOCOCT16-10 RHITHRO Pin_Oak         October           2016            30       P8  5.1 <NA>          HG             1
11    RHPOCOCT16-11 RHITHRO Pin_Oak         October           2016            30       P8    5    M        <NA>             0
12    RHPOCOCT16-12 RHITHRO Pin_Oak         October           2016            30       P8  6.5    F          HG             1
13    RHPOCOCT16-13 RHITHRO Pin_Oak         October           2016            30       P8  8.1    F          HG             1
14     RHPOCDEC16-1 RHITHRO Pin_Oak        December           2016            17       N8  7.5    F          HG             1
15     RHPOCDEC16-2 RHITHRO Pin_Oak        December           2016            17       N8  5.5    F          HG             1
16     RHPOCDEC16-3 RHITHRO Pin_Oak        December           2016            17       N8  7.3    M          HG             1
17     RHPOCDEC16-4 RHITHRO Pin_Oak        December           2016            17       N8  5.1    M          HG             1
18     RHPOCDEC16-5 RHITHRO Pin_Oak        December           2016            17       N8  4.1    J        <NA>             0
19     RHPOCDEC16-6 RHITHRO Pin_Oak        December           2016            17       N8  7.4    F          HG             1
20     RHPOCDEC16-7 RHITHRO Pin_Oak        December           2016            17       N8  8.7    F          HG             1
21     RHPOCDEC16-8 RHITHRO Pin_Oak        December           2016            17       N8    4    J        <NA>             0
22     RHPOCDEC16-9 RHITHRO Pin_Oak        December           2016            17       N8  8.2    M        <NA>             0
23    RHPOCDEC16-10 RHITHRO Pin_Oak        December           2016            17       N8    8 <NA>          HG             1
24    RHPOCDEC16-11 RHITHRO Pin_Oak        December           2016            17       N8  7.2 <NA>          HG             1
25    RHPOCDEC16-12 RHITHRO Pin_Oak        December           2016            17       N8  9.2 <NA>          HG             1
26    RHPOCDEC16-13 RHITHRO Pin_Oak        December           2016            17       N8 11.1    F          HG             1
27    RHPOCDEC16-14 RHITHRO Pin_Oak        December           2016            17       N8 11.3 <NA>          HG             1
28     EDPOCDEC16-1    EURY Pin_Oak        December           2016            17       N8  5.6    M          HG             1
29      RHPODEC16-1 RHITHRO Pin_Oak        December           2016            17       N7   12    M          HG             1
   Loxo Entonicid Nematode Trematode Cestode Acanthocephalan Microspordian Loxo_Prev Ent_Prev Nem_Prev Trem_Prev Ces_Prev
1     1         0        0         0       0               0             0 0.1111111        0        0         0        0
2     0         0        0         0       0               0             0 0.0000000        0        0         0        0
3     0         0        0         0       0               0             0 0.0000000        0        0         0        0
4     0         0        0         0       0               0             0 0.0000000        0        0         0        0
5     0         0        0         0       0               0             0 0.0000000        0        0         0        0
6    NA        NA       NA        NA      NA              NA            NA        NA       NA       NA        NA       NA
7     0         0        0         0       0               0             0 0.0000000        0        0         0        0
8    NA        NA       NA        NA      NA              NA            NA        NA       NA       NA        NA       NA
9    NA        NA       NA        NA      NA              NA            NA        NA       NA       NA        NA       NA
10    1         0        0         0       0               0             0 0.1111111        0        0         0        0
11   NA        NA       NA        NA      NA              NA            NA        NA       NA       NA        NA       NA
12    0         0        0         0       0               0             0 0.0000000        0        0         0        0
13    0         0        0         0       0               0             0 0.0000000        0        0         0        0
14    0         0        0         0       0               0             0 0.0000000        0        0         0        0
15    0         0        0         0       0               0             0 0.0000000        0        0         0        0
16    0         0        0         0       0               0             0 0.0000000        0        0         0        0
17    0         0        0         0       0               0             0 0.0000000        0        0         0        0
18   NA        NA       NA        NA      NA              NA            NA        NA       NA       NA        NA       NA
19    0         0        0         0       0               0             0 0.0000000        0        0         0        0
20    0         0        0         0       0               0             0 0.0000000        0        0         0        0
21   NA        NA       NA        NA      NA              NA            NA        NA       NA       NA        NA       NA
22   NA        NA       NA        NA      NA              NA            NA        NA       NA       NA        NA       NA
23    1         0        0         0       0               0             0 0.0400000        0        0         0        0
24    1         0        0         0       0               0             0 0.0400000        0        0         0        0
25    1         0        0         0       0               0             0 0.0400000        0        0         0        0
26    0         0        0         0       0               0             0 0.0000000        0        0         0        0
27    1         0        0         0       0               0             0 0.0400000        0        0         0        0
28    0         0        0         0       0               0             0 0.0000000        0        0         0        0
29    0         0        0         0       0               0             0 0.0000000        0        0         0        0
   Acan_Prev Micro_Prev Total_Richness Total_Prevalence Microplastics      Notes X   SpeciesxSite    SpeciesxSitexYearxMonth
1          0          0              1        0.1111111             0              RHITHROPin_Oak  RHITHROPin_Oak2016October
2          0          0              0        0.0000000             0              RHITHROPin_Oak  RHITHROPin_Oak2016October
3          0          0              0        0.0000000             0              RHITHROPin_Oak  RHITHROPin_Oak2016October
4          0          0              0        0.0000000             0              RHITHROPin_Oak  RHITHROPin_Oak2016October
5          0          0              0        0.0000000             0              RHITHROPin_Oak  RHITHROPin_Oak2016October
6         NA         NA             NA               NA            NA  no tissue   RHITHROPin_Oak  RHITHROPin_Oak2016October
7          0          0              0        0.0000000             0              RHITHROPin_Oak  RHITHROPin_Oak2016October
8         NA         NA             NA               NA            NA  juvenile    RHITHROPin_Oak  RHITHROPin_Oak2016October
9         NA         NA             NA               NA            NA  juvenile    RHITHROPin_Oak  RHITHROPin_Oak2016October
10         0          0              1        0.1111111             0              RHITHROPin_Oak  RHITHROPin_Oak2016October
11        NA         NA             NA               NA            NA no tissue    RHITHROPin_Oak  RHITHROPin_Oak2016October
12         0          0              0        0.0000000             0              RHITHROPin_Oak  RHITHROPin_Oak2016October
13         0          0              0        0.0000000             0              RHITHROPin_Oak  RHITHROPin_Oak2016October
14         0          0              0        0.0000000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
15         0          0              0        0.0000000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
16         0          0              0        0.0000000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
17         0          0              0        0.0000000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
18        NA         NA             NA               NA            NA  juvenile    RHITHROPin_Oak RHITHROPin_Oak2016December
19         0          0              0        0.0000000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
20         0          0              0        0.0000000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
21        NA         NA             NA               NA            NA  juvenile    RHITHROPin_Oak RHITHROPin_Oak2016December
22        NA         NA             NA               NA            NA no tissue    RHITHROPin_Oak RHITHROPin_Oak2016December
23         0          0              1        0.0400000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
24         0          0              1        0.0400000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
25         0          0              1        0.0400000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
26         0          0              0        0.0000000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
27         0          0              1        0.0400000             0              RHITHROPin_Oak RHITHROPin_Oak2016December
28         0          0              0        0.0000000             0                 EURYPin_Oak    EURYPin_Oak2016December
29         0          0              0        0.0000000             0              RHITHROPin_Oak RHITHROPin_Oak2016December

Does this help? That is also a very helpful function thank you!

Some but it would be much better to have the data in dput() format. As it is I cannot read the data into R. The data seems to be in three blocks and the third block with the variable names " Acan_Prev Micro_Prev Total_Richness Total_Prevalence, ..." is garbled.

It would be much better to have dput() output. There is an example below of how to do it.

Just going on the data I have managed to import you have a lot of missing data. Applying na.omit to the entire data set might leave you with N = 1

I think this may give you something a bit like what you want where we only drop NA's in the variables being plotted.

Loxo <- ggplot(dat1, aes(x=Species, y=Loxo_Prev)) + geom_boxplot(na.rm = TRUE) +  geom_jitter()

In this case a boxplot is not going to tell us anything since you only have 4 values for Loxo_Prev.

> table(dat1$Loxo_Prev,  useNA =  "ifany")

        0      0.04 0.1111111      <NA> 
       16         4         2         7 

If the data set ins larger, ignore this comment.

Sample Data

dat1  <- structure(list(ID = 1:29, Old_ID = c("RHPOCOCT16-1", "RHPOCOCT16-2", 
"RHPOCOCT16-3", "RHPOCOCT16-4", "RHPOCOCT16-5", "RHPOCOCT16-6", 
"RHPOCOCT16-7", "RHPOCOCT16-8", "RHPOCOCT16-9", "RHPOCOCT16-10", 
"RHPOCOCT16-11", "RHPOCOCT16-12", "RHPOCOCT16-13", "RHPOCDEC16-1", 
"RHPOCDEC16-2", "RHPOCDEC16-3", "RHPOCDEC16-4", "RHPOCDEC16-5", 
"RHPOCDEC16-6", "RHPOCDEC16-7", "RHPOCDEC16-8", "RHPOCDEC16-9", 
"RHPOCDEC16-10", "RHPOCDEC16-11", "RHPOCDEC16-12", "RHPOCDEC16-13", 
"RHPOCDEC16-14", "EDPOCDEC16-1", "RHPODEC16-1"), Species = c("RHITHRO", 
"RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", 
"RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", 
"RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", 
"RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", "RHITHRO", 
"RHITHRO", "RHITHRO", "EURY", "RHITHRO"), Site = c("Pin_Oak", 
"Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", 
"Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", 
"Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", 
"Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak", 
"Pin_Oak", "Pin_Oak", "Pin_Oak", "Pin_Oak"), Month_Collected = c("October", 
"October", "October", "October", "October", "October", "October", 
"October", "October", "October", "October", "October", "October", 
"December", "December", "December", "December", "December", "December", 
"December", "December", "December", "December", "December", "December", 
"December", "December", "December", "December"), Year_Collected = c(2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L), Day_Collected = c(30L, 30L, 30L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L, 30L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L), Crate_ID = c("P8", 
"P8", "P8", "P8", "P8", "P8", "P8", "P8", "P8", "P8", "P8", "P8", 
"P8", "N8", "N8", "N8", "N8", "N8", "N8", "N8", "N8", "N8", "N8", 
"N8", "N8", "N8", "N8", "N8", "N7"), Size = c(8.4, 6.3, 5.3, 
8.5, 8.8, 5.3, 5.3, 4.5, 4.4, 5.1, 5, 6.5, 8.1, 7.5, 5.5, 7.3, 
5.1, 4.1, 7.4, 8.7, 4, 8.2, 8, 7.2, 9.2, 11.1, 11.3, 5.6, 12), 
    Sex = c("<NA>", "M", "M", "M", "M", "F", "F", "J", "J", "<NA>", 
    "M", "F", "F", "F", "F", "M", "M", "J", "F", "F", "J", "M", 
    "<NA>", "<NA>", "<NA>", "F", "<NA>", "M", "M"), Tissue_Type = c("HG", 
    "HG", "HG", "HG", "HG", "<NA>", "HG", "<NA>", "<NA>", "HG", 
    "<NA>", "HG", "HG", "HG", "HG", "HG", "HG", "<NA>", "HG", 
    "HG", "<NA>", "<NA>", "HG", "HG", "HG", "HG", "HG", "HG", 
    "HG"), Number_Slides = c(1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 
    0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), Loxo = c(1L, 0L, 0L, 0L, 0L, NA, 
    0L, NA, NA, 1L, NA, 0L, 0L, 0L, 0L, 0L, 0L, NA, 0L, 0L, NA, 
    NA, 1L, 1L, 1L, 0L, 1L, 0L, 0L), Entonicid = c(0L, 0L, 0L, 
    0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 0L, NA, 
    0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Nematode = c(0L, 
    0L, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 
    0L, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Trematode = c(0L, 
    0L, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 
    0L, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Cestode = c(0L, 
    0L, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 
    0L, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Acanthocephalan = c(0L, 
    0L, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 
    0L, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Microspordian = c(0L, 
    0L, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 
    0L, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Loxo_Prev = c(0.1111111, 
    0, 0, 0, 0, NA, 0, NA, NA, 0.1111111, NA, 0, 0, 0, 0, 0, 
    0, NA, 0, 0, NA, NA, 0.04, 0.04, 0.04, 0, 0.04, 0, 0), Ent_Prev = c(0L, 
    0L, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 
    0L, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Nem_Prev = c(0L, 
    0L, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 
    0L, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Trem_Prev = c(0L, 
    0L, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 
    0L, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Ces_Prev = c(0L, 
    0L, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, NA, 0L, 0L, 0L, 0L, 0L, 
    0L, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(NA, 
-29L), class = "data.frame")

Example dput()

dat <- data.frame(xx = 1:10, yy = letters[1:10])

dput(dat)

This gives us

structure(list(xx = 1:10, yy = c("a", "b", "c", "d", "e", "f", 
                                 "g", "h", "i", "j")), class = "data.frame", row.names = c(NA,  -10L))

We can then copy it into R

my_dat  <- structure(list(xx = 1:10, yy = c("a", "b", "c", "d", "e", "f", 
                                            "g", "h", "i", "j")), class = "data.frame", row.names = c(NA,  -10L))

and we have an exact copy of your data.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.