如何在 ggplot2 中的图形中写入额外的标题?

How to write additional captions in a graphic in ggplot2?

我有这个数据:

Animal  Age GC  production  Sire
1   220 5   945 1
2   246 5   870 1
3   210 5   430 2
4   270 5   415 2
5   225 6   750 1
6   198 6   730 1
7   227 6   280 2
8   221 6   295 2

我使用了这段代码:

data=read.xlsx("arquivo.xlsx",1)

data.1 = data %>%
  group_by(GC) %>%
  dplyr::summarize(Mean = mean(na.omit(production)))

tiff("exemplo.tiff", width =10 , height =6 , units = 'in', res = 400, compression = 'none')
ggplot(data.1, aes(x=GC, y=Mean)) + 
  geom_bar(stat="identity", width=0.5)  +
  scale_y_continuous(breaks = round(seq(min(0), max(700), by = 100), digits=2),limits=c(0,700))+
  geom_text(aes(label = round(Mean, 1)), position = position_dodge(0.9), vjust = -0.3)  +
  geom_hline(yintercept=513.75, linetype="dashed", color = "red")+
  labs(x = "GC" ,y="Production") + 
  labs(subtitle="General", title= "Production kg") + 
  theme ()
dev.off()

我得到了这张图:

但我想在标题中添加更多信息,例如值 665 和 513.8。我想把 "Production = 665 kg" 和 "Production = 513.8 kg",在同一个地方,但有额外的信息。我试过这个:

geom_text(aes(label = round(paste0("Production = "Mean, 1, paste0("kg"))), position = position_dodge(0.9), vjust = -0.3)

但是没用。

您需要在 Mean 中的 round 之后添加 paste0,因为它对 round 字符值没有意义。

library(ggplot2)

ggplot(data.1, aes(x=GC, y=Mean)) + 
  geom_bar(stat="identity", width=0.5)  +
  scale_y_continuous(breaks = round(seq(min(0), max(700), by = 100),
                     digits=2),limits=c(0,700))+
  geom_text(aes(label = paste0("Production = ", round(Mean, 1), " kg")), 
                position = position_dodge(0.9), vjust = -0.3)  +
  geom_hline(yintercept=513.75, linetype="dashed", color = "red")+
  labs(x = "GC" ,y="Production") + 
  labs(subtitle="General", title= "Production kg") + 
  theme ()