繁体   English   中英

如何在ggplot中将图例部分放在plot的内部和外部?

[英]How to separate a legend to have it partly inside and outside the plot in ggplot?

有什么方法可以在 plot 中分离图例? 我想把它部分放在 plot 的外面和里面。 我试图通过get_legend (cowplot 库)在 plot 中分离我想要的图例,并通过补丁库中的inset_element添加为 object ......不幸的是,我无法自定义它并且需要具有透明背景的内部图例。

这是我现在使用的 ggplot 代码:

ggplot() +
  geom_point(data = tot_grp, aes(x = soc_wat_m, y = obs_m, size = factor(n_group, levels = grp_n), fill = factor(group, levels = grp)), pch = 24) +
  geom_point(data = tot_grp, aes(x = soc_m, y = obs_m, size = factor(n_group, levels = grp_n), fill = factor(group, levels = grp)), pch = 21) +
  
  scale_fill_brewer(name = expression(paste("Classes [",m^-2,"·",yr^-58,"]")), 
                    palette = "PuOr") +
  scale_size_manual(name = "N per class", values = c(1, 2, 3, 4, 5, 6, 7)) +
  guides(fill = guide_legend(override.aes = list(shape = 21, size = 5), order = 2),
         color = guide_legend(override.aes = list(size = 5), order = 1),
         size = guide_legend(override.aes = list(shape = 21), order = 3)) +
  
  labs(x = expression(paste("SOC [kg·",m^-2, "]")),
       y = expression(paste("SOC [kg·",m^-2, "]"))) +
      
  coord_equal()+
  theme_bw()+
  theme(legend.position = "right"
        
  )+
  
  inset_element(p = legend_approach,
                right = 0.5,
                top = 0.55,
                left = 0.95,
                bottom = 0.95,
                clip = TRUE)

legend_approach 是来自 get_legend function 的 object

谢谢!

由于没有您的数据,我无法重新创建您的 plot。 您可以尝试将您的图例 position 编辑到特定位置

theme(legend.position = c(0.8, 0.2))

你可以弄乱特定的数字

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM