在R中使用facet_grid为每个面板绘制多条ablines

0md85ypi  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(149)

我有一个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

gpfsuwkq

gpfsuwkq1#

您的year是factor。如果您将其设置为数字,它将正常工作。您可以使用以下代码

df <- data.frame(year=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"))

ggplot(df, aes(x=year, y=value)) +
  geom_point(stat='identity', position='identity', aes(color=model)) +
  xlab("Year") +
  ylab(expression('Grain Yield (Mg ha ' ^-1*')')) +
  ylim(0,13) + 
  geom_smooth(method='lm', formula= y~x, aes(colour = model)) +
  facet_grid(crop~treatment_code) +
  scale_x_continuous(labels = scales::label_number(accuracy = 1), n.breaks = 3)

相关问题