How do mean calculate with summarise and use in a ggplot con with pipes?

Hi community,
I'm want to use a mean values calculate with summarise for set the max() and min() in y axis. I'm reproduce the error with iris data base.
I want to do this directly so as not to create a df of means.

library(tidyverse)

iris |> group_by(Species) |> 
  summarise(mean=mean(Sepal.Length)) |> 
  ggplot(aes(x=Species,y=mean))+
  geom_point() +
  scale_y_continuous(limits = c(min(mean),
                             max(mean)),
                  expand = c(0,0))
#Error in min(mean) : invalid 'type' (closure) of argument 

I found this solution on StackOverflow

Notice that I had to use the %>% pipe rather than |>

library(tidyverse)

iris |> group_by(Species) |> 
  summarise(mean=mean(Sepal.Length)) %>% 
  {ggplot(., aes(x=Species,y=mean))+
  geom_point() +
  scale_y_continuous(limits = c(min(.$mean),
                                max(.$mean)),
                     expand = c(0,0))}

Created on 2024-03-10 with reprex v2.0.2

1 Like

With iris data run well :slight_smile:

I don't know why with my data don't run.

library(tidyverse)
library(lubridate)
library(ggpubr)

ENSAYO_1 <- data.frame(structure(list(dia = c("4", "4", "4", "4", "4", "5", "4", "4", 
                                              "4", "4", "4", "6", "6", "6", "6", "6", "6"), mes = c("10", "10", 
                                                                                                    "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", 
                                                                                                    "10", "10", "10", "10"), anio = c("2022", "2022", "2022", "2022", 
                                                                                                                                      "2022", "2022", "2022", "2022", "2022", "2022", "2022", "2022", 
                                                                                                                                      "2022", "2022", "2022", "2022", "2022"), hora = c("0:00:00", 
                                                                                                                                                                                        "0:10:00", "0:20:00", "0:30:00", "0:40:00", "0:00:00", "23:50:00", 
                                                                                                                                                                                        "23:40:00", "23:30:00", "23:20:00", "23:10:00", "0:00:00", "0:10:00", 
                                                                                                                                                                                        "0:20:00", "0:30:00", "0:40:00", "0:50:00"), `MEDIANA SENSORES TEMP` = c(23.65, 
                                                                                                                                                                                                                                                                 23.6, 23.9, 24, 23.95, 20.25, 20.3, 20.3, 20.2, 20.35, 20.35, 
                                                                                                                                                                                                                                                                 22.5, 22.5, 22.55, 22.75, 22.5, 22.6), `MEDIANA SENSORES HUM` = c(84.7375, 
                                                                                                                                                                                                                                                                                                                                   85.0625, 85.0875, 84.475, 84.4375, 95.5875, 95.6625, 95.8875, 
                                                                                                                                                                                                                                                                                                                                   95.025, 95, 95.05, 88.6625, 88.8125, 88.7875, 88.275, 88.7, 89.15
                                                                                                                                                                                                                                                                 ), `DATE AND TIME_2` = c("4 10 2022 0:00:00", "4 10 2022 0:10:00", 
                                                                                                                                                                                                                                                                                          "4 10 2022 0:20:00", "4 10 2022 0:30:00", "4 10 2022 0:40:00", 
                                                                                                                                                                                                                                                                                          "5 10 2022 0:00:00", "4 10 2022 23:50:00", "4 10 2022 23:40:00", 
                                                                                                                                                                                                                                                                                          "4 10 2022 23:30:00", "4 10 2022 23:20:00", "4 10 2022 23:10:00", 
                                                                                                                                                                                                                                                                                          "6 10 2022 0:00:00", "6 10 2022 0:10:00", "6 10 2022 0:20:00", 
                                                                                                                                                                                                                                                                                          "6 10 2022 0:30:00", "6 10 2022 0:40:00", "6 10 2022 0:50:00"
                                                                                                                                                                                                                                                                 ), fecha = structure(c(1664841600, 1664842200, 1664842800, 1664843400, 
                                                                                                                                                                                                                                                                                        1664844000, 1664928000, 1664927400, 1664926800, 1664926200, 1664925600, 
                                                                                                                                                                                                                                                                                        1664925000, 1665014400, 1665015000, 1665015600, 1665016200, 1665016800, 
                                                                                                                                                                                                                                                                                        1665017400), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
                                      fecha_2 = structure(c(19269, 19269, 19269, 19269, 19269, 
                                                            19270, 19269, 19269, 19269, 19269, 19269, 19271, 19271, 19271, 
                                                            19271, 19271, 19271), class = "Date")), row.names = c(NA, 
                                                                                                                  -17L), class = c("tbl_df", "tbl", "data.frame")))
# Plot
ENSAYO_1 |> select(MEDIANA.SENSORES.TEMP,fecha_2, hora) |> 
  group_by(fecha_2) |> 
  summarise(mean=mean(MEDIANA.SENSORES.TEMP)) |> 
  {ggplot(.,aes(x=fecha_2,y=mean)) +
  geom_point() +
  geom_line(color='blue') +
  scale_y_continuous(limits = c(min(.$mean),
                                max(.$mean)),
                     expand = c(0,0)) +
  scale_x_date(date_breaks = "1 day", date_minor_breaks = "1 day",
               date_labels = "%d-%b") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,size = 10)) +
  labs(title = 'Ensayo 1 - Sensores Temperatura',
       x='',
       y='Media temperatura')}

# Error in { : function '{' not supported in RHS call of a pipe (<input>:4:3)

Im dont read this part, Im change and all run excellent.

Notice that I had to use the %>% pipe rather than |>

ENSAYO_1 |> select(MEDIANA.SENSORES.TEMP,fecha_2, hora) %>% 
  group_by(fecha_2) %>%  
  summarise(mean=mean(MEDIANA.SENSORES.TEMP)) %>% 
  {ggplot(.,aes(x=fecha_2,y=mean)) +
  geom_point() +
  geom_line(color='blue') +
  scale_y_continuous(limits = c(min(.$mean)-0.5,
                                max(.$mean)+0.5),
                     expand = c(0,0)) +
  scale_x_date(date_breaks = "3 day", date_minor_breaks = "3 day",
               date_labels = "%d-%b") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,size = 10)) +
  labs(title = 'Ensayo 1 - Sensores Temperatura',
       x='',
       y='Media temperatura')}

Tnks!

There is actually not need to specify the limits in this case so I think you can simply do something like this

library(dplyr)
library(ggplot2)

ENSAYO_1 <- data.frame(structure(list(dia = c("4", "4", "4", "4", "4", "5", "4", "4", 
                                              "4", "4", "4", "6", "6", "6", "6", "6", "6"), mes = c("10", "10", 
                                                                                                    "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", 
                                                                                                    "10", "10", "10", "10"), anio = c("2022", "2022", "2022", "2022", 
                                                                                                                                      "2022", "2022", "2022", "2022", "2022", "2022", "2022", "2022", 
                                                                                                                                      "2022", "2022", "2022", "2022", "2022"), hora = c("0:00:00", 
                                                                                                                                                                                        "0:10:00", "0:20:00", "0:30:00", "0:40:00", "0:00:00", "23:50:00", 
                                                                                                                                                                                        "23:40:00", "23:30:00", "23:20:00", "23:10:00", "0:00:00", "0:10:00", 
                                                                                                                                                                                        "0:20:00", "0:30:00", "0:40:00", "0:50:00"), `MEDIANA SENSORES TEMP` = c(23.65, 
                                                                                                                                                                                                                                                                 23.6, 23.9, 24, 23.95, 20.25, 20.3, 20.3, 20.2, 20.35, 20.35, 
                                                                                                                                                                                                                                                                 22.5, 22.5, 22.55, 22.75, 22.5, 22.6), `MEDIANA SENSORES HUM` = c(84.7375, 
                                                                                                                                                                                                                                                                                                                                   85.0625, 85.0875, 84.475, 84.4375, 95.5875, 95.6625, 95.8875, 
                                                                                                                                                                                                                                                                                                                                   95.025, 95, 95.05, 88.6625, 88.8125, 88.7875, 88.275, 88.7, 89.15
                                                                                                                                                                                                                                                                 ), `DATE AND TIME_2` = c("4 10 2022 0:00:00", "4 10 2022 0:10:00", 
                                                                                                                                                                                                                                                                                          "4 10 2022 0:20:00", "4 10 2022 0:30:00", "4 10 2022 0:40:00", 
                                                                                                                                                                                                                                                                                          "5 10 2022 0:00:00", "4 10 2022 23:50:00", "4 10 2022 23:40:00", 
                                                                                                                                                                                                                                                                                          "4 10 2022 23:30:00", "4 10 2022 23:20:00", "4 10 2022 23:10:00", 
                                                                                                                                                                                                                                                                                          "6 10 2022 0:00:00", "6 10 2022 0:10:00", "6 10 2022 0:20:00", 
                                                                                                                                                                                                                                                                                          "6 10 2022 0:30:00", "6 10 2022 0:40:00", "6 10 2022 0:50:00"
                                                                                                                                                                                                                                                                 ), fecha = structure(c(1664841600, 1664842200, 1664842800, 1664843400, 
                                                                                                                                                                                                                                                                                        1664844000, 1664928000, 1664927400, 1664926800, 1664926200, 1664925600, 
                                                                                                                                                                                                                                                                                        1664925000, 1665014400, 1665015000, 1665015600, 1665016200, 1665016800, 
                                                                                                                                                                                                                                                                                        1665017400), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
                                      fecha_2 = structure(c(19269, 19269, 19269, 19269, 19269, 
                                                            19270, 19269, 19269, 19269, 19269, 19269, 19271, 19271, 19271, 
                                                            19271, 19271, 19271), class = "Date")), row.names = c(NA, 
                                                                                                                  -17L), class = c("tbl_df", "tbl", "data.frame")))
# Plot
ENSAYO_1 %>% 
  select(MEDIANA.SENSORES.TEMP,fecha_2, hora) %>% 
  group_by(fecha_2) %>% 
  summarise(mean = mean(MEDIANA.SENSORES.TEMP), .groups = "drop") %>%
  mutate(min_mean = min(mean),
         max_mean = max(mean)) %>% 
  ggplot(aes(x = fecha_2, y = mean)) +
      geom_point() +
      geom_line(color = 'blue') +
      scale_y_continuous(expand = expansion(add = c(0.5, 0.5))) +
      scale_x_date(date_breaks = "1 day", date_minor_breaks = "1 day",
                   date_labels = "%d-%b") +
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1,size = 10)) +
      labs(title = 'Ensayo 1 - Sensores Temperatura',
           x='',
           y='Media temperatura')

Created on 2024-03-11 with reprex v2.0.2

2 Likes

Tnks, this is solution an other good way. :handshake:

Why does the native pipe |> not work in this case?

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.