How to create multiple rank plot in R - r

I have a dataset of 10 variables and 150 observation.
ebi anago maguro ika uni sake tamago toro tekka.maki kappa.maki
7 4 5 1 0 2 8 3 9 6
1 4 5 7 2 0 8 6 9 3
7 2 5 4 8 1 0 3 9 6
4 7 5 1 2 0 3 8 6 9
4 5 7 2 0 3 8 1 6 9
4 5 7 2 0 3 1 8 6 9
5 7 4 1 0 2 3 8 9 6
5 4 1 6 7 2 0 8 3 9
5 7 2 3 8 4 9 0 6 1
1 7 2 0 8 3 5 4 6 9
4 7 5 1 8 2 3 9 6 0
7 5 0 4 2 3 8 6 1 9
4 7 0 5 2 1 8 3 6 9
4 5 7 0 3 1 2 6 8 9
7 4 0 2 5 3 1 8 9 6
7 5 4 0 2 3 8 1 6 9
2 7 0 8 6 3 1 9 5 4
7 2 5 4 3 0 8 1 6 9
7 5 0 2 1 6 8 9 3 4
7 4 5 0 3 1 2 8 6 9
Every variable is the rank of the agent preference of sushi type and I'd like to create multiple plot in the same image like the one in the photo.
Any help?

Something like this, maybe?
library(ggplot2)
library(reshape2)
rank=read.csv('rank.csv')
melt=melt(rank, id.vars=NULL)
melt$value=factor(melt$value, c(9:0))
ggplot(melt, aes(value)) + geom_bar() + facet_wrap(~variable, 5,5)

Related

Creating a vector with specified order

I have to create a vector in R with a specified order. I need 30 0s, then 30 1's, then 30 2's, and so on until 9. I was thinking I could create an empty vector and then create a loop that would add in my numbers but I'm not sure how to go about it. The code below is something along the lines of what I was thinking could possibly work.
labels <- c()
i = 0
for(i in 0:30){
append(labels,0)
i += 1
}
rep(0:9, each = 30)
#> [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
#> [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2
#> [75] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
#> [112] 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
#> [149] 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6
#> [186] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7
#> [223] 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
#> [260] 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
#> [297] 9 9 9 9

Limit Number of Items Displayed in Legend - GGplot R

I have a large taxonomic dataset that I need to plot as a stacked bar chart. Sample Data:
ID X A B C D E F G
1 5 9 6 7 4 8 10 6
2 6 3 9 10 3 10 4 8
3 6 6 5 8 8 8 8 1
4 9 3 2 8 4 1 5 8
5 6 6 2 8 3 7 4 10
6 0 7 8 9 1 4 9 10
7 3 2 6 8 8 1 8 7
8 4 7 10 2 9 7 9 8
9 5 7 9 10 8 2 2 1
10 0 4 6 8 9 10 7 1
11 8 9 2 2 6 5 1 7
12 8 6 0 9 7 9 8 1
13 2 8 4 4 4 2 6 7
14 4 6 6 4 9 9 3 5
15 8 1 0 6 5 8 1 1
16 6 6 9 3 9 2 1 1
17 2 4 0 2 4 8 10 9
18 5 9 8 9 4 9 3 9
19 0 2 1 6 6 9 6 2
20 3 3 7 10 4 5 6 8
21 2 6 6 9 8 10 9 4
22 7 7 1 6 8 3 7 1
23 1 9 4 5 8 9 7 7
24 0 8 5 9 1 8 9 1
25 2 1 0 1 1 2 10 7
26 10 4 1 8 2 5 9 0
27 2 7 10 10 2 3 8 6
28 6 4 2 6 7 3 1 0
29 8 1 3 4 1 10 3 6
30 1 6 5 4 7 9 7 10
31 4 4 3 2 2 9 0 4
32 9 6 6 1 6 1 5 2
The plotting part is no problem, using gggplot as below:-
l5 <- read.xlsx(paste(taxawmeta,taxawmeta_files[2], sep = ""), sheetIndex = 1)
l5_long <- l5 %>% gather(taxa,value,-c(X.FinalSampleID,TimePoint_Luna))
ggplot(l5_long, aes(fill=taxa, y = value, x = X.FinalSampleID, )) +
geom_bar(position='stack', stat='identity') +
theme_minimal() +
labs(x='Sample', y='Relative Abundance', title='Family Level Relative Abundance') +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position="none")
Where I'm running into an issue is the actual dataset has almost 200 variables. Meaning the legend is completely out of control. I know I can just hide the legend with:-
theme(.position="none")
... but what I'd like to do is keep say the top 10 entries as those are the ones of most interest. Is there any simple method to limit the number of items that are displayed in the legend? Anything I've found so far seems very convoluted and not directly applicable to this problem.

Read a hashtagged "##" textual data as a data.frame

I have a textual dataset (m) that is shown below. I was wondering given its hash-tagged format, is there is way to read it correctly in R as a data.frame?
m <-
"
## Item1 Item2 Item3 Item4 Item5 Item6 Item7 Item8 Item9 Item10 Item11
## 1 0 4 5 1 2 8 9 7 6 4 3
## 2 4 0 9 7 1 5 6 7 8 5 4
## 3 5 9 0 6 3 1 2 4 5 7 9
## 4 1 7 6 0 5 4 3 2 7 8 9
## 5 2 1 3 5 0 4 6 7 8 1 2
## 6 8 5 1 4 4 0 1 2 4 3 2
## 7 9 6 2 3 6 1 0 6 7 5 2
## 8 7 7 4 2 7 2 6 0 4 5 3
## 9 6 8 5 7 8 4 7 4 0 6 7
## 10 4 5 7 8 1 3 5 5 6 0 5
## 11 3 4 9 9 2 2 2 3 7 5 0
## 12 2 2 4 2 3 3 3 2 8 6 5
## 13 1 3 6 7 4 1 1 6 9 4 6
## 14 4 4 8 9 9 3 2 5 1 2 7
## 15 3 5 9 9 7 2 3 3 2 5 4
## Item12 Item13 Item14 Item15
## 1 2 1 4 3
## 2 2 3 4 5
## 3 4 6 8 9
## 4 2 7 9 9
## 5 3 4 9 7
## 6 3 1 3 2
## 7 3 1 2 3
## 8 2 6 5 3
## 9 8 9 1 2
## 10 6 4 2 5
## 11 5 6 7 4
## 12 0 7 8 8
## 13 7 0 9 4
## 14 8 9 0 6
## 15 8 4 6 0
"
Easiest it to remove those annoying ## and then read the two parts of the table separately.
Bind them together at the end to get your expected result.
m <- gsub("## ", "", m)
m1 <- read.table(text = m, nrows = 15)
m2 <- read.table(text = m, skip = 17)
cbind(m1, m2)
Item1 Item2 Item3 Item4 Item5 Item6 Item7 Item8 Item9 Item10 Item11 Item12 Item13 Item14 Item15
1 0 4 5 1 2 8 9 7 6 4 3 2 1 4 3
2 4 0 9 7 1 5 6 7 8 5 4 2 3 4 5
3 5 9 0 6 3 1 2 4 5 7 9 4 6 8 9
4 1 7 6 0 5 4 3 2 7 8 9 2 7 9 9
5 2 1 3 5 0 4 6 7 8 1 2 3 4 9 7
6 8 5 1 4 4 0 1 2 4 3 2 3 1 3 2
7 9 6 2 3 6 1 0 6 7 5 2 3 1 2 3
8 7 7 4 2 7 2 6 0 4 5 3 2 6 5 3
9 6 8 5 7 8 4 7 4 0 6 7 8 9 1 2
10 4 5 7 8 1 3 5 5 6 0 5 6 4 2 5
11 3 4 9 9 2 2 2 3 7 5 0 5 6 7 4
12 2 2 4 2 3 3 3 2 8 6 5 0 7 8 8
13 1 3 6 7 4 1 1 6 9 4 6 7 0 9 4
14 4 4 8 9 9 3 2 5 1 2 7 8 9 0 6
15 3 5 9 9 7 2 3 3 2 5 4 8 4 6 0
if all rows start with "##" and if you know that true rows have a row number in the beginning and if all header start with "Item" (lots of ifs :D) you could do something awful like this (which would give you some flexibility on the data structure)
# split according to seperator and linebreak
strsplit(m,split="\n|##") %>%
.[[1]] %>%
.[.!=""] %>%
# split according to row names
strsplit(split="^ [0-9]{1,2}") %>%
unlist() %>%
.[.!=""] %>%
dplyr::as_tibble() %>%
# flag header
dplyr::mutate(HEADER=grepl("Item",value)) %>%
# split into groups (each group will be used with bind_cols)
dplyr::mutate(GRP=cumsum(HEADER)) %>%
dplyr::group_by(GRP) %>%
dplyr::group_split() %>%
dplyr::bind_cols() %>%
# reduce to relevant columns
dplyr::select(dplyr::matches("value")) %>%
magrittr::set_colnames(paste0("X",seq_len(ncol(.)))) %>%
tidyr::unite("X",dplyr::everything(),sep=" ") %>%
# create comma seperated list
.$X %>%
gsub("^ +| +$","",.) %>%
gsub(" +",",",.)

How can I read an Elliptical Fourier Descriptor from SHAPE under the Momocs package of Rstudio?

I wish to load a CHC or NEF file from the SHAPE software in order to read the contours under MOMOCS and make the related analyses (Plot, PCA...)
I have difficulties under R as well as under the package.
How should I proceed?
Here is an extract of an outline. The first one is from a CHC and the other one from NEF.
Thank you in advance, any help is welcome.
CHC
IMG_20200710_0001_1 1045 531 1,428639E-02 46736 5 5 4 4 4 5 4 4 4 5 5 4 5 5 5 5 5 5 4 5 6 5 4 5 5 5 6 5 5 6 5 5 5 5 6 5 6 5 5 6 5 5 6 5 6 5 5 6 6 5 6 5 6 5 6 5 6 5 6 6 5 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 5 6 5 6 6 5 6 6 6 5 5 6 6 5 6 6 5 6 6 5 6 6 5 5 6 6 6 6 5 6 5 6 6 6 5 6 5 6 6 6 6 5 6 6 6 5 6 5 6 6 6 6 6 5 6 6 6 5 6 6 5 6 6 6 5 6 6 5 6 6 5 6 6 5 6 6 6 5 6 6 6 6 5 6 6 5 6 6 5 6 6 6 6 5 6 6 5 6 6 6 5 6 6 6 6 6 6 6 5 6 6 6 5 6 5 6 6 6 6 6 6 6 5 6 6 5 6 6 6 5 6 6 6 5 6 6 5 6 6 6 6 5 6 6 6 6 6 5 6 6 6 6 6 6 6 6 6 6 6 6 6 5 6 6 6 6 6 6 6 6 6 6 5 6 6 6 6 6 6 6 6 5 6 6 6 6 6 6 6 6 5 6 6 6 5 6 6 6 6 6 6 6 5 6 6 6 6 6 6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 2 2 2 1 2 2 2 1 2 2 2 2 1 0 2 2 2 2 1 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 3 1 0 2 1 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 3 1 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 1 2 2 3 2 2 2 2 3 2 2 2 2 2 2 2 2 2 3 2 2 2 2 3 2 2 2 3 2 2 3 2 2 3 2 2 3 2 3 2 3 2 2 3 3 2 4 3 2 3 3 4 4 -1
NEF
#CONST 1 1 1 0
#HARMO 15
IMG_20200710_0001_1
1.0000000e+00 3.5723303e-18 -2.2782437e-17 1.4062862e-01
-3.5345700e-03 -2.9677476e-04 -7.7529255e-02 2.8661995e-02
1.0149772e-01 3.3103762e-03 1.0055048e-03 3.3982778e-02
-5.6327330e-04 -3.2074328e-03 -5.6057424e-03 4.8512662e-03
3.8131276e-02 -1.5129959e-05 2.2552160e-03 1.4017338e-02
1.1789915e-04 -2.9600305e-03 -8.4295737e-04 -3.4299796e-04
1.7271413e-02 -7.2651006e-04 -1.2037379e-04 7.6870442e-03
-2.0549746e-04 -1.3433972e-03 -1.8426509e-03 -5.7843862e-04
1.0074513e-02 -9.3238318e-04 4.8671011e-05 5.6731905e-03
-7.5994187e-04 -1.0418345e-03 -7.7574542e-04 -2.8944747e-04
6.4933570e-03 -4.0572074e-04 -7.6458810e-04 4.0939891e-03
-5.4921053e-04 -8.4817676e-04 1.6206266e-04 -4.6108536e-04
4.5195469e-03 -5.4106090e-04 1.6321172e-04 3.1721989e-03
-1.8313986e-04 -3.5979483e-04 4.8276592e-04 -9.0690768e-04
3.0959844e-03 -6.1876139e-04 -6.3948596e-04 2.2105213e-03
In Momocs version 1.3.2 installed from GitHub https://github.com/MomX/Momocs/ I see the function chc2pix() that should import SHAPE's chain codes to R. In earlier Momocs documentation (https://www.rdocumentation.org/packages/Momocs/versions/0.2-03/topics/nef2Coe), you can find (in Examples!) the function nef2Coe(). I do not see this function in the current Momocs 1.3.2 but you can copy the whole function to your R script or save it to .txt and call it from R as source('path/to/your/NEF2COE.txt'). When the Momocs will be fully resurrected as MomX (https://momx.github.io/MomX/), perhaps the function again will be a part of the package.
sorry for the delay.
If you want to use CHC, then chc2pix is your friend indeed. You end up with an traditionnal outline as defined by its (x, y) coordinates.
For the NEF, it's "just" a simple string manipulation task. The clue pointed by #ordynets would likely work (or at least be very useful). Another option would be to go around :
# read the nef
m <- read.table("~/Desktop/nef.txt", skip=3)
# transpose to get in the "right" order
coe <- m %>% t %>% c %>% matrix(nrow=1)
# name columns to make thngs clear
colnames(coe) <- paste0(rep(LETTERS[1:4], times=nrow(m)),
rep(1:nrow(m), each=4))
# from now you can do whatever you want in Momocs
# eg rbind many to have an OutCoe, calculate the inverse EFT:
coe %>% coeff_rearrange("name") %>% coeff_split(nb.h=10) %>% efourier_i() %>% coo_plot()
(by the way, I'm still working on MomX, and such wrapper would fit in Momit but I struggle to find time to finish it).
do not hesitate to contact me directly !

Why isn't my histogram showing the number 9?

I have a variable named labels with the following values:
[1] 1 0 1 4 0 0 7 3 5 3 8 9 1 3 3 1 2 0 7 5 8 6 2 0 2 3 6 9 9 7 8 9 4 9 2 1 3
[38] 1 1 4 9 1 4 4 2 6 3 7 7 4 7 5 1 9 0 2 2 3 9 1 1 1 5 0 6 3 4 8 1 0 3 9 6 2
[75] 6 4 7 1 4 1 5 4 8 9 2 9 9 8 9 6 3 6 4 6 2 9 1 2 0 5 9 2 7 7 2 8 8 5 0 6 0
[112] 0 2 9 0 4 7 7 1 5 7 9 4 6 1 5 7 6 5 0 4 8 7 6 1 8 7 3 7 3 1 0 3 4 5 4 0 5
[149] 4 0 3 5 1 0 8 3 7 0 9 6 6 9 5 4 6 9 3 5 4 2 4 8 7 7 5 8 8 8 2 6 9 3 1 0 4
[186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3 0 8 4 8 0 0 9 7 7 9 8 2 1 6 5 5 1 1 9 7 7 8 6 4 7 5 3 1 6 4 5 7
[704] 4 1 8 3 5 1 7 1 1 8 6 4 3 8 3 1 2 8 9 0 9 1 2 3 3 0 3 0 2 0 3 3 8 3 5 7 0
[741] 5 9 0 5 9 1 5 1 1 2 6 5 5 4 5 1 6 0 2 2 8 0 7 1 0 8 5 6 3 2 9 4 3 6 0 3 4
[778] 1 5 9 3 0 5 0 6 2 7 6 6 6 9 6 7 8 2 0 6 0 8 9 5 3 6 7 4 3 9 7 2 0 4 7 2 2
[815] 8 2 7 0 4 0 5 2 8 7 7 9 1 4 0 1 1 2 3 6 2 0 6 6 1 9 4 5 2 7 7 8 9 5 8 3 8
[852] 5 6 2 0 9 7 1 8 2 6 9 8 4 9 4 1 3 8 4 0 7 7 3 7 6 6 8 8 2 7 0 4 3 7 7 0 8
[889] 4 7 4 0 6 9 8 6 0 1 6 4 5 2 7 3 6 2 2 9 2 7 4 8 7 2 9 5 3 4 8 0 4 4 6 5 6
[926] 1 2 2 8 4 5 7 8 0 6 8 9 1 7 7 2 6 3 9 9 1 0 4 2 5 4 4 9 2 6 7 2 8 3 3 2 7
[963] 0 4 7 0 7 7 8 1 7 3 7 8 0 1 0 2 9 7 6 2 2 6 9 0 6 8 8 9 6 3 5 0 2 2 5 9 6
[1000] 4
For some reason when I run hist(labels) it returns the following histogram which has no occurrences of the number 9.
What Am i missing here?
The last bar counts the occurrences of 9. You can check that by looking at the output of hist().
R> x <- rep(1:9, each=10)
R> hh <- hist(x, breaks=0:9) # also creates chart
R> str(hh)
List of 7
$ breaks : int [1:10] 0 1 2 3 4 5 6 7 8 9
$ counts : int [1:9] 10 10 10 10 10 10 10 10 10
$ intensities: num [1:9] 0.111 0.111 0.111 0.111 0.111 ...
$ density : num [1:9] 0.111 0.111 0.111 0.111 0.111 ...
$ mids : num [1:9] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5
$ xname : chr "x"
$ equidist : logi TRUE
- attr(*, "class")= chr "histogram"
R>
Nine counts of 10 as we expected.
You can use axis to define the axis. (plot.histogram has some default values)
labels <- sample(1:9, 1000, replace = T)
.h <- hist(labels)
# if you save the histogram, then you can refer to the components
axis(1, at = .h$breaks))

Resources