我有一个2x2的网格图,每个面板有两个模型的数据。我需要为每个面板中的每个模型提供单独的回归线。我可以使用facet_grid来做到这一点吗?我有除了回归线之外的所有东西:
df <- data.frame(year=factor(c(2000,2001,2002,2000,2001,2002,2000,2001,2002,2000,2001,2002,
2000,2001,2002,2000,2001,2002,2000,2001,2002,2000,2001,2002)),
crop=c("Maize","Maize","Maize","Maize","Maize","Maize",
"Soybean","Soybean","Soybean","Soybean","Soybean","Soybean",
"Maize","Maize","Maize","Maize","Maize","Maize",
"Soybean","Soybean","Soybean","Soybean","Soybean","Soybean"),
treatment_code=c("T1","T1","T1","T2","T2","T2",
"T1","T1","T1","T2","T2","T2",
"T1","T1","T1","T2","T2","T2",
"T1","T1","T1","T2","T2","T2"),
value=c(9,10,8.5,11,12,7.8,
4,7,7.5,3,3,5,
12,11,9,13,9,10,
5,3,6,4,5,4),
model=c("A","A","A","A","A","A","A","A","A","A","A","A",
"B","B","B","B","B","B","B","B","B","B","B","B"))
gY_calib <- df %>%
ggplot(aes(x=year, y=value, color=model, show.legend=TRUE)) +
geom_point(show.legend=TRUE) +
xlab("Year") +
ylab(expression('Grain Yield (Mg ha ' ^-1*')')) +
ylim(0,13) +
facet_grid(crop~treatment_code)
gY_calib
1条答案
按热度按时间gpfsuwkq1#
您的
year
是factor。如果您将其设置为数字,它将正常工作。您可以使用以下代码