[R] cannot coerce class '"expression"' to a data.frame

Troels Ring tr|ng @end|ng |rom gvdnet@dk
Sun Mar 22 17:27:48 CET 2020


Thanks a lot - this does indeed do the trick!

-----Oprindelig meddelelse-----
Fra: peter dalgaard <pdalgd using gmail.com> 
Sendt: 21. marts 2020 18:08
Til: Troels Ring <tring using gvdnet.dk>
Emne: Re: [R] cannot coerce class '"expression"' to a data.frame

Oops, disregard previous...

I'm no ggplot expert, but google "ggplot plotmath" suggests that it wants unparsed expressions, so:

dd <- data.frame(x=1:7, ID = I(ID), ID2 = sapply(ID, deparse))
ggplot(data=dd,aes(x=x,y=x,label=ID2)) + geom_point(color="blue",size=4) + geom_label(parse=TRUE)

seems to do something in the right direction.

-p

> On 21 Mar 2020, at 13:44 , Troels Ring <tring using gvdnet.dk> wrote:
> 
> Thanks a lot - actually I was on my way to ggplot2 - and still gets 
> struck - sorry 😊
> 
> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>        expression(paste(H[2]*SO[4]," 7 no  Na")),
>        expression(paste(HNO[3]," 7 No Na")))
> 
> dd <- data.frame(x=1:7, ID = I(ID))
> 
> library(ggplot2)
> 
> ggplot(data=dd,aes(x=x,y=x,label=ID))+geom_point(color="blue",size=4)
> # Error: Aesthetics must be valid data columns. Problematic aesthetic(s): label = ID. 
> # Did you mistype the name of a data column or forget to add stat()?
> 
> -----Oprindelig meddelelse-----
> Fra: peter dalgaard <pdalgd using gmail.com>
> Sendt: 21. marts 2020 11:14
> Til: Troels Ring <tring using gvdnet.dk>
> Emne: Re: [R] cannot coerce class '"expression"' to a data.frame
> 
> This seems to work:
> 
> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>        expression(paste(HSO[4]," 7 no  Na")),
>        expression(paste(HNO[3]," 7 No Na")))
> 
> ## Actually H[2]*SO[4], no?
> 
> dd <- data.frame(x=1:7, ID = I(ID))
> plot(dd$x, dd$x, typ="n")
> text(dd$x, dd$x, dd$ID)
> 
> 
>> On 21 Mar 2020, at 09:44 , Troels Ring <tring using gvdnet.dk> wrote:
>> 
>> Dear friends - I have some old data of chemical results and want to 
>> annotate the points with chemical formula. If I just do
>> 
>> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>> 
>> "HSO4 7 no  Na",
>> 
>>       "HNO3 7 No Na")
>> 
>> I can make a data.frame like
>> 
>> DD <- data.frame(x=1:7,ID)
>> 
>> 
>> 
>> and plotting works fine. 
>> 
>> But if I try to format sulfuric acid and nitric acid like
>> 
>> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>> 
>>       expression(paste(HSO[4]," 7 no  Na")),
>> 
>>       expression(paste(HNO[3]," 7 No Na")))
>> 
>> 
>> 
>> DD <- data.frame(x=1:7,ID)
>> 
>> Elicits an error
>> 
>> Error in as.data.frame.default(x[[i]], optional = TRUE) : 
>> 
>> cannot coerce class '"expression"' to a data.frame
>> 
>> and plotting is prohibited - so how is this bypassed?
>> 
>> 
>> 
>> I'm on Windows 10,
>> 
>> R version 3.6.1 (2019-07-05)
>> 
>> 
>> 
>> All best wishes
>> 
>> Troels Ring, MD
>> 
>> Aalborg, Denmark
>> 
>> 
>> This email has been scanned by BullGuard antivirus protection.
>> For more info visit www.bullguard.com 
>> <http://www.bullguard.com/tracking.aspx?affiliate=bullguard&buyaffili
>> a
>> te=smt
>> p&url=/>
>> 
>> 	[[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help using r-project.org 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.
> 
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 
> 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> This email has been scanned by BullGuard antivirus protection.
> For more info visit www.bullguard.com
> 
> 

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com











This email has been scanned by BullGuard antivirus protection.
For more info visit www.bullguard.com



More information about the R-help mailing list