【发布时间】:2026-02-03 16:15:01
【问题描述】:
有没有办法让 ggplot 将图例放在顶部但在标题下方?
举个例子……
..使用以下代码生成:
carrots<-list(Yield=c(345,226,74,559,288,194),
Field=c("A","B","C","D","E","F"),
Breed=rep(c("Long","Short"),each=3))
carrots<-data.frame(carrots)
ggplot(carrots,aes(y=Yield,x=Field,fill=Breed)) +
geom_bar() +
opts(title="Title",
legend.direction = "horizontal",
legend.position = "top") +
labs(fill="")
任何建议将不胜感激?
【问题讨论】:
-
下个版本已经修复了这个问题。
-
现在,如果您可以访问 Adobe Illustrator,请将绘图保存为 eps,例如
ggsave("plot.eps")然后将图例移到 AI 的顶部。 -
您可以使用 gridExtra 包微调网格排列的元素并保存不同的元素(不同对象中的绘图、图例、标题)。
-
这似乎在 ggplot2 的开发版本中得到修复:*.com/questions/9656016/… install.packages("devtools") library(devtools) dev_mode(on=T) install_github("ggplot2") # use dev ggplot2 now # 完成后 do: dev_mode(on=F) #and you are back to have stable ggplot2
-
@EtienneLow-Décarie with grid 我不会在 ggplot2 中使用标题,而是在绘图上方手动添加它(例如使用
grid.arrange(plot, main = "title"))