Reputation: 593
I'm trying to reproduce this figure, I'm facing hard times in reproducing two things, the right scale ratio (all plots are square with clear top title and top left corner letter label), the second is I see a gap between the marginal graphs and the plots themselves. What I want to achieve is this:
My dataset:
structure(list(rated = c(FALSE, TRUE, TRUE, TRUE, TRUE, FALSE,
TRUE, FALSE, TRUE, TRUE), turns = c(13L, 16L, 61L, 61L, 95L,
5L, 33L, 9L, 66L, 119L), victory_status = structure(c(3L, 4L,
2L, 2L, 2L, 1L, 4L, 4L, 4L, 2L), .Label = c("draw", "mate", "outoftime",
"resign"), class = "factor"), winner = structure(c(2L, 1L, 2L,
2L, 2L, 3L, 2L, 1L, 1L, 2L), .Label = c("charcoal", "cream",
"draw"), class = "factor"), increment_code = structure(c(3L,
7L, 7L, 5L, 6L, 1L, 1L, 4L, 2L, 1L), .Label = c("10+0", "15+0",
"15+2", "15+30", "20+0", "30+3", "5+10"), class = "factor"),
cream_rating = c(1500L, 1322L, 1496L, 1439L, 1523L, 1250L,
1520L, 1413L, 1439L, 1381L), charcoal_rating = c(1191L, 1261L,
1500L, 1454L, 1469L, 1002L, 1423L, 2108L, 1392L, 1209L)), row.names = c(NA,
10L), class = "data.frame")
I tried the following with great help from @stefan:
library(ggplot2)
library(dplyr)
library(gtable)
library(cowplot)
library(gridExtra)
d1<-read.csv("./data/games.csv")
lims <- c(floor(min(d1$cream_rating, d1$charcoal_rating)), ceiling(max(d1$cream_rating, d1$charcoal_rating)))
p.001<- ggplot(d1, aes(x=cream_rating, y=charcoal_rating, color = winner, shape = winner)) +
# Map winner on color. Add some transparency in case of overplotting
geom_point(alpha = 0.3, na.rm = TRUE) +
scale_color_manual(values = c(cream = "seagreen4", charcoal = "chocolate3", draw = "seagreen4")) +
scale_shape_manual(values = c(cream = 17, charcoal = 16, draw= 15)) +
ggtitle("Rating of Cream vs Charcoal") +
xlab("rating of cream") + ylab("rating of charcoal") + theme(plot.title = element_text(size=16, face = "plain", hjust = 0.5), legend.text = element_blank(), legend.title = element_blank())+guides(color = guide_legend(override.aes = list(color = c(NA, NA, NA))))
p.002.a<- ggplot(d1, aes(x=cream_rating, y=charcoal_rating, color = winner, shape = winner)) +
# Map winner on color. Add some transparency in case of overplotting
geom_point(alpha = 0.3, na.rm = TRUE) +
scale_color_manual(values = c(cream = "seagreen4", charcoal = "chocolate3", draw = "seagreen4")) +
scale_shape_manual(values = c(cream = 17, charcoal = 16, draw= 15)) +
ggtitle("Scatter Plot With Marginal Histograms") +
xlab("rating of cream") + ylab("rating of charcoal") + theme(plot.title = element_text(size=16, face = "plain", hjust = 0.5), legend.text = element_blank(), legend.title = element_blank())+guides(color = guide_legend(override.aes = list(color = c(NA, NA, NA))))
p.002<-
ggExtra::ggMarginal(p.002.a+ ggtitle("Scatter Plot With Marginal Histograms") , type = "histogram",
margins = 'both',
size = 5,
groupColour = TRUE,
groupFill = TRUE,
position = "identity")
p.004a<-ggplot(d1, aes(x=cream_rating, y=charcoal_rating, color = winner, shape = winner)) +
geom_density2d(alpha = 0.3, na.rm = TRUE) +
geom_point(alpha = 0, show.legend = FALSE) +
scale_color_manual(values = c(cream = "cyan4", charcoal = "peachpuff2", draw = "lightskyblue1")) +
scale_shape_manual(values = c(cream = 16, charcoal = 17, draw = 15)) +
xlim(lims) +
ylim(lims) +
ggtitle("2D-Density Plot with Marginal Density plot") +
xlab("rating of cream") + ylab("rating of charcoal")+ theme_classic() + theme(plot.title = element_text(size=14, face = "bold", hjust = 0.5)) +
guides(color = guide_legend(override.aes = list(color = c(NA, NA, NA)))) +
theme(legend.text = element_blank(), legend.title = element_blank())
p.005 <- ggExtra::ggMarginal(p.004a+ggtitle("2D Density Plot With Marginal Density plot"), d1, type = "density",
margins = 'both',
size = 5,
groupColour = TRUE,
groupFill = TRUE,
position = "identity"
)
plot_row <- plot_grid(p.001, p.002, p.005, nrow = 1)
final <- plot_grid(
title, plot_row,
ncol = 1,
rel_heights = c(0.5, 0.5)
)
final
But I'm getting these figures:
I don't mind using any function such as grid.arrange or anything that could reproduce the same figure above.
Upvotes: 1
Views: 357
Reputation: 76
In order to make the plot square use:
theme(aspect.ratio = 1)
To fix the gap between the graph and the marginal use:
theme(legend.position = "none")
instead of
theme(legend.text = element_blank())
Upvotes: 1