使用下面的代码,我在 Rstudio 中创建了文件gamma0plot.tex
。然后在figure
环境中的 TexNicCenter 中使用input
命令加载文件gamma0plot.tex
并生成 .pdf 文件。在这个 .pdf 中,x 轴的标题是,如何在生成文件时g1
保留字母希腊文而不是?欢迎提出任何其他改进这些图形的建议!$\gamma_{1}$
gamma0plot.tex
g1
library(ggplot2)
library(grid)
grid.newpage()
require(tikzDevice)
# make sure the working directory is where you want your tikz file to go
setwd("D:/Área de Trabalho/teste")
# export plot as a .tex file in the tikz format
tikz('gamma0plot.tex', width = 6,height = 3,pointsize = 12) #define plot name size and font size
grid.newpage()
set.seed(000)
m11 <- matrix(rnorm(1000,0,1),1000,1)
df11 <- data.frame(m11)
names(df11) <- c("X")
library(ggplot2)
g1 <- ggplot(df11, aes(x=X))+coord_cartesian(ylim = c(0,0.6)) +
ggtitle("n=500")+
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(-2, 2, by = 0.1))
g1 <- g1 + stat_function(fun=dnorm,
color="black",geom="area", fill="gray", alpha=0.1,
args=list(mean=mean(df11$X),
sd=sd(df11$X)))
g1 <- g1+ geom_vline(aes(xintercept=0, linetype="Valor Verdadeiro"),show.legend =TRUE)
g1 <- g1+ geom_vline(aes(xintercept=mean(df11$X, 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[1])))+ylab("")
g1 <- g1+ theme(plot.margin=unit(c(0.5, -0.5, 0.5, 0.5), units="line"),
legend.text=element_text(size=6),
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(.3, "cm"),
legend.key.width = unit(0.5, "cm"))
# Get the ggplot Grob
gt1 = ggplotGrob(g1)
# grid.ls(grid.force(gt)) # To get a list of editable grobs
# Edit the relevant keys
library(grid)
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"))
###############################################
m12 <- matrix(rnorm(2000,0,1),2000,1)
df12 <- data.frame(m12)
names(df12) <- c("X")
library(ggplot2)
g2 <- ggplot(df12, aes(x=X)) +coord_cartesian(ylim = c(0,0.6))+
ggtitle("n=1000")+
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(-2, 2, by = 0.1))
g2 <- g2 + stat_function(fun=dnorm,
color="black",geom="area", fill="gray", alpha=0.1,
args=list(mean=mean(df12$X),
sd=sd(df12$X)))
g2 <- g2+ geom_vline(aes(xintercept=0, linetype="Valor Verdadeiro"),show.legend =TRUE)
g2 <- g2+ geom_vline(aes(xintercept=mean(df12$X, 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[1])))+ylab("")
g2 <- g2+ theme(plot.margin=unit(c(0.5, -0.5, 0.5, -0.5), units="line"),
legend.text=element_text(size=6),
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(.3, "cm"),
legend.key.width = unit(0.5, "cm"))
# Get the ggplot Grob
gt2 = ggplotGrob(g2)
# # grid.ls(grid.force(gt)) # To get a list of editable grobs
#
# Edit the relevant keys
library(grid)
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"))
####################################
m13 <- matrix(rnorm(3000,0,1),3000,1)
df13 <- data.frame(m13)
names(df13) <- c("X")
library(ggplot2)
g3 <- ggplot(df13, aes(x=X)) +coord_cartesian(ylim = c(0,0.6)) +
ggtitle("n=2000")+
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(-2, 2, by = 0.1))
g3 <- g3 + stat_function(fun=dnorm,
color="black",geom="area", fill="gray", alpha=0.1,
args=list(mean=mean(df13$X),
sd=sd(df13$X)))
g3 <- g3+ geom_vline(aes(xintercept=0, linetype="Valor Verdadeiro"),show.legend =TRUE)
g3 <- g3+ geom_vline(aes(xintercept=mean(df13$X, 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[1])))+ylab("")
g3 <- g3+ theme(plot.margin=unit(c(0.5, 0.5, 0.5, -0.5), units="line"),
legend.text=element_text(size=6),
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(0.5, "cm"))
# Get the ggplot Grob
gt3 = ggplotGrob(g3)
# grid.ls(grid.force(gt)) # To get a list of editable grobs
# Edit the relevant keys
library(grid)
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)
# export plot as a .tex file in the tikz format
dev.off() # export file and exit tikzDevice function
答案1
我解决了这个问题,只需使用:xlab("$\\gamma_{0}$")