贝洛是我的代码

library(ggplot2)
# Create dummy data
set.seed(123)
df <- data.frame(
Date = rep(seq.Date(from = as.Date("2024-01-01"), by = "days", length.out = 10), 3),
Value = c(runif(10, 50, 100), runif(10, 60, 110), runif(10, 40, 90)),
Name = rep(c("A - Strategy - Net",
"B - S&P 500 Total Return Index",
"C - Bloomberg US Aggregate Bond Index"), each = 10)
)
# Create the plot
ggplot(df, aes(x = Date, y = Value, color = Name)) +
geom_line(size = 1) +
theme_minimal() +
guides(color = guide_legend(nrow = 2)) + # Set legend to 2 rows
theme(
legend.position = "bottom", # Move legend to the bottom
legend.spacing.x = unit(0.1, "cm"), # Reduce horizontal spacing
legend.margin = margin(-5, 0, 0, 0) # Reduce extra space around legend
) +
theme(legend.title = element_blank())
一个快速hack是在两行上包装B的标签:
set.seed(123)
df <- data.frame(
Date = rep(seq.Date(from = as.Date("2024-01-01"), by = "days", length.out = 10), 3),
Value = c(runif(10, 50, 100), runif(10, 60, 110), runif(10, 40, 90)),
Name = rep(c("A - Strategy - Net",
"B - S&P 500 \nTotal Return Index",
"C - Bloomberg US Aggregate Bond Index"), each = 10)
)