使用多个方面播放 ggplot2 的图形

fsbmat

我建立下面使用该命令的图形grid.arrangegridExtra包,想与所述多刻面图的视觉效果离开它,使用样品尺寸小,这可能吗?

library(ggplot2)
library(grid)
library(utils)
grid.newpage()
#Lendo os dados
dt1 <- read.table("https://cdn.rawgit.com/fsbmat/StackOverflow/master/sim50.txt", header = TRUE)
attach(dt1)
#head(dt1)
dt2 <- read.table("https://cdn.rawgit.com/fsbmat/StackOverflow/master/sim200.txt", header = TRUE)
attach(dt2)
#head(dt2)
dt3 <- read.table("https://cdn.rawgit.com/fsbmat/StackOverflow/master/sim1000.txt", header = TRUE)
attach(dt3)
#head(dt3)
g1 <- ggplot(dt1, aes(x=dt1$gamma0))+coord_cartesian(xlim=c(-1.3,3.3),ylim = c(0,0.63)) +
  ggtitle("n=50")+ 
  theme(plot.title = element_text(margin = margin(b = 2),size = 6,hjust = 0))
g1 <- g1+geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                        binwidth=.5,
                        colour="black", fill="white",breaks=seq(-3, 3, by = 0.1)) 
g1 <- g1 + stat_function(fun=dnorm,
                         color="black",geom="area", fill="gray", alpha=0.1,
                         args=list(mean=mean(dt1$gamma0), 
                                   sd=sd(dt1$gamma0)))
g1 <- g1+  geom_vline(aes(xintercept=1.23, linetype="Valor Verdadeiro"),show.legend =TRUE)
g1 <- g1+  geom_vline(aes(xintercept=mean(dt1$gamma0, na.rm=T),    linetype="Valor Estimado"),show.legend =TRUE)
g1 <- g1+  scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot
g1 <- g1+  xlab(expression(paste(gamma[0])))+ylab("")
g1 <- g1+  theme(plot.margin=unit(c(0.1, 0.1, 0.1, 0.1), units="line"),
                 legend.text=element_text(size=5),
                 legend.position = c(0, 0.97),
                 legend.justification = c("left", "top"),
                 legend.box.just = "left",
                 legend.margin = margin(0,0,0,0),
                 legend.title=element_blank(),
                 legend.direction = "vertical",
                 legend.background = element_rect(colour = NA,fill="transparent", size=.5, linetype="dotted"),
                 legend.key = element_rect(colour = "transparent", fill = NA))
g1 <- g1+ guides(linetype = guide_legend(override.aes = list(size = 0.5)))


# Adjust key height and width
g1 = g1 + theme(
  legend.key.height = unit(0.3, "cm"),
  legend.key.width  = unit(0.4, "cm"))

# Get the ggplot Grob
gt1 = ggplotGrob(g1)

# Edit the relevant keys

gt1 <- editGrob(grid.force(gt1), gPath("key-[3,4]-1-[1,2]"), 
                grep = TRUE, global = TRUE,
                x0 = unit(0, "npc"), y0 = unit(0.5, "npc"), 
                x1 = unit(1, "npc"), y1 = unit(0.5, "npc")) 

###################################################

g2 <- ggplot(dt2, aes(x=dt2$gamma0))+coord_cartesian(xlim=c(0,3),ylim = c(0,1.26)) +
  ggtitle("n=200")+ 
  theme(plot.title = element_text(margin = margin(b = 2),size = 6,hjust = 0))
g2 <- g2+geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                        binwidth=.5,
                        colour="black", fill="white",breaks=seq(-3, 3, by = 0.1)) 
g2 <- g2 + stat_function(fun=dnorm,
                         color="black",geom="area", fill="gray", alpha=0.1,
                         args=list(mean=mean(dt2$gamma0), 
                                   sd=sd(dt2$gamma0)))
g2 <- g2+  geom_vline(aes(xintercept=1.23, linetype="Valor Verdadeiro"),show.legend =TRUE)
g2 <- g2+  geom_vline(aes(xintercept=mean(dt2$gamma0, na.rm=T),    linetype="Valor Estimado"),show.legend =TRUE)
g2 <- g2+  scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot
g2 <- g2+  xlab(expression(paste(gamma[0])))+ylab("")
g2 <- g2+  theme(plot.margin=unit(c(0.1, 0.1, 0.1, 0.1), units="line"),
                 legend.text=element_text(size=5),
                 legend.position = c(0, 0.97),
                 legend.justification = c("left", "top"),
                 legend.box.just = "left",
                 legend.margin = margin(0,0,0,0),
                 legend.title=element_blank(),
                 legend.direction = "vertical",
                 legend.background = element_rect(colour = NA,fill="transparent", size=.5, linetype="dotted"),
                 legend.key = element_rect(colour = "transparent", fill = NA))
g2 <- g2+ guides(linetype = guide_legend(override.aes = list(size = 0.5)))

# Adjust key height and width
g2 = g2 + theme(
  legend.key.height = unit(0.3, "cm"),
  legend.key.width = unit(0.4, "cm"))

# Get the ggplot Grob
gt2 = ggplotGrob(g2)

# Edit the relevant keys

gt2 <- editGrob(grid.force(gt2), gPath("key-[3,4]-1-[1,2]"), 
                grep = TRUE, global = TRUE,
                x0 = unit(0, "npc"), y0 = unit(0.5, "npc"), 
                x1 = unit(1, "npc"), y1 = unit(0.5, "npc")) 

#######################################################

g3 <- ggplot(dt3, aes(x=gamma0))+coord_cartesian(xlim=c(0.6,1.8),ylim = c(0,2.6)) +
  ggtitle("n=1000")+ 
  theme(plot.title = element_text(margin = margin(b = 2),size = 6,hjust = 0))
g3 <- g3+geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                        binwidth=.5,
                        colour="black", fill="white",breaks=seq(-3, 3, by = 0.1)) 
g3 <- g3 + stat_function(fun=dnorm,
                         color="black",geom="area", fill="gray", alpha=0.1,
                         args=list(mean=mean(dt3$gamma0), 
                                   sd=sd(dt3$gamma0)))
g3 <- g3+  geom_vline(aes(xintercept=1.23, linetype="Valor Verdadeiro"),show.legend =TRUE)
g3 <- g3+  geom_vline(aes(xintercept=mean(dt3$gamma0, na.rm=T),    linetype="Valor Estimado"),show.legend =TRUE)
g3 <- g3+  scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot
g3 <- g3+  xlab(expression(paste(gamma[0])))+ylab("")
g3 <- g3+  theme(plot.margin=unit(c(0.1, 0.1, 0.1, 0.1), units="line"),
                 legend.text=element_text(size=5),
                 legend.position = c(0, 0.97),
                 legend.justification = c("left", "top"),
                 legend.box.just = "left",
                 legend.margin = margin(0,0,0,0),
                 legend.title=element_blank(),
                 legend.direction = "vertical",
                 legend.background = element_rect(colour = NA,fill="transparent", size=.5, linetype="dotted"),
                 legend.key = element_rect(colour = "transparent", fill = NA))
g3 <- g3+ guides(linetype = guide_legend(override.aes = list(size = 0.5)))


# Adjust key height and width
g3 = g3 + theme(
  legend.key.height = unit(.3, "cm"),
  legend.key.width = unit(.4, "cm"))

# Get the ggplot Grob
gt3 = ggplotGrob(g3)

# Edit the relevant keys

gt3 <- editGrob(grid.force(gt3), gPath("key-[3,4]-1-[1,2]"), 
                grep = TRUE, global = TRUE,
                x0 = unit(0, "npc"), y0 = unit(0.5, "npc"), 
                x1 = unit(1, "npc"), y1 = unit(0.5, "npc")) 

####################################################

library(gridExtra)
grid.arrange(gt1, gt2, gt3, widths=c(0.3,0.3,0.3), ncol=3)

注:我想复制相同的图形,但在facet_wrap的格式,使用n = 50n = 200n = 1000作为面。

fsbmat

我得到它使用grid.arrange

library(ggplot2)
library(grid)
library(utils)
grid.newpage()

dt1 <- read.table("https://cdn.rawgit.com/fsbmat/StackOverflow/master/sim50.txt", header = TRUE)
attach(dt1)
#head(dt1)
dt2 <- read.table("https://cdn.rawgit.com/fsbmat/StackOverflow/master/sim200.txt", header = TRUE)
attach(dt2)
#head(dt2)
dt3 <- read.table("https://cdn.rawgit.com/fsbmat/StackOverflow/master/sim1000.txt", header = TRUE)
attach(dt3)
#head(dt3)
g1 <- ggplot(dt1, aes(x=dt1$gamma0))+coord_cartesian(xlim=c(-1.3,3.3),ylim = c(0,0.63)) +
  theme(plot.title = element_text(margin = margin(b = 2),size = 6,hjust = 0))
g1 <- g1+geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                        binwidth=.5,
                        colour="black", fill="white",breaks=seq(-3, 3, by = 0.1)) 
g1 <- g1 + stat_function(fun=dnorm,
                         color="black",geom="area", fill="gray", alpha=0.1,
                         args=list(mean=mean(dt1$gamma0), 
                                   sd=sd(dt1$gamma0)))
g1 <- g1+  geom_vline(aes(xintercept=1.23, linetype="Valor Verdadeiro"),show.legend =TRUE)
g1 <- g1+  geom_vline(aes(xintercept=mean(dt1$gamma0, na.rm=T),    linetype="Valor Estimado"),show.legend =TRUE)
g1 <- g1+  scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot
g1 <- g1+  xlab(expression(paste(gamma[0])))+ylab("densidade")
g1 <- g1+  annotate("rect", xmin = -2, xmax = 3.52, ymin = 0.635, ymax = 0.7, fill="gray")
g1 <- g1+  annotate("text", x = 1.23, y = 0.65, label = "n=50", size=4)
g1 <- g1+  theme(plot.margin=unit(c(0.1, 0.1, 0.1, 0.1), units="line"),
                 legend.text=element_text(size=5),
                 legend.position = c(0, 0.97),
                 legend.justification = c("left", "top"),
                 legend.box.just = "left",
                 legend.margin = margin(0,0,0,0),
                 legend.title=element_blank(),
                 legend.direction = "vertical",
                 legend.background = element_rect(colour = NA,fill="transparent", size=.5, linetype="dotted"),
                 legend.key = element_rect(colour = "transparent", fill = NA),
                 axis.text.y=element_blank(),
                 axis.ticks.y=element_blank())
g1 <- g1+ guides(linetype = guide_legend(override.aes = list(size = 0.5)))


# Adjust key height and width
g1 = g1 + theme(
  legend.key.height = unit(0.3, "cm"),
  legend.key.width  = unit(0.4, "cm"))

# Get the ggplot Grob
gt1 = ggplotGrob(g1)

# Edit the relevant keys

gt1 <- editGrob(grid.force(gt1), gPath("key-[3,4]-1-[1,2]"), 
                grep = TRUE, global = TRUE,
                x0 = unit(0, "npc"), y0 = unit(0.5, "npc"), 
                x1 = unit(1, "npc"), y1 = unit(0.5, "npc")) 

###################################################

g2 <- ggplot(dt2, aes(x=dt2$gamma0))+coord_cartesian(xlim=c(0,3),ylim = c(0,1.26)) +
  theme(plot.title = element_text(margin = margin(b = 2),size = 6,hjust = 0))
g2 <- g2+geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                        binwidth=.5,
                        colour="black", fill="white",breaks=seq(-3, 3, by = 0.1)) 
g2 <- g2 + stat_function(fun=dnorm,
                         color="black",geom="area", fill="gray", alpha=0.1,
                         args=list(mean=mean(dt2$gamma0), 
                                   sd=sd(dt2$gamma0)))
g2 <- g2+  geom_vline(aes(xintercept=1.23, linetype="Valor Verdadeiro"),show.legend =TRUE)
g2 <- g2+  geom_vline(aes(xintercept=mean(dt2$gamma0, na.rm=T),    linetype="Valor Estimado"),show.legend =TRUE)
g2 <- g2+  scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot
g2 <- g2+  xlab(expression(paste(gamma[0])))+ylab("")
g2 <- g2+  annotate("rect", xmin = -2, xmax = 3.5, ymin = 1.27, ymax = 1.8, fill="gray")
g2 <- g2+  annotate("text", x = 1.23, y = 1.3, label = "n=200", size=4)

g2 <- g2+  theme(plot.margin=unit(c(0.1, 0.1, 0.1, 0.1), units="line"),
                 legend.text=element_text(size=5),
                 legend.position = c(0, 0.97),
                 legend.justification = c("left", "top"),
                 legend.box.just = "left",
                 legend.margin = margin(0,0,0,0),
                 legend.title=element_blank(),
                 legend.direction = "vertical",
                 legend.background = element_rect(colour = NA,fill="transparent", size=.5, linetype="dotted"),
                 legend.key = element_rect(colour = "transparent", fill = NA),
                 axis.title.y=element_blank(),
                 axis.text.y=element_blank(),
                 axis.ticks.y=element_blank())
g2 <- g2+ guides(linetype = guide_legend(override.aes = list(size = 0.5)))

# Adjust key height and width
g2 = g2 + theme(
  legend.key.height = unit(0.3, "cm"),
  legend.key.width = unit(0.4, "cm"))

# Get the ggplot Grob
gt2 = ggplotGrob(g2)

# Edit the relevant keys

gt2 <- editGrob(grid.force(gt2), gPath("key-[3,4]-1-[1,2]"), 
                grep = TRUE, global = TRUE,
                x0 = unit(0, "npc"), y0 = unit(0.5, "npc"), 
                x1 = unit(1, "npc"), y1 = unit(0.5, "npc")) 

#######################################################

g3 <- ggplot(dt3, aes(x=gamma0))+coord_cartesian(xlim=c(0.6,1.8),ylim = c(0,2.6)) +
  theme(plot.title = element_text(margin = margin(b = 2),size = 6,hjust = 0))
g3 <- g3+geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                        binwidth=.5,
                        colour="black", fill="white",breaks=seq(-3, 3, by = 0.1)) 
g3 <- g3 + stat_function(fun=dnorm,
                         color="black",geom="area", fill="gray", alpha=0.1,
                         args=list(mean=mean(dt3$gamma0), 
                                   sd=sd(dt3$gamma0)))
g3 <- g3+  geom_vline(aes(xintercept=1.23, linetype="Valor Verdadeiro"),show.legend =TRUE)
g3 <- g3+  geom_vline(aes(xintercept=mean(dt3$gamma0, na.rm=T),    linetype="Valor Estimado"),show.legend =TRUE)
g3 <- g3+  scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot
g3 <- g3+  labs(x=expression(paste(gamma[0])), y="")
g3 <- g3+  annotate("rect", xmin = -2, xmax = 1.875, ymin = 2.62, ymax = 3, fill="gray")
g3 <- g3+  annotate("text", x = 1.23, y = 2.68, label = "n=1000", size=4)
g3 <- g3+  theme(plot.margin=unit(c(0.1, 0.1, 0.1, 0.1), units="line"),
                 legend.text=element_text(size=5),
                 legend.position = c(0, 0.97),
                 legend.justification = c("left", "top"),
                 legend.box.just = "left",
                 legend.margin = margin(0,0,0,0),
                 legend.title=element_blank(),
                 legend.direction = "vertical",
                 legend.background = element_rect(colour = NA,fill="transparent", size=.5, linetype="dotted"),
                 legend.key = element_rect(colour = "transparent", fill = NA),
                 axis.title.y=element_blank(),
                 axis.text.y=element_blank(),
                 axis.ticks.y=element_blank())
g3 <- g3+ guides(linetype = guide_legend(override.aes = list(size = 0.5)))


# Adjust key height and width
g3 = g3 + theme(
  legend.key.height = unit(.3, "cm"),
  legend.key.width = unit(.4, "cm"))

# Get the ggplot Grob
gt3 = ggplotGrob(g3)

# Edit the relevant keys

gt3 <- editGrob(grid.force(gt3), gPath("key-[3,4]-1-[1,2]"), 
                grep = TRUE, global = TRUE,
                x0 = unit(0, "npc"), y0 = unit(0.5, "npc"), 
                x1 = unit(1, "npc"), y1 = unit(0.5, "npc")) 

####################################################

library(gridExtra)
grid.arrange(gt1, gt2, gt3, widths=c(0.1,0.1,0.1), ncol=3)

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用ggplot2时持续出现无效的图形状态错误

如何使用ggplot2在R中制作具有透明背景的图形?

向ggplot2图形添加多个阴影/矩形

使用R将带箭头的单箭头添加到ggplot2中的图形中

如何在多个多方面ggplot2图形中实现相同的方面大小和比例?

ggplot2和Shiny:如何使用图形大小缩放图例的大小?

如何制作具有多个几何形状的自定义ggplot2几何图形

如何使用ggplot2为图形编号

ggplot2:如何通过多个变量为图形着色

手动缩放多个几何图形的颜色,形状和线型ggplot2

使用ggplot2在单个图中以图形方式表示许多曲线

在ggplot2中使用for循环保存图形

使用Shiny和ggplot2在单个图形上绘制多条线

如何使用ggplot2图例表示不同的几何图形

在ggplot2中使用pmap自动绘制不同数据集的图形

如何使用ggplot2清理数据并创建图形?

使用R中的ggplot2按顺序循环打印许多图形(PDF)

在ggplot2中使用多个箱形图创建图形

使用ggplot2 / Rstudio进行慢速图形渲染-GPU问题?

ggplot2是否使用/继承R基本图形中的某些参数?

使用ggplot绘制多个图形

使用ggplot2的图形

使用ggplot2在嵌套数据组中的图形上连接点

使用ggplot2的多个方面

R-使用空标签从ggplot2图形中转换NA标签

使用带有 ggplot2 的 for 循环在数据框中绘制多个图形

我需要使用 ggplot2 更改每个图形的网格高度和大小

如何使用ggplot2填充图形的区域

在 R 中绑定多个 csv 以输出到 ggplot2 中的图形