Plotted graphs separately in single window

Asked

Viewed 47 times

1

I am trying to reproduce the graphic "1" below for the model "2", however I am not having success in my command used.

The idea is to separate into different Plots for a better visualization (geom_line + geom_point upper part and geom_col underside).

Follows the command and figures exemplifying the models.

library(ggplot2)
library(lubridate)
dt <- data.frame(periodo = c ("junho", "julho", "agosto"), peso = c(1, 5, 4, 3, 4, 3),  
                 atr = c(0.95, 0.5, 0.7, 0.75, 0.6, 0.8))
dt$grupo <- rep(c("A", "B"), each = nrow(dt)/2)
dt$periodo <- factor(dt$periodo, levels = c("junho", "julho", "agosto"))
dtF <- rbind(
       data.frame(when=dt$periodo, num=dt$peso, what="Peso"),
       data.frame(when=periodo, num=dt$atr, what="ATR"))
secondFacet <- FALSE +
ggplot(data = dtF, mapping = aes(peso, atr, fill = grupo))+
geom_col(position = "dodge")
facet_grid(what~., scale = "free") +
geom_bar(data=dtF[dtF$what=="Peso",], stat = "identity", fill = "grey") +
geom_line(data=dtF[dtF$what=="ATR",], size = 2, color = "blue") + 
scale_y_continuous(name = NULL, labels = function(b) {
if(!secondFacet) {secondFacet <<- TRUE 
  return(paste0(round(b * 100, 0), "%"))}else{return(b)}}) +
scale_x_date(name = "Day", labels = NULL)

Figura exemplo

2 answers

3

There is more than one way to do this. My favorite is by using the function grid.arrange package gridExtra. For this, just create two charts independently and save them inside objects in the R.

In this case, I created the column chart and saved it to an object called g_col, while the point and line chart was saved to the object g_point.

Then just join them with the function grid.arrange, saying that I would like to organize them in two lines of graphics. I could have put the graphics in columns, as in the following example, but the visualization is not so good in this case.

library(ggplot2)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(gridExtra)

dt <- data.frame(periodo = c ("junho", "julho", "agosto"), 
                                 peso = c(1, 5, 4, 3, 4, 3),  
                                 atr = c(0.95, 0.5, 0.7, 0.75, 0.6, 0.8))
dt$grupo <- rep(c("A", "B"), each = nrow(dt)/2)
dt$periodo <- factor(dt$periodo, levels = c("junho", "julho", "agosto"))

g_col <- ggplot(dt, aes(x = periodo, y = peso, fill = grupo)) +
    geom_col(position = "dodge")

g_point <- ggplot(dt, aes(x = periodo, y = atr, colour = grupo, group = grupo)) +
    geom_point() +
    geom_line()

grid.arrange(g_point, g_col, nrow = 2)

grid.arrange(g_point, g_col, ncol = 2)

Created on 2021-07-27 by the reprex package (v2.0.0)

2


Complementing the reply by Marcus Nunes (and taking advantage of the same data) with one more option: the ggpubr package. It is less flexible than gridExtra, but for multiple graphics has option to align plotting areas and share subtitles:

library(ggpubr)

ggarrange(g_col + labs(x = NULL), g_point,
  ncol = 1, align = "v", common.legend = TRUE, legend = "bottom")

inserir a descrição da imagem aqui

  • Carlos in this case the symbol of the graph of points and lines can also be included in this single legend ?

  • No. The option is common.legend assumes that both are equal and the ggarrange extracts and uses that of the first graph. There are ways to do this, but not simply with ggpubr. As I commented in the reply, it is friendly but not flexible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.