Dois gráficos em uma única figura usando ggplot

Pessoal, eu gostaria de colocar dois plots em uma mesma figura.
Isto aqui abaixo eu sei fazer:
height = rnorm(n=100, mean = 165, sd = 5)
weight = rpois(n = 100, lambda = 6)
par(mfrow =c(1,2))
plot(height, col=“blue”)
plot(weight, col=“red”)

Gostaria de fazer a mesma coisa usando o ggplot, alguém sabe como fazer?
Caso sim qual library eu deveria usar? Agradeço qualquer contribuição.

Giovani

Use facet_wrap do ggplot2. Assim:

library(ggplot2)
library(tidyr)

height = rnorm(n=100, mean = 165, sd = 5)
weight = rpois(n = 100, lambda = 6)

df <- data.frame(height, weight)

df_long <- gather(df, key = "variable", value = "value")

ggplot(df_long, aes(x = variable, y = value, col = variable)) +
  geom_boxplot() +
  facet_wrap(~variable, ncol = 2)

Caro Giovani, muito obrigado pelo retorno.

Até a criação do data frame (arquivo) df_long funcionou.
Deu erro a partir daqui, veja:

ggplot(df, aes(x = height, y = value, col = weight)) +

  • geom_boxplot() +
  • facet_wrap(~variable, ncol = 2)
    Error in combine_vars():
    ! At least one layer must contain all faceting variables: variable
    :heavy_multiplication_x: Plot is missing variable
    :heavy_multiplication_x: Layer 1 is missing variable
    Run rlang::last_trace() to see where the error occurred.

Obrigado pela atenção.
Giovani

Você está usando o RStudio?

Agora funcionou :slight_smile:
Sim eu uso R studio em um MacOS High Sierra versão 10.13.6.
Quando eu rodo o ggplot2 dá essa warining abaixo:

library(ggplot2)
Warning message:
R graphics engine version 15 is not supported by this version of RStudio. The Plots tab will be disabled until a newer version of RStudio is installed.

Em fim agora funcionou. Muito obrigado :slight_smile: