[英]legend position and size in ggplot2
我在ggplot2中有一些有关图例位置和图例框大小问题的问题。 我尝试了很多事情,但到目前为止还没有运气!
我不想在图例框的位置上进行手动协调,并且每次都根据图来调整其大小。 我希望仅在需要时将其始终放置在具有调整大小的特定位置上!
我也想删除背景中的“白色”填充物,所以我使用了
legend.key = element_blank()
但似乎也行不通!
library(ggplot2)
ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
geom_point() +
labs(title = "Scatterplot", x = "Carat", y = "Price") + # add axis labels and plot title. print(gg)
facet_wrap(color ~ cut) +
theme(legend.position = c(0.9, 0.8),
legend.title = element_text(colour = "black", size = 6, face = "bold"),
legend.text = element_text(colour = "black", size = 6),
legend.key = element_blank(),
) +
guides(col = guide_legend(override.aes = list(size = 1, alpha = 1),
nrow = 1, title.position = "left"))
这创造了这个情节
在我看来,以下内容并不是一个完全令人满意的解决方案。
使用此处提出的解决方案,我们可以使用grid.text
添加文本:
library(ggplot2)
p <- ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
geom_point() +
labs(x = "Carat", y = "Price") + # add axis labels and plot title. print(gg)
facet_wrap(color ~ cut) +
theme(legend.position = c(0.9, 0.8),
legend.title = element_text(colour = "black", size = 6, face = "bold"),
legend.text = element_text(colour = "black", size = 6),
legend.key = element_blank()
)
makeTitle <- function(txt, xpos, ypos, size=1, color= "black") {
require(grid)
pushViewport(viewport())
grid.text(label = txt,
x = unit(xpos,"npc"),
y = unit(ypos, "npc"),
just = c("left", "bottom"),
gp = gpar(cex = size, col = color))
popViewport()
}
p + guides(col = guide_legend(override.aes = list(size = 1, alpha = 1),
nrow = 1, title.position = "left")) +
theme(legend.position = "top", legend.justification = "right")
makeTitle("Scatterplot", size=1.5, xpos=0.05, ypos=0.95)
希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.