Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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 geom_文本仅在缩放绘图时显示;韩元';在查看器中或保存到文件时不显示_R_Ggplot2 - Fatal编程技术网

R geom_文本仅在缩放绘图时显示;韩元';在查看器中或保存到文件时不显示

R geom_文本仅在缩放绘图时显示;韩元';在查看器中或保存到文件时不显示,r,ggplot2,R,Ggplot2,我想将文本添加到刻面的绘图中。为此,我在原始数据文件中添加了一列,其中包含我想要绘制的文本,然后通过geom_文本添加,正如其他各种帖子中所指定的那样。但是,仅当我在RStudio中放大绘图时,文本才会显示-它不会显示在绘图查看器面板中,更重要的是,当我尝试将绘图保存到pdf或png文件时,文本不会显示 我尝试使用注释而不是geom_文本,并以多种方式修改ggsave函数(例如,通过向调用中添加设备和更改格式大小)。我试图改变geom_文本的大小,但这似乎也不起作用。可能有一些简单的问题,但我看

我想将文本添加到刻面的绘图中。为此,我在原始数据文件中添加了一列,其中包含我想要绘制的文本,然后通过geom_文本添加,正如其他各种帖子中所指定的那样。但是,仅当我在RStudio中放大绘图时,文本才会显示-它不会显示在绘图查看器面板中,更重要的是,当我尝试将绘图保存到pdf或png文件时,文本不会显示

我尝试使用注释而不是geom_文本,并以多种方式修改ggsave函数(例如,通过向调用中添加设备和更改格式大小)。我试图改变geom_文本的大小,但这似乎也不起作用。可能有一些简单的问题,但我看不出来

library(tidyverse)

###Creates the df I"m using 

explanations <- tribble(
  ~study, ~model, ~comparison, ~ame, ~se, ~p, ~yax, ~fstat, 
  "Study 1", "Co-Partisans", "By Itself",  0.16043, 0.02521, 0.001, "Overall Effects", "",
  "Study 1", "Co-Partisans", "With Counter",  -0.04861, 0.02011, 0.01588, 
      "Overall Effects", "Sig diff from Explanation, p < 0.001", 
  "Study 1", "Co-Partisans", "With Counter (High Credibility)",  -0.07990, 0.02103, 0.000157, 
  "Overall Effects", "Sig diff from Explanation, p < 0.001",
  "Study 1", "Co-Partisans", "With Counter (Low Credibility)",  0.02350, 0.02550, 0.357079,
  "Overall Effects", "Sig diff from Explanation, p < 0.05",
  "Study 2", "Co-Partisans", "By Itself", 0.01168, 0.01589, 0.46261, "Overall Effects", "", 
  "Study 2", "Co-Partisans", "With Counter", -0.03962, 0.01387, 0.00439,"Overall Effects", 
  "Sig diff from Explanation, p < 0.001",
  "Study 2", "Co-Partisans", "With Counter (High Credibility)",  -0.05847, 0.01602, 0.000282,
  "Overall Effects", "Sig diff from Explanation, p < 0.001",
  "Study 2", "Co-Partisans", "With Counter (Low Credibility)",      -0.02062, 0.01606, 0.199389,
  "Overall Effects", "Sig diff from Explanation, p < 0.05")

###Plots the figure
library(ggstance)

explanations %>%
  tbl_df %>%
  ggplot() +
  geom_vline(xintercept=0, 
             linetype=2) +
  geom_pointrangeh(aes(y=yax, x=ame, xmin=ame-(1.96*se), xmax=ame+(1.96*se))) + 
  scale_y_discrete(
    breaks = explanations$yax %>%
      levels, 
    labels = explanations$yax %>%
      levels %>%
      str_to_title
  ) + 
  facet_grid(comparison ~ study, 
             labeller = label_wrap_gen(width=13), 
             switch = "y") + 
  theme_minimal(20) + 
  theme(panel.background=element_rect(color="grey96", fill = "grey96"), 
        strip.background = element_rect(color="grey96", fill="grey96"),
        panel.grid=element_blank() ,
        strip.text = element_text(face="bold"), 
        strip.text.y = element_text(angle=180), 
        plot.title = element_text(hjust=0.5)
  ) + 
  labs(title = "Effect of Explanation \nwith and without Counters" , 
       y=NULL, x="Marginal Effect \nPositive = The Explantion 'Works'") + 
  geom_text(data = explanations, aes(x=-Inf, y=-Inf, label=fstat, 
                hjust = -1.90,
                vjust = -5.5))

ggsave("figure.png", height=6, width=8)
库(tidyverse)
###创建我正在使用的df
解释%
tbl_df%>%
ggplot()+
几何线(xintercept=0,
线型=2)+
geom_pointrangeh(aes(y=yax,x=ame,xmin=ame-(1.96*se),xmax=ame+(1.96*se))+
比例y离散(
中断=解释$yax%>%
水平,
标签=解释$yax%>%
级别%>%
str_to_title
) + 
分面网格(对比研究),
贴标机=贴标机包装机(宽度=13),
开关=“y”)+
主题(20)
主题(panel.background=element_rect(color=“grey96”,fill=“grey96”),
strip.background=元素(color=“grey96”,fill=“grey96”),
panel.grid=element\u blank(),
strip.text=元素\文本(face=“bold”),
strip.text.y=元素\文本(角度=180),
plot.title=元素\文本(hjust=0.5)
) + 
实验室(title=“解释的效果\n有或没有计数器”,
y=NULL,x=“边际效应\n积极性=解释‘工作’”+
geom_文本(数据=解释,aes(x=-Inf,y=-Inf,label=fstat,
hjust=-1.90,
vjust=-5.5))
ggsave(“figure.png”,高度=6,宽度=8)
绘图应在所有面上显示“fstat”文本(例如,“Sig diff from…”),并保存前两个面。确实如此……但只有当我通过RStudio上的缩放功能查看图形时,保存的输出才不会在面上打印“Sig diff…”文本。我哪里出错了


如果有任何帮助,我们将不胜感激!

当绘图显示在您的“绘图”选项卡中时,请单击“缩放”按钮。然后尝试将窗口变小变大,看看文本会发生什么;)啊,很有趣。这确实会影响文本的外观(例如,如果太小,它会消失)。我也尝试过增大pdf的大小(例如,高度=10,宽度=12,而不是8和10)现在,文本开始向图像中窥视。有没有办法让文本显示出来,而不将图像放大到巨大的尺寸?我最终会将图形放入Beamer演示文稿中,因此可能那里的inlcudegraphics命令会解决这个问题……试着使用一些屏幕标尺:D Set“Plot“将窗格设置为所需大小。然后操纵
geom_text
s参数使文本可见,最后使用
ggsave
保存它,而不指定
宽度和
高度
参数,因此它将取用户的“绘图”窗格维度。我知道这听起来很愚蠢;但是我在互联网上查找了很多关于这个问题的解决方案,但没有找到任何解决方案。