geom_text error

947 views
Skip to first unread message

Babak

unread,
Mar 5, 2012, 6:54:33 PM3/5/12
to ggp...@googlegroups.com
Howdy,

I get an error after attempt to add a text to my graph. 
Bellow is the description of my steps to draw a Matlab's like steam graph:
All the steps works fine except the geom_text command.

> q2 = ggplot(df, aes(x=length, y=count, ymin=c0, ymax = count))

> q2 + geom_point(aes(x=length, y=count))

> last_plot() + geom_linerange(aes(x=length, y=count, ymin=c0, ymax = count))

> last_plot() + geom_vline(xintercept = c(100, 200), color = "red")

> last_plot() + geom_text(aes(x2,y2,label = texthere), data.frame(x2=100, y2=0, texthere="Text Here"))


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

What I don't get is why geom_text should look at the df x and y, BTW here is the df

> head(df)
  length count
1    200     5
2    400    41
3    600    68
4    800   124
5   1000   204
6   1200   296

Thanks for your help'
Robert

Winston Chang

unread,
Mar 5, 2012, 7:03:57 PM3/5/12
to Babak, ggp...@googlegroups.com
You can give geom_text the argument 'inherit.aes=FALSE', so that it doesn't inherit the default x and y mappings.

But a better solution is probably to use annotate, something like this:
last_plot() + annotate(geom="text", x=100, y=0, label="Text Here")


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

iqbalh...@gmail.com

unread,
Mar 5, 2012, 7:01:57 PM3/5/12
to ggp...@googlegroups.com
Hi, I'm new here I would like to know, what is data.frame is always mention about? Is data.frame like when you save a data from ascii,etc save to be package in R? Anyone can help me to know?because I'm searching a data that could save like a package or could save in R so that could call them everytime beside using read.csv and so on. thank you.
Sent from my BlackBerry®
powered by Sinyal Kuat INDOSAT

From: Babak <bfar...@gmail.com>
Date: Mon, 5 Mar 2012 18:54:33 -0500
Subject: geom_text error
--

muhammad iqbal habibie

unread,
Mar 5, 2012, 9:29:08 PM3/5/12
to ggp...@googlegroups.com
Thank you michael, Yes i have been using read.csv then i call some data from read csv. Is it the same like data.frame?if yes, i would like input my data from csv,ascii to be a class like data frame. or i must used load() and save(0 function that you are mention.

Example :

sumcr<- read.csv(file.choose())

attach(sumcr)
plot(CumLen, WidthWS)
lines(lowess(CumLen, WidthWS), col="blue", lwd=2)
cor(CumLen, WidthWS)
detach(sumcr)

if i'm using data frame like this :

sumcr<- data.frame (cumLen, WidthWS)


Is that possible and the function of this is it the same?


Best regards,

Iqbal Habibie

On Tue, Mar 6, 2012 at 9:13 AM, R. Michael Weylandt <michael....@gmail.com> wrote:
Data.frames are a fundamental data structure in R. The official
introductory material, accessible at
http://cran.r-project.org/doc/manuals/R-intro.pdf, describes them in
6.3ff and you can also read about them by typing ?data.frame at the
console. If you've been using read.csv and friends, you've probably
already been using data.frames perhaps without knowing so.

It sounds like you are looking for the load() and save() functions
which can save objects in R binary format for easy reuse.

Michael

On Mon, Mar 5, 2012 at 7:01 PM,  <iqbalh...@gmail.com> wrote:
> Hi, I'm new here I would like to know, what is data.frame is always mention
> about? Is data.frame like when you save a data from ascii,etc save to be
> package in R? Anyone can help me to know?because I'm searching a data that
> could save like a package or could save in R so that could call them
> everytime beside using read.csv and so on. thank you.
> Sent from my BlackBerry®
> powered by Sinyal Kuat INDOSAT
> ________________________________



--
Muhammad Iqbal Habibie,S.Kom,MCP,MT
Center of Technology for Natural Resources Inventory
Agency for the Assessment and Application of Technology
Building II, 19th floor
Jl. MH. Thamrin No.8 Jakarta 10340 - Indonesia
Tel: +62-21-3169712, Fax: +62-21-3169720
HP : +628567459747, +6281322856739
Email: muhamma...@bppt.go.id , iqbalh...@gmail.com

sumcr.csv

muhammad iqbal habibie

unread,
Mar 5, 2012, 10:06:51 PM3/5/12
to ggp...@googlegroups.com
I thought that read.csv can be replaced by data.frame function. because the format it is almost the same. Is it possible read.csv can be replace by data.frame function?  because i have tried like in example below it is not possible to call. i was wondering  is it possible data from csv to be data.frame? so that command in 3 could call and be found?

1. >sumcr<- read.csv(file.choose())
2. > attach(sumcr)
3. > sumcr<- data.frame (cumLen, WidthWS)
Error in data.frame(cumLen, WidthWS) : object 'cumLen' not found
4. > plot(CumLen, WidthWS)


On Tue, Mar 6, 2012 at 9:52 AM, R. Michael Weylandt <michael....@gmail.com> wrote:
The output of read.csv is *already* a data.frame so you don't need to
do anything further. You can check this by using class(sumcr).

Any object, once in R, can be saved using save() and load() -- type
?save and ?load to see how they work.

Michael
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

muhammad iqbal habibie

unread,
Mar 5, 2012, 10:28:23 PM3/5/12
to ggp...@googlegroups.com
for this function :

> qplot(CumLen, WidthWS, data = sumcr)
Error: could not find function "qplot"

I do not have package for gplot. please give me the link...there is some package R that i have been using it is in
http://cran.r-project.org/web/packages/available_packages_by_name.html but i can't find qplot. thank you.

Iqbal Habibie


On Tue, Mar 6, 2012 at 10:14 AM, R. Michael Weylandt <michael....@gmail.com> wrote:
This question is really more appropriate for the general R-help list
than the ggplot2 specialty list so I'd ask we continue this there if
the following doesn't clarify things.

Please read what I already said carefully and then consider this: your
command 3 is entirely unnecessary. read.csv has already given you
sumcr *as a data frame* so you don't need to remake the same data
frame or convert anything to a data.frame. The error you are seeing
has nothing to do with csv files or load() or anything complicated:
the problem is that "cumLen" and "CumLen" are not the same to R (note
the capitalization differences).

It's also not generally good practice to use attach() in R as it leads
to lots of problems down the line: the following two lines work just
as well.

sumcr <- read.csv(file.choose())
plot(WidthWS ~ CumLen, data = sumcr)

## or if you want a ggplot2 solution ;-)

qplot(CumLen, WidthWS, data = sumcr)

Michael

On Mon, Mar 5, 2012 at 10:06 PM, muhammad iqbal habibie

Dennis Murphy

unread,
Mar 6, 2012, 12:26:35 AM3/6/12
to Babak, ggp...@googlegroups.com
Hi:

There are several potential sources of confusion here, but the main
one is that you're evidently unaware that any aesthetics stated in the
ggplot() call are expected to be present in *every* subsequent layer.
This would be cleaner for your problem:

q2 <- ggplot(df)
q2 + geom_point(aes(x = length, y = count)) +
geom_linerange(aes(x = length, y = count, ymin = c0, ymax = count)) +
geom_vline(xintercept = c(100, 200), color = "red") +
annotate(geom = 'text', x = 100, y = 0, label = 'Text here')

The 'count not found' error in your code comes when you use a new data
frame in an annotation layer that contains no reference to ymin or
ymax. In my code, the aesthetics are defined to be local to the geom
call so no mismatch problems should arise.

As for your other question regarding data frames, I'd suggest that you
take the time to read An Introduction to R, which comes with the
software.

Thirdly, most of us don't use Matlab so we may not know what a 'steam
graph' is in Matlab-speak (I know I don't). It's better to supply an
example picture to illustrate what you want - it's easier to help when
we know what help you need.

Dennis

R. Michael Weylandt

unread,
Mar 5, 2012, 9:13:18 PM3/5/12
to iqbalh...@gmail.com, ggp...@googlegroups.com
Data.frames are a fundamental data structure in R. The official
introductory material, accessible at
http://cran.r-project.org/doc/manuals/R-intro.pdf, describes them in
6.3ff and you can also read about them by typing ?data.frame at the
console. If you've been using read.csv and friends, you've probably
already been using data.frames perhaps without knowing so.

It sounds like you are looking for the load() and save() functions
which can save objects in R binary format for easy reuse.

Michael

On Mon, Mar 5, 2012 at 7:01 PM, <iqbalh...@gmail.com> wrote:

R. Michael Weylandt

unread,
Mar 5, 2012, 9:52:33 PM3/5/12
to muhammad iqbal habibie, ggp...@googlegroups.com
The output of read.csv is *already* a data.frame so you don't need to
do anything further. You can check this by using class(sumcr).

Any object, once in R, can be saved using save() and load() -- type
?save and ?load to see how they work.

Michael

R. Michael Weylandt

unread,
Mar 5, 2012, 10:14:53 PM3/5/12
to muhammad iqbal habibie, ggp...@googlegroups.com
This question is really more appropriate for the general R-help list
than the ggplot2 specialty list so I'd ask we continue this there if
the following doesn't clarify things.

Please read what I already said carefully and then consider this: your
command 3 is entirely unnecessary. read.csv has already given you
sumcr *as a data frame* so you don't need to remake the same data
frame or convert anything to a data.frame. The error you are seeing
has nothing to do with csv files or load() or anything complicated:
the problem is that "cumLen" and "CumLen" are not the same to R (note
the capitalization differences).

It's also not generally good practice to use attach() in R as it leads
to lots of problems down the line: the following two lines work just
as well.

sumcr <- read.csv(file.choose())
plot(WidthWS ~ CumLen, data = sumcr)

## or if you want a ggplot2 solution ;-)

qplot(CumLen, WidthWS, data = sumcr)

Michael

On Mon, Mar 5, 2012 at 10:06 PM, muhammad iqbal habibie

R. Michael Weylandt

unread,
Mar 5, 2012, 10:34:43 PM3/5/12
to ggp...@googlegroups.com
I'll give you a hint: look at the name of the mailing list
(ggp...@googlegroups.com). If you need a second hint, look
immediately after the "@" symbol. ;-)

Michael

On Mon, Mar 5, 2012 at 10:28 PM, muhammad iqbal habibie
<iqbalh...@gmail.com> wrote:
> for this function :
>
>> qplot(CumLen, WidthWS, data = sumcr)
> Error: could not find function "qplot"
>

> I do not have package for qplot. please give me the link...there is some

Reply all
Reply to author
Forward
0 new messages