plot factors with dots in R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

plot factors with dots in R

Luigi
Hello,
I have a dataframe as follows:
```
x = c("0 pmol", "10 pmol", "100 pmol", "1000 pmol")
y = c(0.9306, 1.8906, 2.2396, 2.7917)
df = data.frame(x, y)

> str(df)
'data.frame': 4 obs. of  2 variables:
 $ x: chr  "0 pmol" "10 pmol" "100 pmol" "1000 pmol"
 $ y: num  0.931 1.891 2.24 2.792
```
I would like to visualize the data with the classic dots (pch=16) but:
```
> plot(df$y ~ df$x)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
```
which is right because x is not numeric, so I took the factor:
```
plot(df$y ~ factor(df$x)) # gives bars instead of dots
plot(df$y ~ factor(df$x), pch = 16) # this also
```
I tried to convert directly the dataframe:
```
df$x = lapply(df$x, factor)
> str(df)
'data.frame': 4 obs. of  2 variables:
 $ x:List of 4
  ..$ : Factor w/ 1 level "0 pmol": 1
  ..$ : Factor w/ 1 level "10 pmol": 1
  ..$ : Factor w/ 1 level "100 pmol": 1
  ..$ : Factor w/ 1 level "1000 pmol": 1
 $ y: num  0.931 1.891 2.24 2.792

> plot(r$y ~ r$x, pch = 16)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
```
If I try to pass the number of levels:
```
plot(df$y ~ factor(df$x, 1:4), pch = 16) # this draw a boxplot with
all data on level 1

> df$x = lapply(df$x, factor(1:4))
Error in match.fun(FUN) :
  'factor(1:4)' is not a function, character or symbol
```

Since the transformation has given only one level (1), my questions are:
How do I tell R to use a dot instead of a line?
What is the correct way of setting factors?


--
Best regards,
Luigi

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: plot factors with dots in R

PIKAL Petr
Hi.
It is probably somewhere in docs, but factors are actually numerics vith
labels.

So with your original data frame

df$x <- factor(df$x)
plot(as.numeric(df$x), df$y)

gives you points. You need to set labels to x axis though.

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Luigi Marongiu
> Sent: Thursday, August 27, 2020 2:16 PM
> To: r-help <[hidden email]>
> Subject: [R] plot factors with dots in R
>
> Hello,
> I have a dataframe as follows:
> ```
> x = c("0 pmol", "10 pmol", "100 pmol", "1000 pmol")
> y = c(0.9306, 1.8906, 2.2396, 2.7917)
> df = data.frame(x, y)
>
> > str(df)
> 'data.frame': 4 obs. of  2 variables:
>  $ x: chr  "0 pmol" "10 pmol" "100 pmol" "1000 pmol"
>  $ y: num  0.931 1.891 2.24 2.792
> ```
> I would like to visualize the data with the classic dots (pch=16) but:
> ```
> > plot(df$y ~ df$x)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> which is right because x is not numeric, so I took the factor:
> ```
> plot(df$y ~ factor(df$x)) # gives bars instead of dots
> plot(df$y ~ factor(df$x), pch = 16) # this also
> ```
> I tried to convert directly the dataframe:
> ```
> df$x = lapply(df$x, factor)
> > str(df)
> 'data.frame': 4 obs. of  2 variables:
>  $ x:List of 4
>   ..$ : Factor w/ 1 level "0 pmol": 1
>   ..$ : Factor w/ 1 level "10 pmol": 1
>   ..$ : Factor w/ 1 level "100 pmol": 1
>   ..$ : Factor w/ 1 level "1000 pmol": 1
>  $ y: num  0.931 1.891 2.24 2.792
>
> > plot(r$y ~ r$x, pch = 16)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> If I try to pass the number of levels:
> ```
> plot(df$y ~ factor(df$x, 1:4), pch = 16) # this draw a boxplot with
> all data on level 1
>
> > df$x = lapply(df$x, factor(1:4))
> Error in match.fun(FUN) :
>   'factor(1:4)' is not a function, character or symbol
> ```
>
> Since the transformation has given only one level (1), my questions are:
> How do I tell R to use a dot instead of a line?
> What is the correct way of setting factors?
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: plot factors with dots in R

Luigi
Thank you, better than before...

On Thu, Aug 27, 2020 at 2:37 PM PIKAL Petr <[hidden email]> wrote:

>
> Hi.
> It is probably somewhere in docs, but factors are actually numerics vith
> labels.
>
> So with your original data frame
>
> df$x <- factor(df$x)
> plot(as.numeric(df$x), df$y)
>
> gives you points. You need to set labels to x axis though.
>
> Cheers
> Petr
>
> > -----Original Message-----
> > From: R-help <[hidden email]> On Behalf Of Luigi Marongiu
> > Sent: Thursday, August 27, 2020 2:16 PM
> > To: r-help <[hidden email]>
> > Subject: [R] plot factors with dots in R
> >
> > Hello,
> > I have a dataframe as follows:
> > ```
> > x = c("0 pmol", "10 pmol", "100 pmol", "1000 pmol")
> > y = c(0.9306, 1.8906, 2.2396, 2.7917)
> > df = data.frame(x, y)
> >
> > > str(df)
> > 'data.frame': 4 obs. of  2 variables:
> >  $ x: chr  "0 pmol" "10 pmol" "100 pmol" "1000 pmol"
> >  $ y: num  0.931 1.891 2.24 2.792
> > ```
> > I would like to visualize the data with the classic dots (pch=16) but:
> > ```
> > > plot(df$y ~ df$x)
> > Error in plot.window(...) : need finite 'xlim' values
> > In addition: Warning messages:
> > 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> > 2: In min(x) : no non-missing arguments to min; returning Inf
> > 3: In max(x) : no non-missing arguments to max; returning -Inf
> > ```
> > which is right because x is not numeric, so I took the factor:
> > ```
> > plot(df$y ~ factor(df$x)) # gives bars instead of dots
> > plot(df$y ~ factor(df$x), pch = 16) # this also
> > ```
> > I tried to convert directly the dataframe:
> > ```
> > df$x = lapply(df$x, factor)
> > > str(df)
> > 'data.frame': 4 obs. of  2 variables:
> >  $ x:List of 4
> >   ..$ : Factor w/ 1 level "0 pmol": 1
> >   ..$ : Factor w/ 1 level "10 pmol": 1
> >   ..$ : Factor w/ 1 level "100 pmol": 1
> >   ..$ : Factor w/ 1 level "1000 pmol": 1
> >  $ y: num  0.931 1.891 2.24 2.792
> >
> > > plot(r$y ~ r$x, pch = 16)
> > Error in plot.window(...) : need finite 'xlim' values
> > In addition: Warning messages:
> > 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> > 2: In min(x) : no non-missing arguments to min; returning Inf
> > 3: In max(x) : no non-missing arguments to max; returning -Inf
> > ```
> > If I try to pass the number of levels:
> > ```
> > plot(df$y ~ factor(df$x, 1:4), pch = 16) # this draw a boxplot with
> > all data on level 1
> >
> > > df$x = lapply(df$x, factor(1:4))
> > Error in match.fun(FUN) :
> >   'factor(1:4)' is not a function, character or symbol
> > ```
> >
> > Since the transformation has given only one level (1), my questions are:
> > How do I tell R to use a dot instead of a line?
> > What is the correct way of setting factors?
> >
> >
> > --
> > Best regards,
> > Luigi
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.



--
Best regards,
Luigi

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: plot factors with dots in R

Rui Barradas
In reply to this post by Luigi
Hello,

The plots that you say give bars (or my equivalent version below) don't
give bars, what they give are boxplots with just one value and the
median Q1 and Q3 are all equal.

plot(y ~ factor(x), df, pch = 16)  # boxplot


Is the following what you are looking for?


plot(y ~ as.integer(factor(x)), df, pch = 16, xlab = "x", xaxt = "n")
axis(1, at = as.integer(factor(df$x)), labels = df$x)


Hope this helps,

Rui Barradas


Às 13:16 de 27/08/20, Luigi Marongiu escreveu:

> Hello,
> I have a dataframe as follows:
> ```
> x = c("0 pmol", "10 pmol", "100 pmol", "1000 pmol")
> y = c(0.9306, 1.8906, 2.2396, 2.7917)
> df = data.frame(x, y)
>
>> str(df)
> 'data.frame': 4 obs. of  2 variables:
>   $ x: chr  "0 pmol" "10 pmol" "100 pmol" "1000 pmol"
>   $ y: num  0.931 1.891 2.24 2.792
> ```
> I would like to visualize the data with the classic dots (pch=16) but:
> ```
>> plot(df$y ~ df$x)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> which is right because x is not numeric, so I took the factor:
> ```
> plot(df$y ~ factor(df$x)) # gives bars instead of dots
> plot(df$y ~ factor(df$x), pch = 16) # this also
> ```
> I tried to convert directly the dataframe:
> ```
> df$x = lapply(df$x, factor)
>> str(df)
> 'data.frame': 4 obs. of  2 variables:
>   $ x:List of 4
>    ..$ : Factor w/ 1 level "0 pmol": 1
>    ..$ : Factor w/ 1 level "10 pmol": 1
>    ..$ : Factor w/ 1 level "100 pmol": 1
>    ..$ : Factor w/ 1 level "1000 pmol": 1
>   $ y: num  0.931 1.891 2.24 2.792
>
>> plot(r$y ~ r$x, pch = 16)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> If I try to pass the number of levels:
> ```
> plot(df$y ~ factor(df$x, 1:4), pch = 16) # this draw a boxplot with
> all data on level 1
>
>> df$x = lapply(df$x, factor(1:4))
> Error in match.fun(FUN) :
>    'factor(1:4)' is not a function, character or symbol
> ```
>
> Since the transformation has given only one level (1), my questions are:
> How do I tell R to use a dot instead of a line?
> What is the correct way of setting factors?
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: plot factors with dots in R

Jim Lemon-4
In reply to this post by Luigi
Hi Luigi,
Maybe just:

plot(as.numeric(factor(x,levels=x)),y,xaxt="n",
 main="Concentration by effect",
 xlab="Concentration",ylab="Effect")
axis(1,at=1:4,labels=x)

Jim

On Thu, Aug 27, 2020 at 10:16 PM Luigi Marongiu
<[hidden email]> wrote:

>
> Hello,
> I have a dataframe as follows:
> ```
> x = c("0 pmol", "10 pmol", "100 pmol", "1000 pmol")
> y = c(0.9306, 1.8906, 2.2396, 2.7917)
> df = data.frame(x, y)
>
> > str(df)
> 'data.frame': 4 obs. of  2 variables:
>  $ x: chr  "0 pmol" "10 pmol" "100 pmol" "1000 pmol"
>  $ y: num  0.931 1.891 2.24 2.792
> ```
> I would like to visualize the data with the classic dots (pch=16) but:
> ```
> > plot(df$y ~ df$x)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> which is right because x is not numeric, so I took the factor:
> ```
> plot(df$y ~ factor(df$x)) # gives bars instead of dots
> plot(df$y ~ factor(df$x), pch = 16) # this also
> ```
> I tried to convert directly the dataframe:
> ```
> df$x = lapply(df$x, factor)
> > str(df)
> 'data.frame': 4 obs. of  2 variables:
>  $ x:List of 4
>   ..$ : Factor w/ 1 level "0 pmol": 1
>   ..$ : Factor w/ 1 level "10 pmol": 1
>   ..$ : Factor w/ 1 level "100 pmol": 1
>   ..$ : Factor w/ 1 level "1000 pmol": 1
>  $ y: num  0.931 1.891 2.24 2.792
>
> > plot(r$y ~ r$x, pch = 16)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> If I try to pass the number of levels:
> ```
> plot(df$y ~ factor(df$x, 1:4), pch = 16) # this draw a boxplot with
> all data on level 1
>
> > df$x = lapply(df$x, factor(1:4))
> Error in match.fun(FUN) :
>   'factor(1:4)' is not a function, character or symbol
> ```
>
> Since the transformation has given only one level (1), my questions are:
> How do I tell R to use a dot instead of a line?
> What is the correct way of setting factors?
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: plot factors with dots in R

Deepayan Sarkar
In reply to this post by Luigi
On Thu, Aug 27, 2020 at 5:46 PM Luigi Marongiu <[hidden email]> wrote:

>
> Hello,
> I have a dataframe as follows:
> ```
> x = c("0 pmol", "10 pmol", "100 pmol", "1000 pmol")
> y = c(0.9306, 1.8906, 2.2396, 2.7917)
> df = data.frame(x, y)
>
> > str(df)
> 'data.frame': 4 obs. of  2 variables:
>  $ x: chr  "0 pmol" "10 pmol" "100 pmol" "1000 pmol"
>  $ y: num  0.931 1.891 2.24 2.792
> ```
> I would like to visualize the data with the classic dots (pch=16) but:

Perhaps this is a good starting point:

with(df, dotchart(y, labels = x, pch = 16))

-Deepayan

> ```
> > plot(df$y ~ df$x)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> which is right because x is not numeric, so I took the factor:
> ```
> plot(df$y ~ factor(df$x)) # gives bars instead of dots
> plot(df$y ~ factor(df$x), pch = 16) # this also
> ```
> I tried to convert directly the dataframe:
> ```
> df$x = lapply(df$x, factor)
> > str(df)
> 'data.frame': 4 obs. of  2 variables:
>  $ x:List of 4
>   ..$ : Factor w/ 1 level "0 pmol": 1
>   ..$ : Factor w/ 1 level "10 pmol": 1
>   ..$ : Factor w/ 1 level "100 pmol": 1
>   ..$ : Factor w/ 1 level "1000 pmol": 1
>  $ y: num  0.931 1.891 2.24 2.792
>
> > plot(r$y ~ r$x, pch = 16)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> If I try to pass the number of levels:
> ```
> plot(df$y ~ factor(df$x, 1:4), pch = 16) # this draw a boxplot with
> all data on level 1
>
> > df$x = lapply(df$x, factor(1:4))
> Error in match.fun(FUN) :
>   'factor(1:4)' is not a function, character or symbol
> ```
>
> Since the transformation has given only one level (1), my questions are:
> How do I tell R to use a dot instead of a line?
> What is the correct way of setting factors?
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: plot factors with dots in R

Luigi
Thank you, all solutions work!

On Fri, Aug 28, 2020 at 9:02 AM Deepayan Sarkar
<[hidden email]> wrote:

>
> On Thu, Aug 27, 2020 at 5:46 PM Luigi Marongiu <[hidden email]> wrote:
> >
> > Hello,
> > I have a dataframe as follows:
> > ```
> > x = c("0 pmol", "10 pmol", "100 pmol", "1000 pmol")
> > y = c(0.9306, 1.8906, 2.2396, 2.7917)
> > df = data.frame(x, y)
> >
> > > str(df)
> > 'data.frame': 4 obs. of  2 variables:
> >  $ x: chr  "0 pmol" "10 pmol" "100 pmol" "1000 pmol"
> >  $ y: num  0.931 1.891 2.24 2.792
> > ```
> > I would like to visualize the data with the classic dots (pch=16) but:
>
> Perhaps this is a good starting point:
>
> with(df, dotchart(y, labels = x, pch = 16))
>
> -Deepayan
>
> > ```
> > > plot(df$y ~ df$x)
> > Error in plot.window(...) : need finite 'xlim' values
> > In addition: Warning messages:
> > 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> > 2: In min(x) : no non-missing arguments to min; returning Inf
> > 3: In max(x) : no non-missing arguments to max; returning -Inf
> > ```
> > which is right because x is not numeric, so I took the factor:
> > ```
> > plot(df$y ~ factor(df$x)) # gives bars instead of dots
> > plot(df$y ~ factor(df$x), pch = 16) # this also
> > ```
> > I tried to convert directly the dataframe:
> > ```
> > df$x = lapply(df$x, factor)
> > > str(df)
> > 'data.frame': 4 obs. of  2 variables:
> >  $ x:List of 4
> >   ..$ : Factor w/ 1 level "0 pmol": 1
> >   ..$ : Factor w/ 1 level "10 pmol": 1
> >   ..$ : Factor w/ 1 level "100 pmol": 1
> >   ..$ : Factor w/ 1 level "1000 pmol": 1
> >  $ y: num  0.931 1.891 2.24 2.792
> >
> > > plot(r$y ~ r$x, pch = 16)
> > Error in plot.window(...) : need finite 'xlim' values
> > In addition: Warning messages:
> > 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> > 2: In min(x) : no non-missing arguments to min; returning Inf
> > 3: In max(x) : no non-missing arguments to max; returning -Inf
> > ```
> > If I try to pass the number of levels:
> > ```
> > plot(df$y ~ factor(df$x, 1:4), pch = 16) # this draw a boxplot with
> > all data on level 1
> >
> > > df$x = lapply(df$x, factor(1:4))
> > Error in match.fun(FUN) :
> >   'factor(1:4)' is not a function, character or symbol
> > ```
> >
> > Since the transformation has given only one level (1), my questions are:
> > How do I tell R to use a dot instead of a line?
> > What is the correct way of setting factors?
> >
> >
> > --
> > Best regards,
> > Luigi
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.



--
Best regards,
Luigi

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.