making histogram in r

my data:

structure(list(years = c(1.111, 3.22444, 3.444, 3.33, 1.33923, 
12.33), counts = c(4, 3, 21, 32, 22, 21)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

hello I am trying to make a histogram where years is on the X axis and Y is the number of counts for those years. I am trying to do this with the following code:

ggplot(data = BOOKIII_121, aes(x = years, y = animal_numbers)) + geom_bar(stat = "identity")

I am so confused how to make this histogram without geom_histogram. I really dont know why the graph is not showing up. It just shows an empty graph that does not look like anything. any advice is helpful thank U:(<3

There is no variable called animal_numbers in your sample data, if you replace this with counts you get a plot

library(ggplot2)

BOOKIII_121 <- structure(list(years = c(1.111, 3.22444, 3.444, 3.33, 1.33923, 
12.33), counts = c(4, 3, 21, 32, 22, 21)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(data = BOOKIII_121, aes(x = as.factor(years), y = counts)) +
    geom_col()

Created on 2022-09-30 with reprex v2.0.2

1 Like

I am so dumb I made a mistake in typing my code out but it still doesnt work. but also the thing is I don't want the x axis as a factor I want it as a histogram like a continuous variable. thank u D:. when I change it to a factor as you did in the ggplot it works. :slight_smile:

Can you be more specific?, what do you mean by "it doesn't work"?, ideally, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

1 Like

yes sorry. if I dont add "y = as.factor(years)" and keep it as "y=years" the plot doesnt show... which is unfortunate because I want a histogram

Well, in your iriginal code you are using geom_bar() with a continuous variable, this is not the expected use case for a barplot, so you get an odd result. If you want to make a proper histogram by using geom_bar() you would need to pre-calculate bins (categories) as well. I think it would be simpler to uncount() your data and use geom_histogram() to calculate the bins for you.

library(tidyverse)

BOOKIII_121 <- structure(list(years = c(1.111, 3.22444, 3.444, 3.33, 1.33923, 
12.33), counts = c(4, 3, 21, 32, 22, 21)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(data = BOOKIII_121, aes(x = years, y = counts)) +
    geom_col()


BOOKIII_121 %>% 
    uncount(weights = counts) %>% 
    ggplot(aes(x = years)) + 
    geom_histogram(binwidth = 1)

Created on 2022-09-30 with reprex v2.0.2

1 Like

this makes so much sense. I had no idea you could do this wow, thank you.

This topic was automatically closed 21 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.