2

I want the six plots in one plot. And I would like to specify the titles of each plot. How can I do that?

p<-ggplot(df, aes(x=COD_NEIGHB))+
  geom_bar(stat="count", width=0.3, fill="steelblue")+
  theme_minimal()
# histogram of the strata in the whole dataset
s<-ggplot(data = df, mapping = aes(x = COD_NEIGHB)) +
  geom_bar(stat="count", width=0.3, fill="steelblue")+
  facet_wrap(~ fold)
plot_grid(p, s, ncol=2,label_size = 2)

enter image description here

After that, I did the suggestion

df$fold <- as.character(df$fold)
# Duplicate data. Set category in the duplicated dataset to "all"
df_all <- df 
df_all$fold <- "all"
# Row bind the datasets
df_all <- rbind(df, df_all)

ggplot(df_all, aes(x=COD_NEIGHB)) +
  geom_bar(stat="count", width=0.3, fill="steelblue")+
  facet_wrap(~fold)

But now the problem is the scale. y-axis has to be on the proper scale.

enter image description here

any idea for that?

Thanks in advance!!!!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
LACL
  • 37
  • 6
  • 3
    Does this answer your question? [Side-by-side plots with ggplot2](https://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2) – Daman deep Nov 20 '20 at 09:42

2 Answers2

2

If I got you right you want a plot with facets by categories plus an additonal facet showing the total data. One option to achieve this is to duplicate your dataset to add an addtional category "all".

As no example data was provided I make use of mtcars to show you the basic idea:

library(ggplot2)

mtcars$cyl <- as.character(mtcars$cyl)
# Duplicate data. Set category in the duplicated dataset to "all"
mtcars_all <- mtcars 
mtcars_all$cyl <- "all"
# Row bind the datasets
mtcars_all <- rbind(mtcars, mtcars_all)

ggplot(mtcars_all, aes(hp, mpg)) +
  geom_point() +
  facet_wrap(~cyl)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Ohhh I am so sorry, I edited your nice answer. How do I undo that? – LACL Nov 20 '20 at 11:14
  • Everything fine. I simply rejected your edit. (; Best S. – stefan Nov 20 '20 at 11:20
  • 1
    ... concerning the issue with scale. You can try with option `scales="free_y"` in facet_wrap. – stefan Nov 20 '20 at 11:23
  • Wowwww thanks a lot!! Perfect!!! Thanks!. Amazing!!! It seems that you are an expert with plots. Do you know how to extract the second plot of the function post.check of GJRM package. I converted them on a ggplot object, but I can't extract the qqplot, which is that I want. – LACL Nov 20 '20 at 14:17
0

Here is another useful tool with the help of the ggarrange() function from the ggpubr package. You can arrange multiple plots on one page or multiple pages. You can also create a common, unique legend once you merge all your plots together.

Similar to previous answers, I used mtcars to demonstrate a simple use case:

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

p1 <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  theme_minimal()

p2 <- ggplot(mtcars, aes(y = mpg, x = cyl)) +
  geom_boxplot() +
  theme_minimal()

ggarrange(p1, p2, ncol = 2)

multiple plots - ggarrange

Thomas Bilach
  • 591
  • 2
  • 16