ggplot 注释刹车轴

问题描述 投票:0回答:1

我想在我的 ggplot 图表中插入文本。文本有一个下标和一个超级下标,例如 X[X} = 1,00*Y R^2 = 0,90。为此,我使用了函数

paste(expression())
annotate(label=())

在图形窗格中,整个图看起来很好,就像我需要的那样。但如果我想使用 svg.save 函数将绘图保存到 svg 中,则绘图会缺少轴和注释文本。

没有注释整个 svg 就可以了。所以我想,解决方案是修复

paste(expression())
函数。但我没有找到解决方案。

Data <- read.csv2("Data.csv", header = TRUE, sep = ";", dec = ",")
Data$ï..Date <- as.Date(Data$ï..Date, format = "%d.%m.%Y")

Gesamt_Plot1 <- ggplot() +
  geom_point(aes(X$EC,Y$BC), show.legend = FALSE, shape = 1, size = 1.5, na.rm = TRUE)+
  geom_abline(linetype = "dashed")+
  geom_segment(aes(x = 0.19,xend = 5.49, y = G1_slp*0.19,  yend = G1_slp*5.49), color = "black", size = 1.2, na.rm = TRUE)+
   annotate(geom="text", x = 4, y=6.4, parse = T, label=paste(expression(EBC~"="~"0,92*"*EC[TOR]~~R²~"="~"0,89")), color="black")+
   annotate(geom="text", x=0.1, y=7, label="a)", color="black")+
   labs(colour = "", x=expression(EC[TOR]~"in"~mu*g~m^-3), y=expression(EBC~"in"~mu*g~m^-3))+
    theme_bw()+
    theme(axis.text.y=element_text(size = 12), axis.text.x = element_text(size = 12),
        axis.title = element_text(size = 12), legend.position = "bottom", panel.grid.minor = element_blank())+
    scale_x_continuous(limit = c(0,7), 
                     breaks = c(0, 1, 2, 3, 4, 5, 6, 7) 
    )+
    scale_y_continuous(limit = c(0,7), 
                     breaks = c(0, 1, 2, 3, 4, 5, 6, 7)
    )

https://i.stack.imgur.com/9axhZ.png

使用 R 版本 4.0.5 和 ggplot v3.3.3。

r ggplot2 svg annotate
1个回答
0
投票

不确定我可以重现您的问题,但我认为您不应该使用

paste(expression(..))
,只需使用
expression(..)
。 (顺便说一句,我在表达式中从你的
改为
R^2
;我不确定这是否是我的 emacs/ess 中的 UTF 问题。如果你愿意,可以尝试两种方法。)

ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  annotate(geom="text", x=20, y=60, 
           label=expression(EBC~"="~"0,92*"*EC[TOR]~~R^2~"="~"0,89"))
# Warning in is.na(x) :
#   is.na() applied to non-(list or vector) of type 'expression'

在 R 图形窗格中,我看到

保存为 svg:

ggsave(filename="mt.svg")
# Warning in is.na(x) :
#   is.na() applied to non-(list or vector) of type 'expression'

然后显示


注意:由于

ggplot2
中长期存在的...错误/功能(?),它会对表达式发出警告。当然,这只是一个警告(并且绘制得很好),因此可以忽略它。请参阅为什么 ggplot 注释会抛出此警告:在 is.na(x) 中:is.na() 应用于“表达式”类型的非(列表或向量)。另一种方法是这样的:

ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  annotate(geom="text", x=20, y=60, 
           label=list('EBC~"="~"0,92*"*EC[TOR]~~R^2~"="~"0,89"'), 
           parse=TRUE) +
  labs(colour = "", x=expression(EC[TOR]~"in"~mu*g~m^-3), y=expression(EBC~"in"~mu*g~m^-3)) +
  theme_bw()

(使用

list('...')
代替
expression(...)
,并添加
parse=TRUE
)。这会产生相同的效果。

© www.soinside.com 2019 - 2024. All rights reserved.