geom_bar showing NA and count labels on bars

Hello, is there a way to show NA counts in geom_bar when variable is of type "double" or do I need to convert it to factor?

Also, is there a way to show the counts on the bars?

the code is below is not working:

ggplot(df)+
  aes_string(x=names[4])+
  geom_bar(fill="steelblue")+
  geom_text(aes(y = after_stat(count)),vjust=1.6, color="white", size=3.5)+
  labs(y="counts")+
  scale_x_discrete(na.translate = TRUE)+
  theme_bw()

Hi @leoncio, it would be helpful to create a reprex so we can understand the problem.

try out the reprex package, and the base function dput.

Then we will have a better understanding of how to help.

Exactly, to make NA's appear as an own category you need to turn those into a factor. And this is no problem since you anway need to turn your numeric variables into groups (explicitly or implicitly). That being said, please keep in mind that bar plots are often not the best choice to show numeric, continuous values.

The {naniar} package is an interesting package to work with NA's so maybe you have a look.

With regard to your text labels (I guess this is what you mean by "showing the coutns"?) a working example is needed as @zac-garland already mentioned. So please provide the data as well so we can try it on our own.

Here is a simple example. I used count to get the frequency manually so it's easy to show text labels.

library(ggplot2)
library(dplyr)
df <- tibble(
  a = c(rbinom(n = 100, size = 1, prob = 0.5), rep(NA, 50))
) 

df %>%
  count(a) %>% 
  ggplot(aes(x = factor(a), y = n)) + 
  geom_col() + 
  geom_text(aes(label = n), vjust = -1)

1 Like

And what is your issue with this plot? You have an NA bar and text labels showing counts so all fine now?

My mistake, I should have used it on e.g. mtcars. But it turned out my tibble behaved differently. I will show a solution I found in a post below.

The problem is exactly that I have a big data sheet from excel. When I imported it in R Studio, the columns which had had categorical responses coded as numbers (1,2,3,4,5) - i.e. most of the columns - got imported as "double". Wanted to get the counts without changing the type of variables as it would be quite laborious. I needed the counts of 1s, 2s and so on.
Actually, a solution to another problem (with names) somewhat hindered me in finding a better solution here, I will post it below.

But counts are the number of rows so it doesn't matter if it's a double or a factor?

If you use readr::read_csv() you can also specify the type of each column while importing, here a code copied from the package examples:

read_csv("x,y\n1,2\n3,4", col_types = list(col_double(), col_character()))

And yes, it would help a lot having a code example which shows exactly your problem.

Thanks, this is indeed what I was looking for, I just cannot replicate it on mtcars$cyl, I do not know why (maybe class of variable is problematic?)

I also found by chance a similar working solution, which is below:

library(tidyverse)
xx <- as_vector(mtcars[,2])
ggplot()+ 
  aes(x=factor(xx))+
  geom_bar()+
  geom_text(aes(label=after_stat(count)),vjust=-0.2, stat='count', color="black", size=3.5)

I do not yet know why as.vector does not work but as_vector does, though. :wink:
Maybe because my data is a tibble, whereas mtcars is a data.frame...

1 Like

Thanks for this cue with read_csv - however, specyfing type of each column (over 300 in my df) would be laborious, so I looked for other solution.. :wink:

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