How to plot side-by-side Plots with ggplot2 in R?

0 votes

Like we are creating two plots of the dataset "iris":

Boxplot:

data("iris")
library(ggplot2)

iris1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() + theme_bw()

BarPlot:

iris2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_bar() + theme_bw() +
  theme(legend.position = c(0.8, 0.8))

image

image But we want to be together as to compare two plots easily!

Apr 16, 2018 in Data Analytics by DataKing99
• 8,130 points
1,747 views

1 answer to this question.

0 votes

By Using gridExtra library we can easily plot with grid.arrange()

require(gridExtra)
gridExtra::grid.arrange(iris1,iris2)

image

There is another way to put it together by using cowplot library, as grid.arrange make it difficult to labels the plots with letters(A,B,C,....)

library(cowplot)
plot_grid(iris1, iris2, labels = "AUTO")

image
answered Apr 16, 2018 by DeepCoder786
• 1,720 points

Related Questions In Data Analytics

0 votes
1 answer

How to join two tables (tibbles) by *list* columns in R

You can use the hash from digest ...READ MORE

answered Apr 5, 2018 in Data Analytics by kappa3010
• 2,020 points
72 views
0 votes
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21 in Data Analytics by anonymous
• 31,840 points
150 views
0 votes
2 answers

How to use group by for multiple columns in dplyr, using string vector input in R?

data = data.frame(   zzz11def = sample(LETTERS[1:3], 100, replace=TRUE),   zbc123qws1 ...READ MORE

answered Aug 5 in Data Analytics by anonymous
3,614 views
0 votes
2 answers

How to sum a variable by group in R?

You can also try this way, x_new = ...READ MORE

answered Jul 31 in Data Analytics by Cherukuri
• 31,840 points
10,571 views
0 votes
1 answer
0 votes
0 answers
0 votes
0 answers

Kmeans Clusters - R visualization

Hi, can someone help me visualizae the ...READ MORE

Aug 1 in Data Analytics by rajeev
25 views
0 votes
1 answer

Aligning title in ggplot2

You can use the theme layer to ...READ MORE

answered Apr 27, 2018 in Data Analytics by Bharani
• 4,550 points
824 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
160 views
0 votes
2 answers

How to write lines to a text file in R?

sink("outfile.txt") cat("hello") cat("\n" ...READ MORE

answered May 24 in Data Analytics by anonymous
2,524 views