【问题标题】:geom_area plot with areas and outlines ggplot带有区域和轮廓的 geom_area 图 ggplot
【发布时间】:2012-09-07 17:54:01
【问题描述】:

我正在尝试制作一个堆叠的 geom_area 图,但想用一条线勾勒出每个区域图(适用于第一个“红色”区域,但不适用于蓝色区域)。这是我最好的尝试,但我不知道如何使线型也堆叠起来。想法?

df= data.frame(Time=as.numeric(strsplit('1939 1949 1959 1969 1979 1989 1999 2009 2019 2029 2039 2049 1939 1949 1959 1969 1979 1989 1999 2009 2019 2029 2039 2049', split=' ')[[1]] ),
               Acres=as.numeric(strsplit('139504.2 233529.0 392105.3 502983.9 685159.9 835594.7 882945.1 1212671.4 1475211.9 1717971.7 1862505.7 1934308.0 308261.4 502460.8 834303.1 1115150.7 1430797.8 1712085.8 1973366.1 1694907.7 1480506.0 1280047.6 1164200.5 1118045.3', split=' ')[[1]] ),
               WUClass= strsplit('DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban Urban Urban Urban Urban Urban Urban Urban Urban Urban Urban Urban Urban', split=' ')[[1]]   
               )

a=ggplot(df,aes(x = Time,y = Acres,fill=WUClass))+ geom_area( position = 'stack'  )
plot(a)
a+ geom_line(aes(position = 'stack'))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    要为区域添加轮廓,只需更改colour

    ggplot(df,aes(x = Time,y = Acres,fill=WUClass)) +
      geom_area( position = 'stack') +
      geom_area( position = 'stack', colour="black", show_guide=FALSE)
    

    但要画线,请这样做:

    ggplot(df,aes(x = Time, y = Acres, fill=WUClass, group=WUClass)) +
      geom_area() + geom_line(aes(ymax=Acres), position="stack")
    

    【讨论】:

      【解决方案2】:

      为了说明差异,我将线宽设置得非常大。

      ggplot(df,aes(x = Time, y = Acres, fill = WUClass)) + 
      geom_area( position = 'stack', linetype = 1, size =2 ,colour="black" , 
      show_guide=FALSE) +
      geom_line(aes(position = 'stack'))
      

      【讨论】:

      • 感谢 Maiasaura!虽然我喜欢其他人回答多一点,因为我不必担心传说中的台词
      【解决方案3】:

      错误只是您将 position="stack" 放入 aes() 中。改为

      a=ggplot(df,aes(x = Time,y = Acres, fill=WUClass))+
                         geom_area( position = 'stack'  )
      a +geom_line( position = 'stack'  )
      

      ...一切都很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-08
        • 1970-01-01
        • 2021-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多