Help with gghistogram function

Hi, I'm new to R and R Studio. In my R class, I can't see histograms using the "gghistogram" function. Anyone know why? I already installed ggplot2 and called it...

Hi @Nat_bio , welcome!

For make a histogram with ggplot you could use this example:

set.seed(3)
x1 <- rnorm(500)
x2 <- rnorm(500, mean = 3)
x <- c(x1, x2)
grupo <- c(rep("G1", 500), rep("G2", 500))
df <- data.frame(x, group = grupo)

library(ggplot2)

ggplot(df, aes(x = x)) +
  geom_histogram(fill = "pink", colour = "brown") 

# Im not sure gghistogram is a function of ggplot.
1 Like

I don't think there is such function gghistogram in the ggplot2 package. You should use the geom_histogram function to create histograms in ggplot2.

Here is an example:

# Load the ggplot2 library if not already loaded
library(ggplot2)

# Create a sample data frame
data = data.frame(values = rnorm(100))

# Create a histogram using ggplot2
ggplot(data, aes(x = values)) +
  geom_histogram(binwidth = 0.5, fill = "red") +
  labs(title = "Histogram of Sample Data", x = "Values", y = "Frequency")

image

Created on 2023-10-11 with reprex v2.0.2

In the Example,I loaded the ggplot2 library using library(ggplot2). Then I created a sample data frame called data with 100 random values (you can replace this with your actual data). Use ggplot() to initialize the plot and specify the data and aesthetics. In this case, we map the values column to the x-axis. Then add a histogram layer to the plot using geom_histogram. The binwidth argument controls the width of the histogram bins, and you can adjust it to suit your data. I also specified the fill aesthetic to customize the appearance of the histogram bars. Lastly add labs() to add a title and label the x and y axes.

1 Like

gghistogram is in the {ggpubr} package a ggplot extension.
Try

install.packages("ggpubr")
library(ggpubr)

Then try using
gghistogram()

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.