Why is no histogram shown?

I tried to add a simple dataframe just to make a simple histogram with ggplot2 but nothing is shown in plot. What is wrong with this code?

> test<-c(1,2,4,19)
> test<-data.frame(test)
> testhisto<-ggplot(test, aes())
> testhisto<-geom_histogram()

Here are three corrected versions of your code. The first version has minimal changes, just enough to get the code to work plus I added a line to display the data before plotting it, so that the plotting code might be easier to understand.
In the second version, I changed the column name in the data frame to COUNT, so it would be clear where the code refers to the data frame and where it refers to the column.
In the third version, I did all the plotting on one line and removed the need to print() the plot.

library(ggplot2)
test<-c(1,2,4,19)
test<-data.frame(test)
test
#>   test
#> 1    1
#> 2    2
#> 3    4
#> 4   19
testhisto <-ggplot(test, aes(x = test))
testhisto <- testhisto + geom_histogram()
print(testhisto)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.


#change the column name to COUNT
test<-c(1,2,4,19)
test<-data.frame(COUNT = test)
test
#>   COUNT
#> 1     1
#> 2     2
#> 3     4
#> 4    19
testhisto <- ggplot(test, aes(x = COUNT))
testhisto <- testhisto + geom_histogram()
print(testhisto)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.


test<-c(1,2,4,19)
test<-data.frame(COUNT = test)
ggplot(test, aes(x = COUNT)) + geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2023-05-06 with reprex v2.0.2

1 Like

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