R ggplot2中的图例位置调整

时间:2017-01-25 08:05:47

标签: r ggplot2

如何在图表中放置这个水平图例,使其不会被切断?可以自动扩展边距并使图例容器与绘图保持对齐吗?我经常搜索,但找不到合理的方法。

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)

enter image description here

0 个答案:

没有答案