[R] plot numeric vs character

Duncan Mackay dulcalma at bigpond.com
Fri Mar 18 00:52:08 CET 2016


I haven't been following the thread but!
If you want to use lattice xyplot

# create x values in their right position -- 
# assuming equal spacing
mydf$x = rep(1:3, each = 3)

library(lattice)
xyplot(num ~ x, mydf,
             scales = list(x = list(at = 1:3, label = letters[1:3])),
             xlab = "")

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ivan
Calandra
Sent: Thursday, 17 March 2016 23:26
To: R list
Cc: Anne CANER
Subject: Re: [R] plot numeric vs character

So it looks like there is no better "base" solution than Petr's code...
Thank you for your input anyway!

Ivan

--
Ivan Calandra, PhD
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calandra at univ-reims.fr
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/

Le 17/03/2016 13:04, John Kane a écrit :
> Another approach using ggplot2 and shamelessly swiped from
>
http://www.sthda.com/english/wiki/ggplot2-dot-plot-quick-start-guide-r-softw
are-and-data-visualization.
>
> library(ggplot2)
> ggplot(mydf, aes(x=let, y=num)) +
>    geom_dotplot(binaxis='y', stackdir='center', dotsize = 0.5)
>
>
>
>
> John Kane
> Kingston ON Canada
>
>
>> -----Original Message-----
>> From: ivan.calandra at univ-reims.fr
>> Sent: Thu, 17 Mar 2016 11:29:52 +0100
>> To: r-help at r-project.org
>> Subject: [R] plot numeric vs character
>>
>> Dear useRs,
>>
>> I would like to plot data points in a simple scatterplot. I don't have a
>> lot of data per category, so a boxplot does not make sense.
>>
>> Here are some sample data:
>> mydf <- data.frame(let=rep(letters[1:3],each=3), num=rnorm(9),
>> stringsAsFactors=FALSE)
>>
>> I would like to do that, which throws an error, most likely because x is
>> character:
>> plot(mydf$let, mydf$num)
>>
>> If I convert to factor(), it plots a boxplot with no possibility (AFAIK)
>> to plot points:
>> mydf$let <- factor(mydf$let)
>> plot(mydf$let, mydf$num, type='p')
>>
>> I know I can use the function points() in a somewhat convoluted manner:
>> plot(mydf$num, xlim=c(1,3), type='n', xaxt='n')
>> axis(side=1, at=1:3, labels=levels(mydf$let))
>> points(as.numeric(mydf$let), mydf$num)
>>
>> Isn't there a simple(r) way? Maybe I just missed something obvious...
>>
>> Thank you in advance for your help,
>> Ivan
>>
>> --
>> Ivan Calandra, PhD
>> University of Reims Champagne-Ardenne
>> GEGENAA - EA 3795
>> CREA - 2 esplanade Roland Garros
>> 51100 Reims, France
>> +33(0)3 26 77 36 89
>> ivan.calandra at univ-reims.fr
>> --
>> https://www.researchgate.net/profile/Ivan_Calandra
>> https://publons.com/author/705639/
>>
>> ______________________________________________
>> R-help at 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.
> ____________________________________________________________
> Can't remember your password? Do you need a strong and secure password?
> Use Password manager! It stores your passwords & protects your account.
> Check it out at http://mysecurelogon.com/manager
>
>
>

______________________________________________
R-help at 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.



More information about the R-help mailing list