[英]Format indentation in ggplot2 legend
在此图的图例中,我希望“ Box”和“ ID”与左边缘的距离相同。 您能给我一些如何做的提示吗? 非常感谢!
# Load packages and data
library(ggplot2)
ID <- c(rep(1, 10), rep(2, 10))
Box <- c(rep("Red", 5), rep("Blue", 5), rep("Red", 5), rep("Blue", 5))
Time <- c(seq(1, 5), seq(1, 5), seq(1, 5), seq(1, 5))
Value <- runif(20, min=10, max=30)
dd <- data.frame(ID, Box, Time, Value)
# Plot
p1 <- ggplot(data = dd,
aes(x = Time, y = Value,
group = interaction(ID, Box),
shape = as.factor(ID),
colour=as.factor(ID),
linetype=Box)) +
geom_point(size = 3.5) +
geom_line() +
scale_colour_grey(start = 0, end = 0.5, name = "ID",
labels = c("1", "2")) +
scale_shape_discrete(name = "ID",
labels = c("1", "2")) +
scale_linetype_discrete(name = "Box",
labels = c("Red", "Blue")) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
aspect.ratio = 1,
text = element_text(size = 20),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.key = element_blank())
plot(p1)
我认为您只需要在theme()
添加legend.box.just = 0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.