geom_line() errors with new data

10 views
Skip to first unread message

Mike Lawrence

unread,
Mar 8, 2009, 11:14:56 AM3/8/09
to ggp...@googlegroups.com
#here is some data
a=as.data.frame(cbind(x=1:10,y=c(1:5,5:1)))
b=5.5
c=2.5

#here's how to plot it using plot() and lines()
# (yes, this looks silly, but it's a simplification of
# other data I have)
plot(a$x,a$y)
lines(
x=c(min(a$x),b,max(a$x))
,y=c(min(a$y),c,min(a$y))
)

#here's an attempt to use qplot() & geom_line() to do the same
p=qplot(data=a,x=x,y=y)
p=p+geom_line(
aes(
x=c(min(a$x),b,max(a$x))
,y=c(min(a$y),c,min(a$y))
)
)
print(p)
# Error in data.frame(..., check.names = FALSE) :
# arguments imply differing number of rows: 3, 10

#try with ggplot()
p=ggplot(data=a,aes(x=x,y=y))
p=p+geom_point()
p=p+geom_line(
aes(
x=c(min(a$x),b,max(a$x))
,y=c(min(a$y),c,min(a$y))
)
)
print(p)
# Error in data.frame(..., check.names = FALSE) :
# arguments imply differing number of rows: 3, 10


--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tinyurl.com/mikes-public-calendar

~ Certainty is folly... I think. ~

baptiste auguie

unread,
Mar 8, 2009, 11:32:48 AM3/8/09
to Mike Lawrence, ggp...@googlegroups.com
Hi,

> p=qplot(data=a,x=x,y=y)
> p=p+geom_line(
+ data=data.frame(
+ x=c(min(a$x),b,max(a$x))
+ ,y=c(min(a$y),c,min(a$y))
+ )
+ )
> p

works for me.

My guess is that you were misusing the aes() function, the description
of which reads,

"Aesthetic mappings describe how variables in the data are mapped to
visual properties (aesthetics) of geoms."

In other words, you passed some data instead of a description of how
the data should relate to the plot.

Hope this helps,

baptiste
--

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
______________________________

Mike Lawrence

unread,
Mar 8, 2009, 1:43:56 PM3/8/09
to baptiste auguie, ggp...@googlegroups.com
Thanks! On a related note, it seems that there's a strange naming error...

#use names "x" and "y"
a=as.data.frame(cbind(x=1:10,y=c(1:5,5:1)))
p=qplot(data=a,x=x,y=y)
print(p) #works ok
p=p+geom_line(
data=data.frame(
x=c(1,5.5,10)
,y=c(1,2.5,1)
)
)
print(p) #works ok

#use names "iv" & "dv"
a=as.data.frame(cbind(iv=1:10,dv=c(1:5,5:1)))
p=qplot(data=a,x=iv,y=dv)
print(p) #works ok
p=p+geom_line(
data=data.frame(
x=c(1,5.5,10)
,y=c(1,2.5,10)
)
)
print(p) #yields an error

#try ggplot()
p=ggplot(data=a,aes(x=iv,y=dv))+geom_point()
print(p) #works ok
p=p+geom_line(
data=data.frame(
x=c(1,5.5,10)
,y=c(1,2.5,10)
)
)
print(p) #yields an error

baptiste auguie

unread,
Mar 8, 2009, 2:44:38 PM3/8/09
to Mike Lawrence, ggp...@googlegroups.com
I think you're still missing the aim of the aes() function. In your
second example you create a mapping between x and iv and y and dv. Any
subsequent layer will use this correspondence by default. So the
geom_line() layer tries to find some values called iv and dv in the
data and there are none of course. You can however specify a new
mapping explicitly for this layer,

a=as.data.frame(cbind(iv=1:10,dv=c(1:5,5:1)))
p=qplot(data=a,x=iv,y=dv)
print(p) #works ok
p=p+geom_line(
data=data.frame(
x=c(1,5.5,10)

,y=c(1,2.5,1)
), mapping=aes(x=x, y=y)
)
print(p) # works

HTH,

baptiste

--

hadley wickham

unread,
Mar 9, 2009, 9:12:36 AM3/9/09
to baptiste auguie, Mike Lawrence, ggp...@googlegroups.com
On Sun, Mar 8, 2009 at 9:32 AM, baptiste auguie <bapt...@googlemail.com> wrote:
>
> Hi,
>
>> p=qplot(data=a,x=x,y=y)
>> p=p+geom_line(
> +        data=data.frame(
> +                x=c(min(a$x),b,max(a$x))
> +                ,y=c(min(a$y),c,min(a$y))
> +        )
> + )
>> p
>
> works for me.
>
> My guess is that you were misusing the aes() function, the description
> of which reads,
>
> "Aesthetic mappings describe how variables in the data are mapped to
> visual properties (aesthetics) of geoms."
>
> In other words, you passed some data  instead of a description of how
> the data should relate to the plot.

That's a good explanation.

Preferably the mapping shouldn't depend on anything outside of the
plot/layer data. If you save this plot and reload it in another
session, it won't work because it won't have access to a, b, or c. If
I could figure out how to make this an error, I would.

Hadley


--
http://had.co.nz/

Mike Lawrence

unread,
Mar 9, 2009, 9:23:51 AM3/9/09
to baptiste auguie, ggp...@googlegroups.com
Ah! That makes much more sense, thank you for your help/patience!

Mike

Reply all
Reply to author
Forward
0 new messages