R ggplot2中的图例位置调整

R ggplot2中的图例位置调整,r,ggplot2,R,Ggplot2,如何将此水平图例放入图表中,使其不被截断?是否可以自动扩展边距,并使图例容器与绘图保持对齐?我找了很多,但找不到一个合理的方法来做这件事 library(ggplot2) names<-c("Bartramiaceae", "Catoscopiaceae", "Phyllodrepaniaceae","Pseudoditrichaceae","Hypopterygiaceae","Brachytheciaceae","Pterobryellaceae") ggplot(data.fra

如何将此水平图例放入图表中,使其不被截断?是否可以自动扩展边距,并使图例容器与绘图保持对齐?我找了很多,但找不到一个合理的方法来做这件事

library(ggplot2)

names<-c("Bartramiaceae", "Catoscopiaceae", "Phyllodrepaniaceae","Pseudoditrichaceae","Hypopterygiaceae","Brachytheciaceae","Pterobryellaceae")

ggplot(data.frame(x=names, y=paste(names,names), shape=sample(c("one shape", "another shape"), 7,replace=T),
                  colour=sample(c("one colour", "another colour","third colour"), 7,replace=T),
                  size=sample(c(10, 100, 300),7,replace=T)), 
       aes(x=x, y=y, colour=colour, size=size, shape=shape)) + geom_point() + 
theme(legend.direction="horizontal",
      legend.box = "horizontal",
      legend.position="top", 
      axis.text.x = element_text(angle = 90, hjust = 1),
      axis.title.x=element_blank(),
      axis.title.y=element_blank()) + 
scale_y_discrete(position="right", expand=c(0.01, 0.01))

ggsave("legend_plot.png",w=8, h=8)
库(ggplot2)

名称增加图形设备的尺寸。(您应该使用
ggsave
导出绘图。它允许您指定高度和宽度以及像素图形的分辨率。)谢谢。我更愿意保持绘图和轴字体的大小相同,但只需重新调整图例即可。正如您在更新的变体中所看到的,右侧有大量的空间(我很乐意添加更多),但自动图例仍然被切断。您是否愿意为图例设置两行?您还可以调整
图例。对齐
。图例。对齐=c(0,1)与图例结合使用。位置=c(0,1)起作用,但是图例与绘图区域重叠,如果超过1,图例将不可见。