在R中将拉长的图形彼此相邻固定 [英] Fixing elongated figures next to each other in R

查看:80
本文介绍了在R中将拉长的图形彼此相邻固定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot复制这些图的主题,我在网上搜索了文章和问题,向我展示了如何分配这些图的正确大小和位置以及分配紧密点的形状,但我发现很少讨论改变立场的文章,我尝试了以下方法:

I'm trying to replicate the theme of these graph using ggplot, I searched online for articles and question to show me how to assign these plots the right size and position and also to assign the tight dot shape, and I found few articles that discussed changing position, I tried the following:

d1<-read.csv("./data/games.csv")
p.1<-ggplot(d1, aes(x=cream_rating, y=charcoal_rating)) + 
  # Map winner on color. Add some transparency in case of overplotting
  geom_point(aes(color = winner), alpha = 0.2) +
  # Add the cross: Add geom_pints with one variable fixed on its mean
  geom_point(aes(x = mean(cream_rating), color = winner), alpha = 0.2) +
  geom_point(aes(y = mean(charcoal_rating), color = winner), alpha = 0.2) +
  scale_shape_manual(values=c(16, 17)) +
  # "draw"s should be dropped and removed from the title
  scale_color_manual(values = c(cream = "seagreen4", charcoal = "chocolate3", draw = NA)) +
  ggtitle("Rating of Cream vs Charcoal") +
  xlab("rating of cream") + ylab("rating of charcoal") + theme_classic() + theme(plot.title = element_text(hjust = 0.5)) 

我尝试了以下方法将它们放在一起:

I tried the following to place them together:

require(gridExtra)
plot.1<-p.1
plot.2<-ggExtra::ggMarginal(p.1, type = "histogram")
grid.arrange(plot.1, plot.2, ncol=3)

library(cowplot)

theme_set(theme_cowplot())

plot.1<-p.1
plot.2<-ggExtra::ggMarginal(p.1, type = "histogram")

plot_grid(plot.1, plot.2, labels = "AUTO")

cowplot::plot_grid(plot.1, plot.2, labels = "AUTO")


library(magrittr)
library(multipanelfigure)
figure1 <- multi_panel_figure(columns = 2, rows = 1, panel_label_type = "none")
# show the layout
figure1

figure1 %<>%
  fill_panel(plot.1, column = 1, row = 1) %<>%
  fill_panel(plot.2, column = 2, row = 1) %<>%
  
figure1

这是我的数据集结构:

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")

这是我要实现的目标:

This is what I want to achieve:

我尝试了Stefan的建议(有很大帮助),但做了一些修改:

I tried Stefan's suggestion (which was great help) with some modifications:

d1<-read.csv("./data/games.csv")
ggplot(d1, aes(x=cream_rating, y=charcoal_rating)) + 
  ##### Map winner on color. Add some transparency in case of overplotting
  geom_point(aes(color = winner), alpha = 0.2) +
  ##### Add the cross: Add geom_pints with one variable fixed on its mean
  geom_point(aes(x = mean(cream_rating), color = winner), alpha = 0.2) +
  geom_point(aes(y = mean(charcoal_rating), color = winner), alpha = 0.2) +
  scale_shape_manual(values=c(16, 17)) +
  ##### "draw"s should be dropped and removed from the title
  scale_color_manual(values = c(cream = "seagreen4", charcoal = "chocolate3", draw = NA)) +
  ggtitle("Rating of Cream vs Charcoal") +
  xlab("rating of cream") + ylab("rating of charcoal") + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) 

我想过滤掉绘制"从图中可以看出,当我将点形状更改为三角形和圆形时,它们似乎也没有变化,此外,我还会收到此错误:

I want to filter out "draw" from the plot, also when I change the dot shapes to triangles and circle, they don't seem to be changing, in addition I get this error:

Warning message:
"Removed 950 rows containing missing values (geom_point)."
Warning message:
"Removed 950 rows containing missing values (geom_point)."
Warning message:
"Removed 950 rows containing missing values (geom_point)."

我注意到的另一件事,我得到的是双叉而不是双叉!

One more thing that I noticed, I get double cross instead of one!

这是我的输出:

当我尝试此问题中的第一个代码块时,我得到的扭曲的数字很长,彼此不成正方形.

When I try the first code block in this question, I get long distorted figures not square next to each other.

推荐答案

也许这符合您的需求.为了将三个图粘合在一起,我使用了 cowplot 包.传说可能仍然不完美.

Maybe this fits your need. To glue the three plots together I make use of the cowplot package. The legend is probably still not perfect.

  1. 为了仅获得一个图例,但仍使图线保持良好的对齐,我将第一个和第三个图线的图例设为透明".相对于 guide_legend theme 选项

为使所有图的大小相同,我向散点图添加了透明边距

To make all plots the same size I added transparent marginals to the scatter plot

为固定位置并使绘图成正方形,我通过 xlim ylim 为两个轴设置了相同的限制,并使用 theme()

To fix the position and make the plots square I set the same limits for both axes via xlim and ylim and set the aspect ratio to 1 using theme()

library(ggplot2)
library(dplyr)
library(cowplot)

# Add a second draw to the example data to make the density work
d1 <- d1 %>% 
  add_row(winner = "draw", cream_rating = 1002, charcoal_rating = 1250)

# Get the limits

lims <- c(floor(min(d1$cream_rating, d1$charcoal_rating)), ceiling(max(d1$cream_rating, d1$charcoal_rating)))

p1 <- d1 %>% 
  ggplot(aes(x=cream_rating, y=charcoal_rating, color = winner, shape = winner)) + 
  geom_point(alpha = 0.2, na.rm = TRUE) +
  scale_color_manual(values = c(cream = "seagreen4", charcoal = "chocolate3", draw = "blue")) +
  scale_shape_manual(values = c(cream = 16, charcoal = 17, draw = 15)) +
  xlim(lims) + 
  ylim(lims) +
  labs(x = "rating of cream", y = "rating of charcoal") + 
  theme_classic() + 
  theme(plot.title = element_text(hjust = 0.5), legend.position = "bottom") +
  theme(aspect.ratio = 1)

p1_1 <- p1 +
  guides(color = guide_legend(override.aes = list(color = c(NA, NA, NA)))) +
  theme(legend.text = element_blank(), legend.title = element_blank())

p1_1 <- ggExtra::ggMarginal(p1_1, type = "histogram",
                    margins = 'both',
                    size = 5,
                    position = "identity",
                    color = NA,
                    fill= NA)

p2 <- ggExtra::ggMarginal(p1, type = "histogram",
                             margins = 'both',
                             size = 5,
                             groupColour = TRUE,
                             groupFill = TRUE,
                             position = "identity"
)

# Make legend transparent
p1 <- p1 +
  guides(color = guide_legend(override.aes = list(color = c(NA, NA, NA)))) +
  theme(legend.text = element_blank(), legend.title = element_blank())

p3 <- d1 %>% 
  ggplot(aes(x=cream_rating, y=charcoal_rating, color = winner, shape = winner)) + 
  geom_density_2d(na.rm = TRUE)  +
  geom_point(alpha = 0, show.legend = FALSE) +
  scale_color_manual(values = c(cream = "seagreen4", charcoal = "chocolate3", draw = "blue")) +
  xlim(lims) + 
  ylim(lims) +
  labs(x = "rating of cream", y = "rating of charcoal") + 
  theme_classic() + 
  theme(plot.title = element_text(hjust = 0.5), 
        legend.position = "bottom") +
  theme(aspect.ratio = 1)

# Make legend transparent
p3 <- p3 +
  guides(color = guide_legend(override.aes = list(color = c(NA, NA, NA)))) +
  theme(legend.text = element_blank(), legend.title = element_blank())

p3 <- ggExtra::ggMarginal(p3, d1, type = "density",
                          margins = 'both',
                          size = 5,
                          groupColour = TRUE,
                          groupFill = TRUE,
                          position = "identity"
)

plot_row <- plot_grid(p1_1, p2, p3, nrow = 1)

# now add the title
title <- ggdraw() +
  draw_label(
    "Rating of Cream vs Charcoal",
    fontface = 'bold',
    x = 0,
    hjust = 0
  )

final <- plot_grid(
  title, plot_row,
  ncol = 1,
  # rel_heights values control vertical title margins
  rel_heights = c(0.1, 1)
)

final

注意:根据绘图设备的宽度和高度,固定纵横比会在顶部和底部添加一些空白.根据最终输出的结果,您可能需要稍微调整一下宽度和高度(和比例),例如使用

Note Depending on the width and heigth of your plotting device, fixing the aspect ratio adds some white space at the top and bottom. Depending on your final output you probably have to play a bit around with the width and height (and scale), e.g. using

ggsave("final.png", width = 18, height = 6, units = "cm", scale = 1.5)

给予

这篇关于在R中将拉长的图形彼此相邻固定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆