代码之家  ›  专栏  ›  技术社区  ›  ePoQ

时间序列图(ggplot)上的线型

  •  2
  • ePoQ  · 技术社区  · 7 年前

    我想在timeseries绘图上应用不同的线型模型。下面是一个使用类似于我的数据/代码的结构的可复制示例。比如说,我想要女人用实线,男人用虚线。

    source("http://www.openintro.org/stat/data/arbuthnot.R")
    library(ggplot2)
    library(reshape2)
    
    names(arbuthnot) <- c("Year", "Men", "Women")
    
    arbuthnot.melt <- melt(arbuthnot, id.vars = 'Year', variable.name = 'Sex', 
                           value.name = 'Rate')
    ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex))+
      geom_line() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) + 
      scale_linetype_manual(values = c('Women' = 'solid', 'Men' = 'dotted'))
    

    我被困在这里很长时间了,我尝试了其他语法,比如 c(0,4) ,则, scale_linetype_manual(values = c('Women' = 1, 'Men' = 4)) ,则, scale_shape_manual 我真的不明白为什么 scale_linetype_manual 不在这里工作。任何帮助都将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  1
  •   coffeinjunky    7 年前

    尝试

    ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex, linetype = Sex))+
      geom_line() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) + 
      scale_linetype_manual(values = c('Women' = 'solid', 'Men' = 'dotted'))
    

    enter image description here

    线型是美学贴图的一部分,因此需要包含在 aes -元素。

    推荐文章