-2

I have a dataset with several pathogens. I want to create a bar-graph that lists the pathogens on the x-axis with 3 bars "Negative" "Positive" and "NA" (which are listed in the dataset) and count on the y-axis. I am successful in creating a bar-graph for a single pathogen with the following code, but I cannot figure out how to get all the pathogens on a single graph. Help would be appreciated.

For single pathogen:

countST=table(NetJoint$`sT-ETEC`)  
barplot(countST)

My attempt for multiple pathogens:

ETECcounts=table(NetJoint$`LT-ETEC`, NetJoint$`sT-ETEC`)  
barplot(ETECcounts, main="Pathogen Distribution",  
        xlab="Pathogen", beside=TRUE) 

^^This code lists "NA", "Negative" and "Positive" on the x-axis instead of the pathogens.

Mr. Discuss
  • 355
  • 1
  • 4
  • 13
Katie
  • 11
  • 2

1 Answers1

1

It may help us if you share some of your data. But I think a solution would be something like this:

# create an example dataframe
NetJoint = data.frame(
  'LT-ETEC' = c('Positive', 'Positive', 'Negative', 'Positive', NA, 'Positive', NA),
  'sT-ETEC' = c(NA, 'Negative', 'Negative', NA, 'Positive', NA, 'Positive')
)
# create a table for each variable of the dataframe
ETECcounts = sapply(NetJoint, table, useNA = 'always')
# plot base layer
barplot(ETECcounts, main = 'Pathogen Distribution',  
        xlab = 'Pathogen', beside = TRUE, col = c('orange', 'darkgreen', 'grey'))
# add legend layer
legend('topright', fill = c('orange', 'darkgreen', 'grey'),
       legend = c('Positive', 'Negative', 'NA'))

Here is the output:

enter image description here

Let me know if this is what you're looking for.

rodolfoksveiga
  • 1,181
  • 4
  • 17