【问题标题】:Using different types lynes using ggplot with facet_wrap使用带有 facet_wrap 的 ggplot 使用不同类型的线
【发布时间】:2021-05-22 07:30:32
【问题描述】:

假设我的数据具有以下结构

structure(list(MODEL = c("SHAR", "SHAR", "SHAR", "SHAR", "SHAR", 
"SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "MHAR", 
"MHAR", "MHAR", "MHAR", "MHAR", "MHAR", "MHAR", "MHAR", "MHAR", 
"MHAR", "MHAR", "MHAR"), name = c("NSW", "QLD", "SA", "VIC", 
"NSW", "QLD", "SA", "VIC", "NSW", "QLD", "SA", "VIC", "NSW",     
"QLD", "SA", "VIC", "NSW", "QLD", "SA", "VIC", "NSW", "QLD", 
"SA", "VIC"), value = c(-0.429298062117084, 0.369924963217068, 
-0.337914242584103, 0.352630004951941, -2.08652175501946, -0.788308699403464, 
-0.283024876840677, -0.673127168726321, -1.68696080065691, -0.584489188226214, 
0.648107971996921, -0.861309510260168, -1.63384189007935, 0.718255492983859, 
-1.17198565174371, 0.591123365147864, 0.208861829604905, -0.596711833086073, 
-1.33166531555532, -1.31370104749052, -1.04879351668995, -0.333940489158404,     
1.40014661442373, 0.270226863552986), Date = structure(c(2011, 
2011, 2011, 2011, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013, 
2011, 2011, 2011, 2011, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 
2013), class = "Date")), class = "data.frame", row.names = c(NA, 
-24L))

我正在使用facet_wrap 绘制两个模型的结果,并且我希望每个模型都有不同的线型。我尝试使用“scale_linetype_manual”,但我认为这不是facet_wrap 的合适命令。我使用的代码如下:

library(ggplot2)
ggplot(aes(x = as.Date(Date), y=value, colour = MODEL), data = EXAMPLE) +
  scale_color_manual(values = c("skyblue3", "firebrick3"), 
                     labels = c("MHAR-ReCov Model", 
                                "SHAR Model")) +
  
  scale_linetype_manual(values = c(2,5), 
                        labels = c("MHAR-ReCov Model", 
                                   "SHAR Model")) +
  geom_line() + facet_wrap(vars(name), ncol = 4, scales = "free")+
  theme_test() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1), 
        legend.position = "bottom",
        legend.title = element_blank())+
  labs(x = "", y="")

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: r ggplot2 facet-wrap


    【解决方案1】:

    aes 中包含linetype = MODEL

    library(ggplot2)
    
    ggplot(aes(x = as.Date(Date), y=value, colour = MODEL, 
               linetype = MODEL), data = EXAMPLE) +
      scale_color_manual(values = c("skyblue3", "firebrick3"), 
                         labels = c("MHAR-ReCov Model", 
                                    "SHAR Model")) +
      scale_linetype_manual(values = c(2,5), 
                            labels = c("MHAR-ReCov Model", 
                                       "SHAR Model")) +
      geom_line() + facet_wrap(vars(name), ncol = 4, scales = "free")+
      theme_test() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1), 
            legend.position = "bottom",
            legend.title = element_blank())+
      labs(x = "", y="")
    

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 2016-07-09
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多