[R] Spicing Up Native Circular Plot Using ggplot2

2016-05-01 Thread Sidoti, Salvatore A.
I have some angle data from an animal behavior study that I would like to plot 
for publication using ggplot2. What follows is my current workflow with some 
example data.

### BEGIN SCRIPT ###

### Create two data frames of random Cartesian coordinates ###
df1 <- data.frame(
x = sample(10, 11, replace = TRUE),
y = sample(10, 11, replace = TRUE))

df2 <- data.frame(
x = sample(10, 11, replace = TRUE),
y = sample(10, 11, replace = TRUE))

### Write a function that converts continuous Cartesian coordinates to 
velocities ###
get.polar <- function(df)
{
x <- diff(df$x)
y <- diff(df$y)
d <- complex(real = x, imaginary = y)
steps <- data.frame(speed = Mod(d), angle = Arg(d))
steps[-1,] # Deletes the first row as it does not contain an angle measurement
steps$time <- (1:nrow(steps))/30 # generates a time column in seconds (1 data 
point = 1/30 of a second)
return(steps)
}

df1_polar <- get.polar(df1)
df2_polar <- get.polar(df2)

require(circular)

### Convert angles into an object of type 'circular' ###
df1_rad <- circular(df1_polar$angle, type = 'angles', units = 'radians', 
zero=0, rotation = "counter")
df2_rad <- circular(df2_polar$angle, type = 'angles', units = 'radians', 
zero=0, rotation = "counter")

### Convert radians to degrees with a clockwise rotation and zero at "north" ###
df1_deg <- conversion.circular(df1_rad, type = "angles", units = "degrees", 
zero = pi/2, rotation = "clock")
df2_deg <- conversion.circular(df2_rad, type = "angles", units = "degrees", 
zero = pi/2, rotation = "clock")

### Convert negative rotations to positive ###
df1_deg[df1_deg < 0] <- df1_deg[df1_deg < 0] + 360 
df2_deg[df2_deg < 0] <- df2_deg[df2_deg < 0] + 360

par(pty = "s")
plot(df1_deg, units = "degrees")
ticks.circular(circular(seq(0,(11/6)*pi, pi/6)), zero = pi/2, rotation = 
"clock", tcl  = 0.075)
points(df2_deg, zero = pi/2, rotation = "clock", pch = 16, col = "darkgrey", 
next.points = -0.2)

### END SCRIPT ###

Some suggestions for turning this rough plot into something publishable using 
ggplot2?

Thank you!
Salvatore A. Sidoti

__
R-help@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.


Re: [R] inserting row(column) in array or dataframe at specified row(column)

2016-05-01 Thread David Winsemius

> On May 1, 2016, at 10:53 AM, Jan Kacaba  wrote:
> 
> Hello dear R users,
> 
> Is there a function or package which can insert row, column or array in
> another array at specified place (row or column)?
> 
> I have made several attempts at this function optimizing both speed, code
> readability and ease of use. The functions are of following format:
> 
> appcol=function(original_array, inserted_object, column_number,
> overwrite=FALSE)

This does not offer code, only the list of formals. If you want help with 
coding, then you should provide some. I remember a posting by Gabor 
Grothendieck on StackOverflow (where he cited another blogger) where he 
demonstrated how to implement a postfix "+" function like that of C, that I 
_think_ might address this question but without a MWE and desired result I 
cannot do any testing.

> 
>  If overwrite=TRUE the columns after column_number are ovewritten by
> inserted_object else the columns after column_number are shifted.
> 
> Now I have started using package dplyr and it seams that there is no
> inserting function either. One can only append at the end or at the
> beginning of tbl_df. Is it true?

Probably not. There's always:

> fortunes::fortune("Yoda")

Evelyn Hall: I would like to know how (if) I can extract some of the
information from the summary of my nlme.
Simon Blomberg: This is R. There is no if. Only how.
   -- Evelyn Hall and Simon 'Yoda' Blomberg
  R-help (April 2005)

>   [[alternative HTML version deleted]]

One of _your_ tasks is now to learn "how" to tell gmail to post in plaintext.

-- 

David Winsemius
Alameda, CA, USA

__
R-help@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.


Re: [R] row names, coulmn names

2016-05-01 Thread David Winsemius

> On May 1, 2016, at 11:09 AM, Jan Kacaba  wrote:
> 
> Hello dear R helpers,
> 
> Is it possible to have more than 1 row for column names in data.frame,
> array, tbl_df? I would like to have column numbers in the first row, string
> names in the second row, physical unit in third row.

It's possible to embed "\n" in names but whether that will deliver desired 
results with plotting and printing functions may be another matter. You would 
always  need to quote names, even when using "$".


> How would I do it?
> 
> Derek
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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

__
R-help@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.


Re: [R] Aggregate FIPS data to State and Census divisions

2016-05-01 Thread David Winsemius

> On May 1, 2016, at 9:30 AM, Miluji Sb  wrote:
> 
> Dear Dennis,
> 
> Thank you for your reply. I can use the dplyr/data.table packages to
> aggregate - its the matching FIPS codes to their states that I am having
> trouble. Thanks again.

So post some example code that demonstrate you paid attention to the answer 
given. Both dplyr and data.table not limited for aggregation. They do several 
versions of matching. So does the base function `merge`. We do not yet know 
what sort of efforts you have made. r-help@r-project.org is not an online 
code-writing service.

-- 
David
> 
> Sincerely,
> 
> Milu
> 
> On Sun, May 1, 2016 at 6:20 PM, Dennis Murphy  wrote:
> 
>> Hi:
>> 
>> Several such packages exist. Given the size of your data, it's likely
>> that the dplyr and data.table packages would be worth investigating.
>> Both are well documented.
>> 
>> Dennis
>> 
>> On Sun, May 1, 2016 at 8:30 AM, Miluji Sb  wrote:
>>> Dear all,
>>> 
>>> I have the following data by US FIPS code. Is there a package to
>> aggregate
>>> the data by State and Census divisions?
>>> 
>>> temp <- dput(head(pop1,5))
>>> structure(list(FIPS = c("01001", "01003", "01005", "01007", "01009"
>>> ), death_2050A1 = c(18.19158, 101.63088, 13.18896, 10.30068,
>>> 131.91798), death_2050A2 = c(22.16349, 116.58387, 15.85324, 12.78564,
>>> 155.20506), death_2050B1 = c(21.38906, 76.23018, 21.38218, 17.14269,
>>> 151.64466), death_2050B2 = c(23.43543, 81.39378, 22.96802, 18.76926,
>>> 161.86404), death_2050BC = c(21.89947, 93.88002, 18.60352, 15.1032,
>>> 152.43414)), .Names = c("FIPS", "death_2050A1", "death_2050A2",
>>> "death_2050B1", "death_2050B2", "death_2050BC"), row.names = c(NA,
>>> 5L), class = "data.frame")
>>> 
>>> Thank you!
>>> 
>>> Sincerely,
>>> 
>>> Milu
>>> 
>>>[[alternative HTML version deleted]]
>>> 
>>> __
>>> R-help@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@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

__
R-help@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.


Re: [R] inserting row(column) in array or dataframe at specified row(column)

2016-05-01 Thread Tom Wright
If you can address your columns by name then order shouldn't matter. If the
column order does matter, perhaps a matrix is a better structure to use?

On Sun, May 1, 2016, 10:56 AM Jan Kacaba  wrote:

> Hello dear R users,
>
> Is there a function or package which can insert row, column or array in
> another array at specified place (row or column)?
>
> I have made several attempts at this function optimizing both speed, code
> readability and ease of use. The functions are of following format:
>
> appcol=function(original_array, inserted_object, column_number,
> overwrite=FALSE)
>
> # If overwrite=TRUE the columns after column_number are ovewritten by
> inserted_object else the columns after column_number are shifted.
>
> Now I have started using package dplyr and it seams that there is no
> inserting function either. One can only append at the end or at the
> beginning of tbl_df. Is it true?
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


Re: [R] row names, coulmn names

2016-05-01 Thread Tom Wright
I think what you ask isn't ideal.Each column in a dataframe should be the
same data type. While column names are stored in the first row when the df
is exported to CSV, they are not stored as columns in the data frame.
Instead the column names are stored as a separate attribute of the df. This
is why you need to use names(df) to access them, not df[1,]. I don't have
access to R right now, but I think ?attributes or ??"attributes" should
point you in the correct direction.

On Sun, May 1, 2016, 11:11 AM Jan Kacaba  wrote:

> Hello dear R helpers,
>
> Is it possible to have more than 1 row for column names in data.frame,
> array, tbl_df? I would like to have column numbers in the first row, string
> names in the second row, physical unit in third row.
> How would I do it?
>
> Derek
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


[R] row names, coulmn names

2016-05-01 Thread Jan Kacaba
Hello dear R helpers,

Is it possible to have more than 1 row for column names in data.frame,
array, tbl_df? I would like to have column numbers in the first row, string
names in the second row, physical unit in third row.
How would I do it?

Derek

[[alternative HTML version deleted]]

__
R-help@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.


[R] inserting row(column) in array or dataframe at specified row(column)

2016-05-01 Thread Jan Kacaba
Hello dear R users,

Is there a function or package which can insert row, column or array in
another array at specified place (row or column)?

I have made several attempts at this function optimizing both speed, code
readability and ease of use. The functions are of following format:

appcol=function(original_array, inserted_object, column_number,
overwrite=FALSE)

# If overwrite=TRUE the columns after column_number are ovewritten by
inserted_object else the columns after column_number are shifted.

Now I have started using package dplyr and it seams that there is no
inserting function either. One can only append at the end or at the
beginning of tbl_df. Is it true?

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Aggregate FIPS data to State and Census divisions

2016-05-01 Thread Miluji Sb
Dear Dennis,

Thank you for your reply. I can use the dplyr/data.table packages to
aggregate - its the matching FIPS codes to their states that I am having
trouble. Thanks again.

Sincerely,

Milu

On Sun, May 1, 2016 at 6:20 PM, Dennis Murphy  wrote:

> Hi:
>
> Several such packages exist. Given the size of your data, it's likely
> that the dplyr and data.table packages would be worth investigating.
> Both are well documented.
>
> Dennis
>
> On Sun, May 1, 2016 at 8:30 AM, Miluji Sb  wrote:
> > Dear all,
> >
> > I have the following data by US FIPS code. Is there a package to
> aggregate
> > the data by State and Census divisions?
> >
> > temp <- dput(head(pop1,5))
> > structure(list(FIPS = c("01001", "01003", "01005", "01007", "01009"
> > ), death_2050A1 = c(18.19158, 101.63088, 13.18896, 10.30068,
> > 131.91798), death_2050A2 = c(22.16349, 116.58387, 15.85324, 12.78564,
> > 155.20506), death_2050B1 = c(21.38906, 76.23018, 21.38218, 17.14269,
> > 151.64466), death_2050B2 = c(23.43543, 81.39378, 22.96802, 18.76926,
> > 161.86404), death_2050BC = c(21.89947, 93.88002, 18.60352, 15.1032,
> > 152.43414)), .Names = c("FIPS", "death_2050A1", "death_2050A2",
> > "death_2050B1", "death_2050B2", "death_2050BC"), row.names = c(NA,
> > 5L), class = "data.frame")
> >
> > Thank you!
> >
> > Sincerely,
> >
> > Milu
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@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@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.


Re: [R] Removing NAs from dataframe (for use in Vioplot)

2016-05-01 Thread David Winsemius

> On May 1, 2016, at 12:15 AM, Mike Smith  wrote:
> 
 On Apr 30, 2016, at 12:58 PM, Mike Smith  wrote:
> 
 Hi
> 
 First post and a relative R newbie
> 
 I am using the vioplot library to produce some violin plots.
> 
> DW> 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)
> 
> 
> Fantastic - that does the trick! Easy when you know how!! 
> 
> Follow-on: is there a way feed all the lists from ds2 to vioplot? It is now a 
> series of lists (rather than a dataframe - is that right?). So this works, 
> 
> library(vioplot)
> ds1 = read.csv("http://www.lecturematerials.co.uk/data/spelling.csv";)
> ds2 <- lapply( ds1, na.omit)
> vioplot(ds2$y1,ds2$y2)
> 
> but this doesnt
> 
> library(vioplot)
> ds1 = read.csv("http://www.lecturematerials.co.uk/data/spelling.csv";)
> ds2 <- lapply( ds1, na.omit)
> vioplot(ds2)
> 
Error in min(data) : invalid 'type' (list) of argument


I had trouble, too. I thought, "Oh, this is easy, just use `do.call`", but I 
failed in getting any successful argument passing that way. 

> do.call('vioplot', list(x=ds2[[6]], ds2[-6]) )
Error in min(data) : invalid 'type' (list) of argument
> do.call('vioplot', c(x=ds2[[6]], ds2[-6]) )
Error in vioplot(x1 = 5L, x2 = 10L, x3 = 6L, x4 = 7L, x5 = 7L, x6 = 6L,  : 
  argument "x" is missing, with no default

Eventually I re-wrote the first line of vioplot's body to behave the way I 
thought made the most sense:

 vioplot <- 
function (x, ..., range = 1.5, h = NULL, ylim = NULL, names = NULL, 
horizontal = FALSE, col = "magenta", border = "black", lty = 1, 
lwd = 1, rectCol = "black", colMed = "white", pchMed = 19, 
at, add = FALSE, wex = 1, drawRect = TRUE) 
{
datas <- c(list(x), ...)
#  but keep the rest the same.

# I then get success with:

vioplot(ds2[['y1']], ds2[-6])  # success

do.call('vioplot', list(x=ds2[[6]], ds2[-6]) ) # also successes
do.call('vioplot', list(x=ds2[['y1']], ds2[-6]) )

This is retracing a route explored 8 years ago:

http://markmail.org/search/?q=list%3Aorg.r-project.r-help+list+argument+to+vioplot#query:list%3Aorg.r-project.r-help%20list%20argument%20to%20vioplot+page:1+mid:j6lapgri46utcod7+state:results


It's probably easier to use that helper-function approach than my efforts at 
hacking.

Best of luck;

David


 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’
> 
> DW> I see that I mispled that _package_ name. However, after loading
> DW> it I realized that I had no way of replicating what you are
> DW> seeing, because you didn't provide that file (or even something
> DW> that resembles it. It's rather unclear how you wanted this information 
> presented.
> 
> The original code *should* have worked as the csv was online. There doesnt 
> seem to be any option in vioplot to add the sample size (these are all small 
> samples which I wanted to highlight) so I dont know if this is easily done 
> elsewhere.
> 
> Thanks again!!
> ---
> Mike Smith
> 

David Winsemius
Alameda, CA, USA

__
R-help@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.

[R] Aggregate FIPS data to State and Census divisions

2016-05-01 Thread Miluji Sb
Dear all,

I have the following data by US FIPS code. Is there a package to aggregate
the data by State and Census divisions?

temp <- dput(head(pop1,5))
structure(list(FIPS = c("01001", "01003", "01005", "01007", "01009"
), death_2050A1 = c(18.19158, 101.63088, 13.18896, 10.30068,
131.91798), death_2050A2 = c(22.16349, 116.58387, 15.85324, 12.78564,
155.20506), death_2050B1 = c(21.38906, 76.23018, 21.38218, 17.14269,
151.64466), death_2050B2 = c(23.43543, 81.39378, 22.96802, 18.76926,
161.86404), death_2050BC = c(21.89947, 93.88002, 18.60352, 15.1032,
152.43414)), .Names = c("FIPS", "death_2050A1", "death_2050A2",
"death_2050B1", "death_2050B2", "death_2050BC"), row.names = c(NA,
5L), class = "data.frame")

Thank you!

Sincerely,

Milu

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Removing NAs from dataframe (for use in Vioplot)

2016-05-01 Thread Mike Smith
>>> On Apr 30, 2016, at 12:58 PM, Mike Smith  wrote:

>>> Hi

>>> First post and a relative R newbie

>>> I am using the vioplot library to produce some violin plots.

DW> 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)


Fantastic - that does the trick! Easy when you know how!! 

Follow-on: is there a way feed all the lists from ds2 to vioplot? It is now a 
series of lists (rather than a dataframe - is that right?). So this works, 

library(vioplot)
ds1 = read.csv("http://www.lecturematerials.co.uk/data/spelling.csv";)
ds2 <- lapply( ds1, na.omit)
vioplot(ds2$y1,ds2$y2)

but this doesnt

library(vioplot)
ds1 = read.csv("http://www.lecturematerials.co.uk/data/spelling.csv";)
ds2 <- lapply( ds1, na.omit)
vioplot(ds2)

>>> 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’

DW> I see that I mispled that _package_ name. However, after loading
DW> it I realized that I had no way of replicating what you are
DW> seeing, because you didn't provide that file (or even something
DW> that resembles it. It's rather unclear how you wanted this information 
presented.

The original code *should* have worked as the csv was online. There doesnt seem 
to be any option in vioplot to add the sample size (these are all small samples 
which I wanted to highlight) so I dont know if this is easily done elsewhere.

Thanks again!!
---
Mike Smith

__
R-help@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.