Phương pháp này tương tự như phương pháp Sandys khi nó đưa ra chú giải là các đối tượng riêng biệt và cho phép bạn thực hiện vị trí độc lập. Nó được thiết kế sơ bộ cho nhiều truyền thuyết thuộc về hai hoặc nhiều ô trong một ô lưới.
Dường như kinda phức tạp và thời gian/mã comsuming nhưng thiết lập một lần, bạn có thể thích ứng và sử dụng nó cho tất cả các loại tùy chỉnh âm mưu/truyền thuyết.
library(ggplot2)
library(cowplot)
# set up function
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend) }
# Some data
df <- data.frame(
Name = factor(rep(c("A", "B", "C"), 12)),
Month = factor(rep(1:12, each=3)),
Temp = sample(0:40, 12),
Precip = sample(50:400, 12))
# create plot1
plot1 <- ggplot(df, aes(Month, Temp, fill = Name)) +
geom_point(show.legend = F, aes(group = Name, colour = Name),
size = 3, shape = 17) +
geom_smooth(method = "loess", se = F,
aes(group = Name, colour = Name),
show.legend = F, size = 0.5, linetype = "dashed")
# create plot2
plot2 <- ggplot(df, aes(Month, Precip, fill = Name)) +
geom_bar(stat = "identity", position = "dodge", show.legend = F) +
geom_smooth(method = "loess", se = F,
aes(group = Name, colour = Name),
show.legend = F, size = 1, linetype = "dashed") +
scale_fill_grey()
# create legend1
legend1 <- ggplot(df, aes(Month, Temp)) +
geom_point(show.legend = T, aes(group = Name, colour = Name),
size = 3, shape = 17) +
geom_smooth(method = "loess", se = F,aes(group = Name, colour = Name),
show.legend = T, size = 0.5, linetype = "dashed") +
labs(colour = "Station") +
theme(legend.text=element_text(size=8),
legend.title = element_text(face = "italic",
angle = -0, size = 10))
# create legend2
legend2 <- ggplot(df, aes(Month, Precip, fill = Name)) +
geom_bar(stat = "identity", position = "dodge", show.legend = T) +
scale_fill_grey() +
guides(fill =
guide_legend(title = "",
title.theme = element_text(face = "italic",
angle = -0, size = 10))) +
theme(legend.text=element_text(size=8))
# extract "legends only" from ggplot object
legend1 <- g_legend(legend1)
legend2 <- g_legend(legend2)
# setup legends grid
legend1_grid <- cowplot::plot_grid(legend1, align = "v", nrow = 2)
# add second legend to grid, specifying its location
legends <- legend1_grid +
ggplot2::annotation_custom(grob = legend2,
xmin = 0.5, xmax = 0.5, ymin = 0.55, ymax = 0.55)
# plot "plots" + "legends" (with legends in between plots)
cowplot::plot_grid(plot1, legends, plot2, ncol = 3,
rel_widths = c(0.45, 0.1, 0.45))
Tôi không nghĩ điều đó là có thể (nơi các truyền thuyết được đặt được điều khiển bởi chủ đề, và những phần xác định vị trí chú thích chỉ có một giá trị duy nhất). Tuy nhiên, tôi không chắc chắn rằng nó không thể làm cho câu trả lời này. –
Đoán xem đó có phải là một trong những chuyên gia đã có ngay bây giờ là – pssguy
@pssguy, nó có thể được thực hiện với một số không quan trọng. Để có quyền kiểm soát các truyền thuyết, bạn cần phải trích xuất các truyền thuyết riêng biệt, sau đó chúng có thể được sắp xếp trong một âm mưu ban đầu không chứa chú thích. –