创建ggplot堆栈栏和趋势线 [英] creating ggplot stack bar and trend line

查看:241
本文介绍了创建ggplot堆栈栏和趋势线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理名为 df 的数据框:

  Date变量价值
1/1/2012 teamA 10
1/1/2012 teamA 10


1/1/2012 teamB 10

1/1/2012 teamC 15

1/2/2012 teamA 25

1/2/2012 teamB 30

1/2 / 2012 teamC 20

第二个数据框叫做 total

 日期总计
1/1/2012 50
1/2/2012 70

我正在尝试创建一个ggplot堆栈var,并在同一图形上绘制一条趋势线:

  ggplot(df,aes(x = Date,y = Value,fill =(variable)))+ 
)geom_bar(stat =identity)+
theme_bw()+
opts(title =Team Performance)+
xlab(Date)+ ylab(Score)+
geom_smooth(data = Total,
aes(Date,Total,group = 1 ),
method =lm,size = 2,color =darkblue)

我得到这个错误:

  eval(expr,envir,enclos)中的错误:object'variable'not found 

当我这样做时:

  ggplot(df,aes(x = Date,y = Value,fill =(variable)))+ 
geom_bar(stat =identity)+ theme_bw()

有效

变量

code> object肯定是存在的,有什么想法,我在这里做错了什么?

解决方案

c $ c> ggplot()级联到每个图层。

这意味着 fill = variable 预计在 geom_smooth 我猜在 Total 中没有任何名为 variable 的变量。将 fill = variable 移动到 geom_bar 或者将其取消映射到 geom_smooth with fill = NULL


I am working with data frame called df:

    Date    variable   Value
    1/1/2012 teamA     10
    1/1/2012 teamA     10


    1/1/2012 teamB    10

    1/1/2012 teamC     15

    1/2/2012 teamA    25

    1/2/2012 teamB     30

    1/2/2012 teamC     20

And a second data frame called total

     Date      Total
    1/1/2012   50
    1/2/2012   70

I am trying to create a ggplot stack var and draw a trend line for the total on the same graph like this:

ggplot(df,aes(x=Date, y=Value, fill=(variable))) +
       geom_bar(stat="identity") + 
       theme_bw() + 
       opts(title = "Team Performance") + 
       xlab("Date") + ylab("Score") + 
       geom_smooth(data=Total,
                   aes(Date,Total,group=1), 
                   method="lm", size=2, color="darkblue")

I get this error:

Error in eval(expr, envir, enclos) : object 'variable' not found

When I do this by itself:

ggplot(df,aes(x=Date, y=Value, fill=(variable))) +  
geom_bar(stat="identity") + theme_bw() 

it works

variable object is definitely there, any ideas, what I am doing wrong here?

解决方案

Aesthetics mapped in ggplot() cascade down to each layer.

That means that fill = variable is expected in geom_smooth and I'm guessing that there's nothing called variable in Total. Either move fill = variable to geom_bar or unmap it in geom_smooth with fill = NULL.

这篇关于创建ggplot堆栈栏和趋势线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆