Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot的具有唯一图例的绘图布局_R_Layout_Plot_Ggplot2_Grid Layout - Fatal编程技术网

R 使用ggplot的具有唯一图例的绘图布局

R 使用ggplot的具有唯一图例的绘图布局,r,layout,plot,ggplot2,grid-layout,R,Layout,Plot,Ggplot2,Grid Layout,我试图创建一个布局,其中的图共享同一个图例。图例位于第一个绘图的顶部,但下一个绘图的比例不同。我怎样才能解决这个问题 library(ggplot2) library(gridExtra) grid.arrange( ggplot(mpg, aes(displ, cty)) + geom_point(aes(shape = "Data")) + stat_smooth(aes(linetype = "Regression"), method = "lm",

我试图创建一个布局,其中的图共享同一个图例。图例位于第一个绘图的顶部,但下一个绘图的比例不同。我怎样才能解决这个问题

library(ggplot2)
library(gridExtra)

grid.arrange(

ggplot(mpg, aes(displ, cty)) + 
  geom_point(aes(shape = "Data")) +
  stat_smooth(aes(linetype = "Regression"), method = "lm", 
              formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
  scale_shape_manual(values = 1) +
  labs(shape = "", linetype = "") +
  theme_classic() + 
  theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
        aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
        legend.position = "top")
,
ggplot(mpg, aes(displ, cty)) + 
  geom_point(shape = 1) +
  stat_smooth(method = "lm", 
              formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
  theme_classic() + 
  theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
        aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10))
)

我不知道如何使用
网格。排列
,但这里有一个使用我的cowplot包的解决方案。这个想法是把传说从情节中分离出来,然后把三个元素放在一列。类似的方法也适用于
网格

library(cowplot)

p1 <- ggplot(mpg, aes(displ, cty)) + 
  geom_point(aes(shape = "Data")) +
  stat_smooth(aes(linetype = "Regression"), method = "lm", 
              formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
  scale_shape_manual(values = 1) +
  labs(shape = "", linetype = "") +
  theme_classic() + 
  theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
        aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
        legend.position = "top")

p2 <- ggplot(mpg, aes(displ, cty)) + 
  geom_point(shape = 1) +
  stat_smooth(method = "lm", 
              formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
  theme_classic() + 
  theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
        aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10))

legend <- get_legend(p1)
plot_grid(legend, p1 + theme(legend.position = "none"), p2,
          ncol=1, rel_heights = c(0.1, 1, 1))
库(cowplot)

p1我不知道如何使用
网格。排列
,但这里有一个使用我的cowplot包的解决方案。这个想法是把传说从情节中分离出来,然后把三个元素放在一列。类似的方法也适用于
网格

library(cowplot)

p1 <- ggplot(mpg, aes(displ, cty)) + 
  geom_point(aes(shape = "Data")) +
  stat_smooth(aes(linetype = "Regression"), method = "lm", 
              formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
  scale_shape_manual(values = 1) +
  labs(shape = "", linetype = "") +
  theme_classic() + 
  theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
        aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
        legend.position = "top")

p2 <- ggplot(mpg, aes(displ, cty)) + 
  geom_point(shape = 1) +
  stat_smooth(method = "lm", 
              formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
  theme_classic() + 
  theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
        aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10))

legend <- get_legend(p1)
plot_grid(legend, p1 + theme(legend.position = "none"), p2,
          ncol=1, rel_heights = c(0.1, 1, 1))
库(cowplot)

p1如果绘图也具有相同的轴标签,则镶嵌面包裹可能是一个好的选择

library(ggplot2)

data = rbind(data.frame("id" = 1, mpg), data.frame("id" = 2, mpg))

ggplot(data, aes(displ, cty)) + 
    geom_point(aes(shape = "Data")) +
    stat_smooth(aes(linetype = "Regression"), method = "lm", 
          formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
    scale_shape_manual(values = 1) +
    labs(shape = "", linetype = "") +
    theme_classic() + 
    facet_wrap(~id, ncol = 1 ) +
    theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
    aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
    legend.position = "top",
    strip.background = element_blank(),
    strip.text.x = element_blank()) #these two lines remove the facet strips

如果绘图也具有相同的轴标签,则镶嵌面包裹可能是一个不错的选择

library(ggplot2)

data = rbind(data.frame("id" = 1, mpg), data.frame("id" = 2, mpg))

ggplot(data, aes(displ, cty)) + 
    geom_point(aes(shape = "Data")) +
    stat_smooth(aes(linetype = "Regression"), method = "lm", 
          formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
    scale_shape_manual(values = 1) +
    labs(shape = "", linetype = "") +
    theme_classic() + 
    facet_wrap(~id, ncol = 1 ) +
    theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
    aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
    legend.position = "top",
    strip.background = element_blank(),
    strip.text.x = element_blank()) #these two lines remove the facet strips

网格。排列
不尝试对齐打印面板;这是一个通用函数,适用于所有类型的网格图形,在这种情况下,由于顶部的绘图有一个图例,因此它会缩小以适应可用空间(默认情况下,此处为页面的1/2)。对于ggplots的特定情况,我将使用
egg::ggarrange

library(ggplot2)
library(egg)

ggarrange(

  ggplot(mpg, aes(displ, cty)) + 
    geom_point(aes(shape = "Data")) +
    stat_smooth(aes(linetype = "Regression"), method = "lm", 
                formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
    scale_shape_manual(values = 1) +
    labs(shape = "", linetype = "") +
    theme_classic() + 
    theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
          aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
          legend.position = "top")
  ,
  ggplot(mpg, aes(displ, cty)) + 
    geom_point(shape = 1) +
    stat_smooth(method = "lm", 
                formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
    theme_classic() + 
    theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
          aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10))
)

grid.arrange
不尝试对齐打印面板;这是一个通用函数,适用于所有类型的网格图形,在这种情况下,由于顶部的绘图有一个图例,因此它会缩小以适应可用空间(默认情况下,此处为页面的1/2)。对于ggplots的特定情况,我将使用
egg::ggarrange

library(ggplot2)
library(egg)

ggarrange(

  ggplot(mpg, aes(displ, cty)) + 
    geom_point(aes(shape = "Data")) +
    stat_smooth(aes(linetype = "Regression"), method = "lm", 
                formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
    scale_shape_manual(values = 1) +
    labs(shape = "", linetype = "") +
    theme_classic() + 
    theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
          aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
          legend.position = "top")
  ,
  ggplot(mpg, aes(displ, cty)) + 
    geom_point(shape = 1) +
    stat_smooth(method = "lm", 
                formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
    theme_classic() + 
    theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
          aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10))
)

cowplot
的确切功能是什么?嗯,我写它主要是为了制作组合图。我来问你一件事。我使用了
theme\u集(theme\u cowplot(font\u size=8))
,但是
注释
文本没有以这种大小显示。你有什么建议吗?请以单独的问题和示例代码发布。
cowplot
的确切功能是什么?嗯,我写它主要是为了制作组合图。我来问你一件事。我使用了
theme\u集(theme\u cowplot(font\u size=8))
,但是
注释
文本没有以这种大小显示。你有什么建议吗?请单独提问,并附上示例代码。