tikzDevice 패키지를 사용하여 플롯에 그리스 문자 생성

tikzDevice 패키지를 사용하여 플롯에 그리스 문자 생성

아래 코드를 사용하여 Rstudio에서 gamma0plot.tex. 그런 다음 환경의 TexNicCenter에서 명령을 figure사용하여 input파일을 로드 gamma0plot.tex하고 .pdf 파일을 생성합니다. 이 .pdf에서 x축의 제목은 입니다 . 대신 파일을 생성하는 동안 g1그리스어 문자를 어떻게 유지합니까 ? 이러한 그래픽을 개선하기 위한 다른 제안도 환영합니다!$\gamma_{1}$gamma0plot.texg1

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

관련 정보