Multipart (n>2) legends
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 30/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- r
- Lĩnh vực
- data-visualization
Hướng nghiên cứu
Bắt đầu bằng cách đọc các hàm draw_legend và tinylegend, đặc biệt là cách các lệnh gọi recordGraphics đệ quy hoặc chồng lớp ảnh hưởng đến các phép tính inset và tọa độ được chuẩn hóa. So sánh hành vi hiện tại của hai chú giải với ví dụ gồm ba phần trong issue, sau đó xác định và kiểm chứng việc hỗ trợ chú giải gồm n phần tùy ý nên có nghĩa là gì.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Adapted from my original comment in the "bubble'" PR here.
Relevant for #31 (#343), #210 and #455, since we'd need to enable multi-part legends irrespective of whether we decide to support (nested and cross) grouping by other graphical parameters, let alone settle on an API for doing so.
Background (dual legends)
One way to create dual (n = 2) legends in vanilla base plot is by using an inset of 0.5 plus an adjustment term. Assuming we don't want a gap between the two legends (i.e., they can be directly adjacent), we have:
- For the 'top' legend this adjustment term is zero, since the 0.5 inset already forces the (bottom of the) legend rectangle relative to the middle of the plot
- For the 'bottom' legend, the adjustment term is the top height of the rectangle, adjusted in terms of normalized plot coordinates (
"npc"), since we want to push this second legend down by its entire height.
A simple example to illustrate:
plot(0:10, type = 'n')
abline(h = 5, lty = 2)
legend("bottomright", "a", pch=1, title= "Legend 1",inset = c(0, 0.5))
l2 = legend("bottomright", c("b", 'c'), pch=1, title= "Legend 2", plot = FALSE)
# l2h = l2$rect$h ## use top, rather than h
l2t = l2$rect$top
l2h = grconvertY(l2t, to = 'npc')
legend("bottomright", c("b", 'c'), pch=1, title= "Legend 2",inset = c(0, 0.5-l2h))

Created on 2025-09-05 with reprex v2.1.1
The problem... and an alternative workaround
Unfortunately, try as I might, I have not been able to make this simple approach work for tinyplot. I honestly don't know why; it could be because of recordGraphics adjustments, our bespoke margin tweaks, or something else. Update: After some further experimentation, I think it's because of (overlayed/recursive) recordGraphics calls. If correct, fixing this would require some bespoke logic in the underlying draw_legend/tinylegend functions.
Luckily, there's another approach that we can use for dual axes. Specifically, we use the same 0.5 inset trick, but coming from opposite edges of the plot (e.g., "bottomright" vs "topright"). In this case, the solution is even simpler since you don't need to calculate an additional adjustment term:
plot(0:10, type = 'n')
abline(h = 5, lty = 2)
legend("bottomright", "a", pch=1, title= "Legend 1",inset = c(0, 0.5))
legend("topright", c("b", 'c'), pch=1, title= "Legend 2", inset = c(0, 0.5))

Created on 2025-09-05 with reprex v2.1.1
The good news is that this latter approach does work with tinyplot and this is what my PR here does underneath the hood.
So what (n > 2 legends)?
The second approach is simpler and gives exactly the same result for this dual legend case. So, why worry? The problem is that it can't scale for multi-legends (> dual legends), b/c it fully depends on the "opposite edges" logic... and thus limits you to two (opposite) legends.
If we'd like to support, say, three-part legends down the road, then we'd have to figure out why the first approach isn't working. And then you could do something in spirit of:
plot(0:10, type = 'n')
abline(h = 5, lty = 2)
l1 = legend("bottomright", "a", pch=1, title= "Legend 1", plot = FALSE)
l2 = legend("bottomright", c("b", 'c'), pch=1, title= "Legend 2", plot = FALSE)
l3 = legend("bottomright", "d", pch=2, title= "Legend 3", plot = FALSE)
l1h = grconvertY(l1$rect$top, to = 'npc')
l2h = grconvertY(l2$rect$top, to = 'npc')
l3h = grconvertY(l3$rect$top, to = 'npc')
lh_tot = l1h + l2h + l3h
adj = 0.5+lh_tot/2
l1 = legend("bottomright", "a", pch=1, title= "Legend 1", inset = c(0, adj-l1h))
l2 = legend("bottomright", c("b", 'c'), pch=1, title= "Legend 2", inset = c(0, adj-l1h-l2h))
l3 = legend("bottomright", "d", pch=2, title= "Legend 3", inset = c(0, adj-l1h-l2h-l3h))

Created on 2025-09-05 with reprex v2.1.1
Until then, we're stuck with dual legends at a maximum.
- Ngôn ngữ chính
- R
- Star
- 430
- Fork
- 24
- Merge trung bình
- 14 giờ 17 phút
- Pull request đã merge (30 ngày)
- 8
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Không có hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của grantmcdermott/tinyplot
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 63/100
grantmcdermott/tinyplot#759 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 67/100
grantmcdermott/tinyplot#664 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
grantmcdermott/tinyplot#663 · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Spineplot wishlistĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
grantmcdermott/tinyplot#660 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 62/100
grantmcdermott/tinyplot#659 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của grantmcdermott/tinyplot
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
bcgov/aqwarnings_shiny#128 ·
-
tech-debt
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
sagemathinc/sagebrush#3570 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug upkeep
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
phoible.feature() failsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
ropensci/lingtypology#94 ·