How do to create a legend for "geom_function" in histograms ggplot2?

Hello dear R community,
I'm trying to improve my histogram graphs of dataframe to display a key information additionally. So I would like print a legend that show the following things:

Tittle: "normal distribution"
mu(symbol) = (mean value)
sigma(symbol) = (standar deviation value)

The problem is that, I can't make possible to show even the float legend structure to proceed edit that in the graph.

Here is my code:

Read data frame

No_porosa <- read.csv("Nano1.csv", header = TRUE, sep = ",")

Build histogram for non-porous nanoparticles

library(ggplot2)

Breaks for method of FD (1981)

nbreaks3 <- pretty(range(No_porosa), nclass.FD(unlist(No_porosa), digits = 5), min.n = 1)

Printing the plot

ggplot(data.frame(No_porosa), aes(x = Diametro..nm.)) +
geom_histogram(breaks = nbreaks3, color = "black", fill = "lightsteelblue2", aes(y = ..density..)) +
geom_function(fun = dnorm, args = list(mean = mean(No_porosa$Diametro..nm.),
sd = sd(No_porosa$Diametro..nm.)),
colour = "red4", size = 1) +
theme(panel.background = element_rect(fill = "white", color = "black")) +
labs(title = "Tipo 1", x = "Di√°metro [nm]", y = "Densidad")

I have tried with "fill = ", "color = ", and "alpha =" inside aes(x, ...) function without succeed, that based on this page: https://r-charts.com/ggplot2/legend/

Please, help me.... My desire is print a legend for normal distribution line generated by "geom_function" in ggplot 2.

image

Hi @Cesar-AVP,
Welcome to the RStudio Community Forum.

You are not really asking for a "legend" but rather a "text box" containing some information.
This code is a bit clunky but gets the job done:

library(ggplot2)

# Make some dummy data as we don't have yours
No_porosa <- data.frame(Diametro..nm. = rnorm(200, mean=25, sd=5))

ggplot(data.frame(No_porosa), aes(x = Diametro..nm.)) +
  geom_histogram(
    #breaks = nbreaks3,
    color = "black",
    fill = "lightsteelblue2",
    aes(y = ..density..)
  ) +
  geom_function(
    fun = dnorm,
    args = list(
      mean = mean(No_porosa$Diametro..nm.),
      sd = sd(No_porosa$Diametro..nm.)
    ),
    colour = "red4",
    size = 1
  ) +
  theme(panel.background = element_rect(fill = "white", color = "black")) +
  labs(title = "Tipo 1", x = "Di√°metro [nm]", y = "Densidad") -> p1

p1
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.


# Get the stats to include in the text box
my_mean <- round(mean(No_porosa$Diametro..nm., na.rm=TRUE), digits=4)
my_sd <- round(sd(No_porosa$Diametro..nm., na.rm=TRUE), digits=4)

my_text <- paste0("Normal Distribution\nMean = ",my_mean,"\nSD = ",my_sd)
my_text
#> [1] "Normal Distribution\nMean = 25.1273\nSD = 4.7495"

# Add the text box to the plot
p1 + geom_text(aes(32, 0.09, label=my_text, hjust="left"))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2022-10-07 with reprex v2.0.2

Hope this helps.

3 Likes

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.