Reputation: 407
I have this data set:
structure(list(sex = structure(c(2L, 1L, 1L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L,
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L,
2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L), levels = c("Male",
"Female"), class = "factor"), ph006d1 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c("238834", "140684",
"313895", "357343", "395260", "113302", "261429", "56770", "246735",
"44335", "243570", "291491", "169951", "84282", "128746", "370073",
"184552", "142856", "159969", "222004", "214571", "206412", "9604",
"408052", "247889", "160392", "112151", "294933", "60628", "255761",
"183798", "355557", "382694", "179322", "232143", "8384", "64595",
"130937", "58238", "211288", "89741", "274124", "15498", "263105",
"16023", "294046", "158389", "194203", "249063", "263042", "269633",
"273931", "334222", "307707", "143820", "394474", "170366", "77730",
"263558", "215557", "108658", "280293", "153393", "19683", "76186",
"398330", "291272", "381430", "7460", "5354", "319521", "132904",
"222158", "34189", "403611", "206023", "37337", "70288", "371791",
"46469", "307796", "196647", "145693", "181532", "152799", "298512",
"325908", "410652", "315940", "224851", "109837", "339716", "94379",
"85190", "92170", "308636", "332581", "344942", "15515", "406412"
), class = "data.frame")
I am trying to perform a barplot using ggplot()
If I use this code:
ggplot(datasample,aes(x=ph006d1))+geom_bar(stat="count")
Error: (converted from warning) Removed 1 rows containing non-finite values (stat_count)
I cannot understand why, I require to count the values of 1 and 0 and put in a barplot
Then I used:
ggplot(datasample,aes(x=factor(ph006d1)))+geom_bar(stat="count")
That worked but with an error
because in the variable there are no NA values.
In fact
table(datasample$ph006d1)
gives the result:
0 1
91 8
Why I am getting NA? how to do properly this barplot ??
Upvotes: 0
Views: 236
Reputation: 2140
One of your observations is missing, So if it is really missing and you want to keep NA in the dataset and produce the bar plot you can filter them using tidyverse package and pass filtered data to ggplot function:
library(tidyverse)
filter(datasample, !is.na(ph006d1)) %>%
ggplot(.,aes(x=factor(ph006d1)))+ geom_bar(stat="count")
Hope it helps.
Upvotes: 1