[英]How to separate a legend to have it partly inside and outside the plot in ggplot?
is there any possible way how to separate a legend in a plot?有什么方法可以在 plot 中分离图例? I want have it partly outside and inside the plot.
我想把它部分放在 plot 的外面和里面。 I tried to separate legend which I wanted inside the plot by
get_legend
(cowplot library) and added as an object by inset_element
from patchwork library...Unfortunately, I cannot customize it and need to inner legend with transparent backround.我试图通过
get_legend
(cowplot 库)在 plot 中分离我想要的图例,并通过补丁库中的inset_element
添加为 object ......不幸的是,我无法自定义它并且需要具有透明背景的内部图例。
This is the ggplot code which I am using at this moment:这是我现在使用的 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 is the object from get_legend function legend_approach 是来自 get_legend function 的 object
Thank you!谢谢!
I can't recreate your plot due to not having your data.由于没有您的数据,我无法重新创建您的 plot。 You can try editing your legend position to a specific location
您可以尝试将您的图例 position 编辑到特定位置
theme(legend.position = c(0.8, 0.2))
You can mess with the specific numbers你可以弄乱特定的数字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.