Simple x,y Plot

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple x,y Plot

sappy
Dear Community,

i have read in a table with two column from a *txt-File. What i get is a data-frame wit two column.
After that i allocate this 2 column to x and y. Plotting this, i obtain a picture that looks like a bar plot (something like a grid).
Is that because x and y is in factor-formatted? A conversion with "asnumeric" doesnt work. I get a vector with "NA".

Here is my code:

daten<-read.table("C:/Users/Robert/Desktop/regen.txt", header=FALSE, colClasses=c("real","real"))
x<-daten[,1]
y<-daten[,2]
plot(x,y)

Greetings
Reply | Threaded
Open this post in threaded view
|

Re: Simple x,y Plot

arun kirshna
Hi,

As there is not much information like str() or the data, it is difficult to comment.

But, try this (considering your x and y are factors):

x<-c("1","4","7","9","10","15")
 y<-c("3","5","8","6","5","14")
 dat1<-data.frame(x,y)
 str(dat1)
'data.frame':    6 obs. of  2 variables:
 $ x: Factor w/ 6 levels "1","10","15",..: 1 4 5 6 2 3
 $ y: Factor w/ 5 levels "14","3","5","6",..: 2 3 5 4 3 1

dat2<-dat1
 dat2$x<-as.numeric(as.character(dat2$x) )
 is.numeric(dat2$x)
#[1] TRUE
 dat2$y<-as.numeric(as.character(dat2$y))
 is.numeric(dat2$y)

#[1] TRUE
#Now try plot
 x1<-dat2[,1]
 y1<-dat2[,2]
 plot(x1,y1)
Hope this helps.

A.K.

----- Original Message -----
From: sappy <[hidden email]>
To: [hidden email]
Cc:
Sent: Sunday, July 29, 2012 11:18 AM
Subject: [R] Simple x,y Plot

Dear Community,

i have read in a table with two column from a *txt-File. What i get is a
data-frame wit two column.
After that i allocate this 2 column to x and y. Plotting this, i obtain a
picture that looks like a bar plot (something like a grid).
Is that because x and y is in factor-formatted? A conversion with
"asnumeric" doesnt work. I get a vector with "NA".

Here is my code:

daten<-read.table("C:/Users/Robert/Desktop/regen.txt", header=FALSE,
colClasses=c("real","real"))
x<-daten[,1]
y<-daten[,2]
plot(x,y)

Greetings



--
View this message in context: http://r.789695.n4.nabble.com/Simple-x-y-Plot-tp4638262.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Simple x,y Plot

mlell08
In reply to this post by sappy
On 29.07.2012 17:18, sappy wrote:

> Dear Community,
>
> i have read in a table with two column from a *txt-File. What i get is a
> data-frame wit two column.
> After that i allocate this 2 column to x and y. Plotting this, i obtain a
> picture that looks like a bar plot (something like a grid).
> Is that because x and y is in factor-formatted? A conversion with
> "asnumeric" doesnt work. I get a vector with "NA".
>
> Here is my code:
>
> daten<-read.table("C:/Users/Robert/Desktop/regen.txt", header=FALSE,
> colClasses=c("real","real"))

As you are german, I suppose you used the comma as decimal separator.
You need to specify that in read.table by dec="," or R won't be able to
parse your numbers.
 daten<-read.table("C:/Users/Robert/Desktop/regen.txt", header=FALSE,dec=",)


Viele Grüße,
Moritz

--
GnuPG Key: 0x7340821E

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Simple x,y Plot

Peter Ehlers
In reply to this post by sappy
On 2012-07-29 08:18, sappy wrote:
> Dear Community,
>
> i have read in a table with two column from a *txt-File. What i get is a
> data-frame wit two column.
> After that i allocate this 2 column to x and y. Plotting this, i obtain a
> picture that looks like a bar plot (something like a grid).
> Is that because x and y is in factor-formatted? A conversion with
> "asnumeric" doesnt work. I get a vector with "NA".

Why don't you run str(daten) on the data.frame to see what you
actually imported? It's a good idea to always run a str() after
reading data.

And I wouldn't use 'real' as a colClass (although it does have
an 'as' method); I don't see it mentioned on the help page for
read.table.

Peter Ehlers

>
> Here is my code:
>
> daten<-read.table("C:/Users/Robert/Desktop/regen.txt", header=FALSE,
> colClasses=c("real","real"))
> x<-daten[,1]
> y<-daten[,2]
> plot(x,y)
>
> Greetings
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Simple-x-y-Plot-tp4638262.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Simple x,y Plot

sappy
Thanks a lot. It Works.
Reply | Threaded
Open this post in threaded view
|

Re: Simple x,y Plot

Rui Barradas
In reply to this post by arun kirshna
Hello,

Inline

Em 29-07-2012 17:41, arun escreveu:

> Hi,
>
> As there is not much information like str() or the data, it is difficult to comment.
>
> But, try this (considering your x and y are factors):
>
> x<-c("1","4","7","9","10","15")
>   y<-c("3","5","8","6","5","14")
>   dat1<-data.frame(x,y)
>   str(dat1)
> 'data.frame':    6 obs. of  2 variables:
>   $ x: Factor w/ 6 levels "1","10","15",..: 1 4 5 6 2 3
>   $ y: Factor w/ 5 levels "14","3","5","6",..: 2 3 5 4 3 1
>
> dat2<-dat1
>   dat2$x<-as.numeric(as.character(dat2$x) )
>   is.numeric(dat2$x)
> #[1] TRUE
>   dat2$y<-as.numeric(as.character(dat2$y))
>   is.numeric(dat2$y)
>
> #[1] TRUE
> #Now try plot
>   x1<-dat2[,1]
>   y1<-dat2[,2]
>   plot(x1,y1)

# Or, using the data.frame directly,
plot(y ~ x, data=dat2)

Hope this helps,

Rui Barradas

> Hope this helps.
>
> A.K.
>
> ----- Original Message -----
> From: sappy <[hidden email]>
> To: [hidden email]
> Cc:
> Sent: Sunday, July 29, 2012 11:18 AM
> Subject: [R] Simple x,y Plot
>
> Dear Community,
>
> i have read in a table with two column from a *txt-File. What i get is a
> data-frame wit two column.
> After that i allocate this 2 column to x and y. Plotting this, i obtain a
> picture that looks like a bar plot (something like a grid).
> Is that because x and y is in factor-formatted? A conversion with
> "asnumeric" doesnt work. I get a vector with "NA".
>
> Here is my code:
>
> daten<-read.table("C:/Users/Robert/Desktop/regen.txt", header=FALSE,
> colClasses=c("real","real"))
> x<-daten[,1]
> y<-daten[,2]
> plot(x,y)
>
> Greetings
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Simple-x-y-Plot-tp4638262.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.