Hi
First post and a relative R newbie....
I am using the vioplot library to produce some violin plots. I have an input CSV
with columns off irregular length that contain NAs. I want to strip the NAs out
and produce a multiple violin plot automatically labelled using the headers. At
the moment I do this
Code:?
ds1 = read.csv("http://www.lecturematerials.co.uk/data/spelling.csv")
library(vioplot)
y6<-na.omit(ds1$y6)
y5<-na.omit(ds1$y5)
y4<-na.omit(ds1$y4)
y3<-na.omit(ds1$y3)
y2<-na.omit(ds1$y2)
y1<-na.omit(ds1$y1)
vioplot(y6, y5, y4,y3,y2,y1,horizontal=TRUE, names=c("Y6",
"Y5","Y4","Y3","Y2","Y1"), col
= "lightblue")
Two queries:
1. Is there a more elegant way of automatically stripping the NAs, passing the
columns to the function along with the header names??
2. Can I easily add the sample size to each violin plotted??
thanks
mike
---
Mike Smith
David Winsemius
2016-Apr-30 23:16 UTC
[R] Removing NAs from dataframe (for use in Vioplot)
> On Apr 30, 2016, at 12:58 PM, Mike Smith <mike at hsm.org.uk> wrote: > > Hi > > First post and a relative R newbie.... > > I am using the vioplot library to produce some violin plots. I have an input CSV with columns off irregular length that contain NAs. I want to strip the NAs out and produce a multiple violin plot automatically labelled using the headers. At the moment I do this > > Code: > ds1 = read.csv("http://www.lecturematerials.co.uk/data/spelling.csv") > library(vioplot) > y6<-na.omit(ds1$y6) > y5<-na.omit(ds1$y5) > y4<-na.omit(ds1$y4) > y3<-na.omit(ds1$y3) > y2<-na.omit(ds1$y2) > y1<-na.omit(ds1$y1) > vioplot(y6, y5, y4,y3,y2,y1,horizontal=TRUE, names=c("Y6", "Y5","Y4","Y3","Y2","Y1"), col = "lightblue") > > > Two queries: > > 1. Is there a more elegant way of automatically stripping the NAs, passing the columns to the function along with the header names?? >ds2 <- lapply( ds1, na.omit)> 2. Can I easily add the sample size to each violin plotted??> ?violplotNo documentation for ?violplot? in specified packages and libraries: you could try ???violplot?>> thanks > > mike > > > > --- > Mike Smith > > ______________________________________________ > 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.David Winsemius Alameda, CA, USA
David Winsemius
2016-Apr-30 23:23 UTC
[R] Removing NAs from dataframe (for use in Vioplot)
> On Apr 30, 2016, at 4:16 PM, David Winsemius <dwinsemius at comcast.net> wrote: > > >> On Apr 30, 2016, at 12:58 PM, Mike Smith <mike at hsm.org.uk> wrote: >> >> Hi >> >> First post and a relative R newbie.... >> >> I am using the vioplot library to produce some violin plots.It's a package, .... not a library.>> I have an input CSV with columns off irregular length that contain NAs. I want to strip the NAs out and produce a multiple violin plot automatically labelled using the headers. At the moment I do this >> >> Code: >> ds1 = read.csv("http://www.lecturematerials.co.uk/data/spelling.csv") >> library(vioplot) >> y6<-na.omit(ds1$y6) >> y5<-na.omit(ds1$y5) >> y4<-na.omit(ds1$y4) >> y3<-na.omit(ds1$y3) >> y2<-na.omit(ds1$y2) >> y1<-na.omit(ds1$y1) >> vioplot(y6, y5, y4,y3,y2,y1,horizontal=TRUE, names=c("Y6", "Y5","Y4","Y3","Y2","Y1"), col = "lightblue") >> >> >> Two queries: >> >> 1. Is there a more elegant way of automatically stripping the NAs, passing the columns to the function along with the header names?? >> > > ds2 <- lapply( ds1, na.omit) > > >> 2. Can I easily add the sample size to each violin plotted?? > >> ?violplot > No documentation for ?violplot? in specified packages and libraries: > you could try ???violplot?I see that I mispled that _package_ name. However, after loading it I realized that I had no way of replicating what you are seeing, because you didn't provide that file (or even something that resembles it. It's rather unclear how you wanted this information presented. -- David. David Winsemius Alameda, CA, USA
Never let it be said there's only one way to do a thing:
require(ggplot2)
require(dplyr)
#create a sample dataset
dat <- data.frame(y1=sample(c(1:10,NA),20,replace=TRUE),
y2=sample(c(1:10,NA),20,replace=TRUE),
y3=sample(c(1:10,NA),20,replace=TRUE))
# convert from wide to long
dat <- melt(dat)
# add the counts as a label
dat <- merge(dat,
group_by(dat,variable) %>%
summarise(lab=paste0('n=',length(na.omit(value)))))
# do the plot
ggplot(dat,aes(x=variable,y=value)) +
geom_violin() +
geom_text(aes(y=max(value,na.rm=TRUE)/2,label=lab))
# apologies to David Winsemius for directing this answer to him, I'll work
out how to use email one day.
On Sat, Apr 30, 2016 at 12:58 PM, Mike Smith <mike at hsm.org.uk> wrote:
> Hi
>
> First post and a relative R newbie....
>
> I am using the vioplot library to produce some violin plots. I have an
> input CSV with columns off irregular length that contain NAs. I want to
> strip the NAs out and produce a multiple violin plot automatically labelled
> using the headers. At the moment I do this
>
> Code:
> ds1 =
read.csv("http://www.lecturematerials.co.uk/data/spelling.csv")
> library(vioplot)
> y6<-na.omit(ds1$y6)
> y5<-na.omit(ds1$y5)
> y4<-na.omit(ds1$y4)
> y3<-na.omit(ds1$y3)
> y2<-na.omit(ds1$y2)
> y1<-na.omit(ds1$y1)
> vioplot(y6, y5, y4,y3,y2,y1,horizontal=TRUE, names=c("Y6",
>
"Y5","Y4","Y3","Y2","Y1"), col
= "lightblue")
>
>
> Two queries:
>
> 1. Is there a more elegant way of automatically stripping the NAs, passing
> the columns to the function along with the header names??
>
> 2. Can I easily add the sample size to each violin plotted??
>
> thanks
>
> mike
>
>
>
> ---
> Mike Smith
>
> ______________________________________________
> 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.
>
[[alternative HTML version deleted]]
But require() should not be used interchangeably with library()... the return value from require() should always be tested. -- Sent from my phone. Please excuse my brevity. On May 1, 2016 3:03:59 AM GMT+01:00, Tom Wright <tom at maladmin.com> wrote:>Never let it be said there's only one way to do a thing: > >require(ggplot2) >require(dplyr) > >#create a sample dataset >dat <- data.frame(y1=sample(c(1:10,NA),20,replace=TRUE), > y2=sample(c(1:10,NA),20,replace=TRUE), > y3=sample(c(1:10,NA),20,replace=TRUE)) > ># convert from wide to long >dat <- melt(dat) > ># add the counts as a label >dat <- merge(dat, > group_by(dat,variable) %>% > summarise(lab=paste0('n=',length(na.omit(value))))) > ># do the plot >ggplot(dat,aes(x=variable,y=value)) + > geom_violin() + > geom_text(aes(y=max(value,na.rm=TRUE)/2,label=lab)) > > ># apologies to David Winsemius for directing this answer to him, I'll >work >out how to use email one day. > >On Sat, Apr 30, 2016 at 12:58 PM, Mike Smith <mike at hsm.org.uk> wrote: > >> Hi >> >> First post and a relative R newbie.... >> >> I am using the vioplot library to produce some violin plots. I have >an >> input CSV with columns off irregular length that contain NAs. I want >to >> strip the NAs out and produce a multiple violin plot automatically >labelled >> using the headers. At the moment I do this >> >> Code: >> ds1 = read.csv("http://www.lecturematerials.co.uk/data/spelling.csv") >> library(vioplot) >> y6<-na.omit(ds1$y6) >> y5<-na.omit(ds1$y5) >> y4<-na.omit(ds1$y4) >> y3<-na.omit(ds1$y3) >> y2<-na.omit(ds1$y2) >> y1<-na.omit(ds1$y1) >> vioplot(y6, y5, y4,y3,y2,y1,horizontal=TRUE, names=c("Y6", >> "Y5","Y4","Y3","Y2","Y1"), col = "lightblue") >> >> >> Two queries: >> >> 1. Is there a more elegant way of automatically stripping the NAs, >passing >> the columns to the function along with the header names?? >> >> 2. Can I easily add the sample size to each violin plotted?? >> >> thanks >> >> mike >> >> >> >> --- >> Mike Smith >> >> ______________________________________________ >> 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. >> > > [[alternative HTML version deleted]] > >______________________________________________ >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.[[alternative HTML version deleted]]
Seemingly Similar Threads
- [R-pkgs] New package: `lavaan' for latent variable analysis (including structural equation modeling)
- lavaan version 0.4-8
- lavaan version 0.4-8
- New package: `lavaan' for latent variable analysis (including structural equation modeling)
- New package: `lavaan' for latent variable analysis (including structural equation modeling)