Search code examples
rcountbigdatahistogramfrequency

Histogram with count R


I am trying to create a histogram of my data. My dataframe looks like this

x  counts
4  78
5  45
... ...

where x is the variable I would like to plot and counts is the number of observations. If I do hist(x) the plot will be misleading because I am not taking into account the count. I have also tried:

hist(do.call("c", (mapply(rep, df$x, df$count))))

Unfortunately this does not work because the resulting vector will be too big

sum(df$ount)
[1] 7943571126

Is there any other way I can try?

Thank you


Solution

  • The solution is a barplot as @Rui Barradas suggested. I use ggplot to plot data.

    library(ggplot2)
    x <- c(4, 5, 6, 7, 8, 9, 10)
    counts <- c(78, 45, 50, 12, 30, 50)
    df <- data.frame(x=x, counts=counts)
    
    plt <- ggplot(df) + geom_bar(aes(x=x, y=counts), stat="identity")
    print(plt)
    

    barplot