Multidimensional Scale in R - data - r

I would like to have a multidimensional scaling plot according to the following table (this is just a shorter form of the whole table).
I have been trying to do it in R (am quite new here...) but now. I am not even sure about that this type of data is good for multidimensional scaling. The whole table should mirror a semantic (linguistic) map (Thats why I thought that MDS should be good) and the rows mean that informants saw some pictures and gave different expressions (columns) for the pictures, so they described them differently.
The numbers in the columns are no judgments in the sense that they are on a scale from 1 to 10 or something like that but they show how many people used the expression for pic1, pic2, and so forth.
Could anyone help me to explain that MDS is actually the appropriate model I am trying to use? (Sorry, I am just too much confused after reading a lot in the last days about different methods...)
If so, here is the coding I used (just to be sure).
Thanks a lot for any advice!
daten <- structure(list(photos = c("p1", "p5", "p8", "p13", "p19", "p23", "p29", "p34", "p36", "p40", "p59", "p2", "p14"), expression1 = c(18, 8, 11, 15, 14, 16, 10, 12, 15, 18, 18, 0, 0), expression2 = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0), expression3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1), expression4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 17), expression5 = c(0, 3, 5, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0), expression6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression8 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"))
library("tibble")
has_rownames(daten)
cr<-column_to_rownames(daten, var="photo")
has_rownames(cr)
matr_cr <- as.matrix(cr[,-1])
matr_cr
d<-dist(matr_cr)
fit <- cmdscale(d, eig = TRUE, k = 2)
x <- fit$points[, 1]
y <- fit$points[, 2]
plot(x, y, xlab="Coordinate 1", ylab="Coordinate 2",
main="Multidimensional Scaling", type="n")
text(x, y, labels = row.names(matr_cr), cex=.6, col="red")
cr

Plotting multidimensional data is difficult and depending on the type of data and analysis is what to do. First of all, if you have several variables, it may be useful to cluster your data, one possible method is k-means that you can find it in the package "ClusterR". Another possible thing to do is to transform your variable by rotating the axis in order to lower the dimension with a Principal Component Analysis (PCA), you can find more about PCA in R in http://www.sthda.com/english/articles/31-principal-component-methods-in-r-practical-guide/
If you opp to plot your data as it is without a previous analysis, you may use ggplot2 package to make more useful and elegant plots. And to plot your different data attributes you can try changing size, color, shape, etc scales representing different dimensions. The problem with this option is that you can not plot several dimensions.
If I understand you well, you got pictures and people (informants) that make a review of the pictures. And the critics are separated in different levels (dimensions). If it is like that, you got as dimensions pictures, reviewers, and each level of the reviews, that make 2+N variables. Note that you can easily plot up to 5 dimensions in this kind of data, by setting x-axis and y-axis you got 2 dimensions, then you can use size scale for another dimension, color scale for another dimension, and the depending on your data and your preference you can use text or shape scale for the fifth dimension. I do not see in the table you provide the informants (reviewers) dimension. Further below you will found two examples of these plot using ggplot2, note that for shape scale a discrete variable must be used. In order to get beautiful plots and with meaning, you will have to try wich type of scale is better for each of your variables and will strongly depend on your data. Lastly, if you have several dimensions normally you should try first to assess if your data is clusterized or do a PCA.
library(ggplot2)
daten <- structure(list(photos = c("p1", "p5", "p8", "p13", "p19", "p23", "p29", "p34", "p36", "p40", "p59", "p2", "p14"), expression1 = c(18, 8, 11, 15, 14, 16, 10, 12, 15, 18, 18, 0, 0), expression2 = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0), expression3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1), expression4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 17), expression5 = c(0, 3, 5, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0), expression6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression8 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"))
# with shape scale
ggplot(data = daten,aes(x=photos, y=expression1, col=expression2, size=expression3, shape=as.factor(expression4))) +
geom_point()
# with text scale
ggplot(data = daten,aes(x=expression4, y=expression1, col=expression2, size=expression3, label=photos)) +
geom_text()

Related

Aggregate similar constructs/ FA with binary variables

I would like to aggregate, in order to reduce the number of constructs, its following data frame containing only binary variables that correspond to "yes/no", its following data frame (first 10 row). The original data frame contains 169 rows.
outcome <-
structure(list(Q9_Automazione.processi = c(0, 0, 0, 0, 0, 0,
1, 1, 1, 0), Q9_Velocita.Prod = c(1, 0, 0, 1, 0, 0, 1, 1, 1,
0), Q9_Flessibilita.Prod = c(0, 0, 0, 1, 0, 0, 1, 1, 0, 1), Q9_Controllo.processi = c(0,
0, 0, 1, 0, 0, 1, 1, 0, 0), Q9_Effic.Magazzino = c(0, 0, 0, 1,
0, 0, 0, 0, 0, 0), Q9_Riduz.Costi = c(0, 1, 0, 0, 0, 0, 0, 0,
0, 1), Q9_Miglior.Sicurezza = c(0, 0, 0, 0, 0, 0, 1, 0, 1, 1),
Q9_Connett.Interna = c(0, 0, 0, 0, 0, 0, 0, 1, 1, 0), Q9_Connett.Esterna = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), Q9_Virtualizzazione = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0), Q9_Innov.Prod = c(0, 0, 0, 0, 0,
1, 0, 0, 0, 1), Q9_Person.Prod = c(0, 1, 0, 1, 0, 1, 0, 0,
0, 1), Q9_Nuovi.Mercati = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Q9_Nuovi.BM = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q9_Perform.Energ = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), Q9_Perform.SostAmb = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame")
I have tried performing factor analysis via the tethracoric method on the obtained correlation matrix ( the obtained value from the KMO function turns out to be inadequate) both directly on the dataframe and then using tethracoric correletions in fafunction (using cor = "tet" I get a negative Tucker Lewis Index).
I have been reading up on this but cannot find a methodology that is adequate and of which I am certain of the correctness of the analysis.
So basically what I would like to achieve is to aggregate similar constructs, e.g., assess whether column 5 has value 1 (i.e., "yes") almost always when column 11 has value 1 and then aggregate.
Here the code that I try to used
library(psych)
tet <- tetrachoric(outcome)
corrplot(tet$rho, "ellipse", tl.cex = 0.75, tl.col = "black")
par(mfrow = c(1,2))
corr_matrix %>%
ggcorrplot(show.diag = F,
type="lower",
lab=TRUE,
lab_size=2)
KMO(corr_matrix)
cortest.bartlett(corr_matrix)
fa.parallel(corr_matrix, fm = "ml")
factor <- fa(corr_matrix, nfactors = 3, rotate = "oblimin", fm = "ml")
print(factor, cut = 0.3, digits = 3)
# -------- Pearson --------
cor(outcome, method = 'pearson', use = "pairwise.complete.obs") %>%
ggcorrplot(show.diag = F,
type="lower",
lab=TRUE,
lab_size=2)
KMO(outcome)
cortest.bartlett(outcome)
fa.parallel(outcome)
factor1 <- fa(outcome, nfactors = 3, rotate = "oblimin", cor = "tet", fm = "ml")
print(factor1, cut = 0.3, digits = 3)

How to add ellipse in bray nmds analysis in vegan package

I have plotted point graph using vegan package but I want to circle the similarly treated species. As shown in the figure, 3 colors for 3 treatments. I want to circle them too.
Here is my code.
library(vegan)
library(MASS)
library(readxl)
bray1 <- read_excel("bray1.xlsx")
cols <- c("red", "blue","blue", "green","green","red","blue","green","green","red","red","blue")
row.names(bray1) <- c("SI1", "SII0", "SI0", "SII2", "SI2", "SII1", "SIII0", "SIV2", "SIII2", "SIV1", "SIII1", "SIV0")
bcdist <- vegdist(bray1, "bray")
bcmds <- isoMDS(bcdist, k = 2)
plot(bcmds$points, type = "n", xlab = "", ylab = "")
text(bcmds$points, dimnames(bray1)[[1]],col = cols,size=10)
[My data
bray1<-structure(list(`Andropogon virginicus` = c(0, 0, 0, 0, 2.7, 31.5333333333333, 0, 0, 0, 0, 0, 0), `Oenothera parviflora` = c(61.6,30.3333333333333, 7.53333333333333, 0, 11.7333333333333, 0, 0, 0,75.4, 0, 0, 0), `Lespedeza cuneata` = c(0, 0, 0, 0, 0, 46.7333333333333, 0, 0, 3, 0, 0, 0), `Lespedeza pilosa` = c(0, 1.93333333333333, 0, 0, 1.73333333333333, 0, 0, 0, 0, 1.7, 0, 0), `Chamaesyce maculata` = c(0, 0, 0,4.733333333, 0, 0, 0, 0, 0, 0, 0, 0), `Chamaesyce nutans` = c(0,0, 0, 0, 0,0, 0.166666666666667, 0, 0, 0, 0, 0), `Bidens frondosa` = c(0, 0, 0,1.76666666666667, 1.03333333333333, 3.23333333333333, 0, 0, 0, 0, 0, 0), `Erigeron annuus` = c(0, 0, 0, 0, 0.4, 0, 0, 0, 0, 0, 0, 0), `Erigeron canadensis` = c(0, 0, 0, 0, 0, 4.33333333333333, 0, 0, 9.1, 2.066666667, 0,0), `Equisetum arvense` = c(46, 62.7333333333333, 0, 1.66666666666667, 0, 0.533333333333333, 0, 0, 0, 0, 0, 0), `Erigeron sumatrensis` = c(0, 0, 0, 0, 0, 16.4333333333333, 0, 4, 0, 6.633333333, 0, 0), `Hypochaeris radicata` = c(0, 3.76666666666667, 116.6, 0, 5.033333333, 9.76666666666667, 29, 0, 23.1666666666667, 82.16666667, 0, 0), `Lactuca indica` = c(10.26666667, 0, 1.566666667, 120.1333333, 44.36666667, 42.0333333333333, 0, 14.2333333333333, 0, 0, 14.36666667, 22.2), `Solidago altissima` = c(0, 1.06666666666667, 33.93333333, 0, 0, 0, 0, 0, 0, 6.6, 0, 0), `Sonchus asper` = c(0, 35.9, 0, 0, 0, 7.46666666666667,
29.6666666666667, 4.96666666666667, 0, 0, 0.23, 2.933333333 )), .Names = c("Andropogon virginicus", "Oenothera parviflora", "Lespedeza cuneata", "Lespedeza pilosa", "Chamaesyce maculata", "Chamaesyce nutans", "Bidens frondosa", "Erigeron annuus", "Erigeron canadensis", "Equisetum arvense", "Erigeron sumatrensis", "Hypochaeris radicata", "Lactuca indica", "Solidago altissima", "Sonchus asper"), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))
Here are a couple of alternatives based on the dataEllipse function in the car package. I have made a few minor alterations to your base graph. I found it hard to read the pure "green" color text, so I switched it to "darkgreen". I changed the plotting limits so that the full ellipses would be in the picture. Also, your text statement included an argument size. text does not have an argument size so I replaced it with cex to set the font size.
library(car)
Group = c(1,2,2,3,3,1,2,3,3,1,1,2)
cols <- c("red", "blue","blue", "darkgreen","darkgreen","red","blue",
"darkgreen","darkgreen","red","red","blue")
In the first version, I did what I think you asked for, ellipses marking the treatment groups.
plot(bcmds$points, type = "n", xlab = "", ylab = "",
xlim=c(-0.8,0.8), ylim=c(-0.8,0.8), asp=1)
text(bcmds$points, dimnames(bray1)[[1]],col = cols, cex=0.8)
dataEllipse(bcmds$points[,1], bcmds$points[,2], factor(Group),
plot.points=F, add=T, col=c("red", "blue", "green"),
levels=rep(0.6, 3), center.pch=0, group.labels="", lwd=1)
In the second version, instead of using the outline of the ellipse, I use a transparent fill color to show the ellipses.
plot(bcmds$points, type = "n", xlab = "", ylab = "",
xlim=c(-0.8,0.8), ylim=c(-0.8,0.8), asp=1)
text(bcmds$points, dimnames(bray1)[[1]],col = cols, cex=0.8)
dataEllipse(bcmds$points[,1], bcmds$points[,2], factor(Group),
plot.points=F, add=T, col=c("red", "blue", "green"),
levels=rep(0.6, 3), center.pch=0, group.labels="",
lty=0, fill=TRUE, fill.alpha=0.04)

What does this error mean "order(vertex_attr(g, measure), decreasing = TRUE) : argument 1 is not a vector" in R?

I am trying to calculate robustness, a graph theory measure using R (braingraph package).
Robustness = robustness(my_networkgraph, type = c("vertex"), measure = ("btwn.cent"))
I get the following error, when I use the above robustness function:
Error in order(vertex_attr(g, measure), decreasing = TRUE) : argument 1 is not a vector
Any idea, what I am doing wrong here?
My network, which is a matrix has been converted to igraph object and robustness was calculated.
My network as a matrix:
mynetwork <- matrix(c(0, 1, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0), nrow = 8)
This matrix was converted as igraph using the following code:
my_networkgraph <-graph_from_adjacency_matrix(mynetwork, mode = c("undirected"),weighted = NULL, diag = TRUE, add.colnames = NULL, add.rownames = NA)
Please help me to understand the above error
Thanks
Priya
There was a bug in the above function. To run the robustness code, you will need to supply a vertex attribute to your network: V(network)$degree <- degree(network) V(network)$btwn.cent <- centr_betw(network)$res

Side-by-side stacked barplot with facetting using ggplot2

The majority of information I can find on side-by-side stacked barplots deals with instances in where some variable (number of side-by-side bars) are repeated for each variable along the x-axis - see: 1, 2, 3, 4, 5, 6. In these cases they use ggplot with besides=TRUE.
I have a more complex example which I believe will require faceting like these two examples: 7 & 8.
Quick background (for those interested in the why?):
I'm trying to compare the efficiency of a proteomics protocol that enriches for chromatin by comparing to the proportion of nuclear proteins found in the core/whole proteome experiments for 4 cell lines. to do this I used The Human Protein Atlas to annotate proteins by their subcellular location and compare nuclear proteins from chromatin-enrichment to whole-enrichment. However, the chromatin-enrichment protocol was 1D-shotgun while the whole proteome data was 2D-shotgun with 50 fractions. In layman terms this means the whole/core proteome data is a more expensive experiment done at higher coverage. Therefore, it wouldn't make sense to look at absolute proportion though because the overall amount of found proteins would be higher in the whole proteome pull-downs (see figure: absolute protein comparison sketch). To circumvent this issue I divided by the total number of proteins found in each pull-down to get relative proportion of proteins from each subcellular location.
Using these relative proportions I've produced a stacked barplot of the following data in my gist with the following code:
df1 <- read.csv("data.csv") # Load data.frame of the data
df2 <- melt(df1, # Reshape the data from
id.vars = "subcellular_location", # wide format into long format
variable.name = "cell_line", # (i.e. tidy data)
value.name = "relative_proportion")
For some reason this didn't change the variable name or value name (headers) - they are called "variable" and "value" still? So I had to rename column headers via the following.
names(df2) <- c("subcellular_location", "cell_line", "relative_proportion")
As there are many subcellular locations I needed to custom add colors, furthermore I grouped them by similar locations (e.g. nuclear in blue).
p <- ggplot() +
geom_bar(aes(x = cell_line, y = percentage, fill = subcellular_location),
data = df2, stat="identity")
p +
coord_flip() +
scale_fill_manual(values = c("#bd5db0","#9ae17c", "#be0024", "#7388ff", "#c456b7",
"#8ed470", "#7ec361", "#7d7304", "#f87a00", "#d543c7",
"#bead47", "#d148c3", "#da8836", "#e28504", "#d93eca",
"#c720b9", "#bc07ae", "#a40098", "#9a008e", "#e8d448",
"#104ed7", "#2c4ecc", "#00428c", "#393c6d", "#173b8f",
"#3f4c96", "#9ba2f5", "#727bcc", "#e59c5f", "#790000",
"#045d00", "#f9ad6f"))
See image here: stacked barplot
The core proteome pull-downs are highlighted in yellow. Ideally what I would like to do is facet this barplot into 4 sections - one for each cell line. I followed the instructions from reference 7 for faceting but am getting an error.
First I split my dataframe into 4 separate tidy dataframes (e.g. below):
K562 <- read.csv("K562-relative.csv")
K562 <- melt(K562, id.vars = "subcellular_location") # Reshape the data into tidy form
names(K562) <- c("subcellular_location", "cell_line", "relative_proportion")
etc.
Than I created a vector for cell line:
cell <- sample(c("HAP1","K562","A673","MDS"))
When I try the following code I get an error:
ref_by_cell <- data.frame(HAP1 = HAP1, K562 = K562, A673 = A673, MDS = MDS, cell = cell)
Error in data.frame(HAP1 = HAP1, K562 = K562, A673 = A673, MDS = MDS,
arguments imply differing number of rows: 576, 544, 64, 4
I would appreciate any help with faceting or alternative ideas for displaying this information.
Thank you!
I'm not entirely sure what you want, but if you want to facet by the first part of each cell_line value...
# add faceting variable to df2
df2 <- df2 %>%
mutate(cell = stringi::stri_extract_first_regex(cell_line, "^[^\\.|_]+"))
# facet by cell, specifying free scales / space on the y-axis
ggplot(data = df2,
aes(x = cell_line, y = relative_proportion, fill = subcellular_location)) +
geom_bar(stat = "identity") +
coord_flip() +
facet_grid(cell~., scales = "free_y", space = "free_y") +
scale_fill_manual(values = c("#bd5db0","#9ae17c", "#be0024", "#7388ff", "#c456b7",
"#8ed470", "#7ec361", "#7d7304", "#f87a00", "#d543c7",
"#bead47", "#d148c3", "#da8836", "#e28504", "#d93eca",
"#c720b9", "#bc07ae", "#a40098", "#9a008e", "#e8d448",
"#104ed7", "#2c4ecc", "#00428c", "#393c6d", "#173b8f",
"#3f4c96", "#9ba2f5", "#727bcc", "#e59c5f", "#790000",
"#045d00", "#f9ad6f")) +
theme_bw() +
theme(strip.text.y = element_text(angle = 0))
Data (copied from your gist link; next time please use dput so that others can reproduce your example more easily):
> dput(df1)
structure(list(subcellular_location = c("actinFilaments", "aggresome",
"cellJunctions", "centrosome", "cytokineticBridge", "cytoplasmicBodies",
"cytosol", "endoplasmicReticulum", "endosome", "focalAdhesion",
"golgiApparatus", "intermediateFilaments", "lipidDroplets", "lysosomes",
"microtubuleEnds", "microtubuleOrganizingCenter", "microtubules",
"midbodyRing", "midbody", "mitochondria", "mitoticSpindle", "nuclearBodies",
"nuclearMembrane", "nuclearSpeckles", "nucleliFibrallar", "nucleoli",
"nucleoplasm", "nucleus", "peroxisomes", "plasmaMembrane", "rodsAndRings",
"vesicles"), HAP1_P5242 = c(0.009581882, 0.000338753, 0.011033682,
0.015824623, 0.003774681, 0.00232288, 0.227013163, 0.024535424,
0.001258227, 0.005807201, 0.04229578, 0.008710801, 0.0014518,
0.001064654, 0.00029036, 0.006484708, 0.013646922, 0.000483933,
0.001064654, 0.063637244, 0.00087108, 0.02303523, 0.013646922,
0.024535424, 0.013259775, 0.054587689, 0.195509098, 0.101480836,
0.00174216, 0.058072009, 0.000822687, 0.071815718), HAP1.wt_P8255.1 = c(0.0176,
0, 0.0032, 0.0096, 0, 0.0032, 0.3664, 0.0912, 0.008, 0.0032,
0.0128, 0, 0, 0.0064, 0, 0.0032, 0.0288, 0, 0, 0.0528, 0, 0.0128,
0.0048, 0.0096, 0, 0.0496, 0.1552, 0.0576, 0, 0.064, 0.0016,
0.0384), HAP1.wt_P8255.2 = c(0.013179572, 0, 0, 0.008237232,
0, 0.004942339, 0.36738056, 0.098846788, 0.003294893, 0.003294893,
0.016474465, 0.001647446, 0, 0.004942339, 0, 0.003294893, 0.029654036,
0, 0, 0.05107084, 0, 0.009884679, 0.004942339, 0.011532125, 0,
0.044481054, 0.154859967, 0.05601318, 0, 0.064250412, 0.001647446,
0.046128501), HAP1.wt_P8254.1 = c(0.012841091, 0, 0, 0.006420546,
0.001605136, 0.004815409, 0.362760835, 0.08988764, 0.001605136,
0.004815409, 0.017656501, 0.003210273, 0, 0.003210273, 0, 0.004815409,
0.032102729, 0, 0, 0.04975923, 0, 0.011235955, 0.003210273, 0.011235955,
0, 0.04975923, 0.160513644, 0.060995185, 0, 0.069020867, 0.001605136,
0.036918138), HAP1.wt_P8254.2 = c(0.015873016, 0, 0, 0.00952381,
0.001587302, 0.004761905, 0.357142857, 0.103174603, 0.003174603,
0.003174603, 0.014285714, 0.001587302, 0.001587302, 0.003174603,
0, 0.003174603, 0.03015873, 0, 0, 0.055555556, 0, 0.012698413,
0.006349206, 0.012698413, 0, 0.050793651, 0.152380952, 0.063492063,
0, 0.057142857, 0.001587302, 0.034920635), HAP1.kd_P8253.1 = c(0,
0, 0, 0, 0, 0, 0.270270271, 0.027027028, 0, 0, 0.027027028, 0,
0, 0, 0, 0, 0.054054053, 0, 0, 0, 0, 0, 0, 0.054054053, 0, 0.054054053,
0.405405405, 0.027027028, 0, 0, 0, 0.081081081), HAP1.kd_P8253.2 = c(0.021381579,
0, 0.003289474, 0.013157895, 0, 0.003289474, 0.368421053, 0.100328947,
0.004934211, 0.003289474, 0.013157895, 0.003289474, 0, 0.006578947,
0, 0.001644737, 0.027960526, 0, 0, 0.046052632, 0, 0.011513158,
0.004934211, 0.009868421, 0, 0.050986842, 0.15131579, 0.050986842,
0, 0.065789474, 0.001644737, 0.036184211), HAP1.kd_P8252.1 = c(0.018518518,
0, 0.00308642, 0.010802469, 0, 0.00462963, 0.354938272, 0.092592593,
0.00617284, 0.00462963, 0.018518518, 0, 0.00154321, 0.00462963,
0, 0.00617284, 0.026234568, 0, 0, 0.043209877, 0, 0.015432099,
0.00308642, 0.015432099, 0, 0.049382716, 0.154320988, 0.061728395,
0, 0.063271605, 0.00154321, 0.040123457), HAP1.kd_P8252.2 = c(0.012965964,
0, 0, 0.011345219, 0.001620746, 0.003241491, 0.367909238, 0.095623987,
0.003241491, 0.004862237, 0.017828201, 0.003241491, 0, 0.004862237,
0, 0.003241491, 0.030794165, 0, 0, 0.051863857, 0, 0.016207455,
0.003241491, 0.009724473, 0, 0.04376013, 0.1636953, 0.055105348,
0, 0.064829822, 0.001620746, 0.02917342), HAP1.kd_P8249.1 = c(0.010309278,
0.001718213, 0, 0.006872852, 0, 0.005154639, 0.197594502, 0.091065292,
0.001718213, 0, 0.013745704, 0.005154639, 0.001718213, 0.001718213,
0, 0, 0.027491409, 0, 0, 0.054982818, 0, 0.017182131, 0.013745704,
0.060137457, 0, 0.082474227, 0.240549828, 0.094501718, 0, 0.04467354,
0, 0.027491409), HAP1.kd_P8249.2 = c(0.010752688, 0, 0, 0.007168459,
0, 0.003584229, 0.20609319, 0.084229391, 0.001792115, 0, 0.007168459,
0.005376344, 0, 0.001792115, 0, 0, 0.03046595, 0, 0, 0.069892473,
0, 0.019713262, 0.014336918, 0.064516129, 0, 0.08781362, 0.224014337,
0.096774194, 0, 0.039426523, 0, 0.025089606), HAP1.kd_P8248.1 = c(0.007207207,
0, 0.001801802, 0.007207207, 0, 0.003603604, 0.198198198, 0.099099099,
0, 0, 0.009009009, 0.007207207, 0, 0, 0, 0.001801802, 0.025225225,
0, 0, 0.061261261, 0, 0.021621622, 0.016216216, 0.068468468,
0, 0.079279279, 0.234234234, 0.093693694, 0.001801802, 0.028828829,
0, 0.034234234), HAP1.kd_P8248.2 = c(0.005272408, 0.001757469,
0, 0.008787346, 0, 0.005272408, 0.202108963, 0.09314587, 0, 0,
0.014059754, 0.005272408, 0, 0, 0, 0.001757469, 0.029876977,
0, 0, 0.056239016, 0, 0.021089631, 0.014059754, 0.065026362,
0, 0.086115993, 0.228471002, 0.094903339, 0.001757469, 0.036906854,
0, 0.028119508), HAP1.wt_P8247.1 = c(0.016333938, 0, 0, 0.001814882,
0, 0.005444646, 0.197822141, 0.09800363, 0.001814882, 0, 0.007259528,
0.005444646, 0, 0.001814882, 0, 0.001814882, 0.030852995, 0,
0, 0.061705989, 0, 0.021778584, 0.012704174, 0.065335753, 0,
0.087114338, 0.234119782, 0.096188748, 0, 0.029038113, 0, 0.023593466
), HAP1.wt_P8247.2 = c(0.011173184, 0, 0, 0.003724395, 0, 0.003724395,
0.197392924, 0.098696462, 0.001862197, 0, 0.009310987, 0.005586592,
0, 0.001862197, 0, 0.001862197, 0.029795158, 0, 0, 0.059590317,
0, 0.018621974, 0.013035382, 0.067039106, 0, 0.08566108, 0.240223464,
0.096834264, 0, 0.029795158, 0, 0.024208566), HAP1.wt_P8246.1 = c(0.008880995,
0, 0, 0.005328597, 0.003552398, 0.003552398, 0.195381883, 0.090586146,
0, 0, 0.005328597, 0.008880995, 0, 0, 0, 0.001776199, 0.030195382,
0, 0, 0.051509769, 0, 0.023090586, 0.01598579, 0.063943162, 0,
0.097690941, 0.245115453, 0.097690941, 0, 0.026642984, 0, 0.024866785
), HAP1.wt_P8246.2 = c(0.009025271, 0, 0.001805054, 0.005415162,
0, 0.003610108, 0.19133574, 0.088447653, 0, 0.001805054, 0.012635379,
0.007220217, 0, 0, 0, 0, 0.028880866, 0, 0, 0.048736462, 0, 0.019855596,
0.027075812, 0.066787004, 0, 0.084837545, 0.241877256, 0.09566787,
0, 0.028880866, 0, 0.036101083), HAP1_P7964.1 = c(0.010040907,
0, 0.007437709, 0.017106731, 0.002975084, 0.003346969, 0.211230941,
0.040535515, 0.002603198, 0.005950167, 0.023056898, 0.00818148,
0.001115656, 0.002231313, 0.000743771, 0.005950167, 0.014503533,
0, 0.000743771, 0.065451841, 0.001115656, 0.023056898, 0.018966158,
0.031610264, 0, 0.065451841, 0.223875046, 0.105243585, 0.002603198,
0.051692079, 0.001487542, 0.051692079), MDS_P7246 = c(0.008080031,
0.000384763, 0.005386687, 0.012889573, 0.002885725, 0.002500962,
0.204116968, 0.035013467, 0.002116199, 0.00461716, 0.030973451,
0.008272412, 0.001539053, 0.001539053, 0.000192382, 0.003270489,
0.01250481, 0.000192382, 0.000961908, 0.082724125, 0.000577145,
0.025971528, 0.018661023, 0.030011543, 0.013851481, 0.065217391,
0.214313197, 0.108310889, 0.002116199, 0.048095421, 0.000577145,
0.052135437), MDS.L_P7246.1 = c(0.008308003, 0.000202634, 0.006079027,
0.013373858, 0.003039513, 0.002228976, 0.207294805, 0.036068891,
0.002026342, 0.004660587, 0.030800401, 0.008308003, 0.001621074,
0.001621074, 0.000202634, 0.003039513, 0.012563322, 0.000202634,
0.001013171, 0.081458956, 0.000405268, 0.026950351, 0.018034445,
0.030395133, 1.34e-07, 0.065450852, 0.218642321, 0.109017209,
0.002228976, 0.050050652, 0.000810537, 0.053900702), A673_P6591 = c(0.01081944,
0.000354736, 0.008158922, 0.013125222, 0.003015254, 0.003015254,
0.202554097, 0.035118836, 0.002128414, 0.006207875, 0.036183044,
0.006917347, 0.000886839, 0.001596311, 0.000709471, 0.004788932,
0.013657325, 0.000177368, 0.001064207, 0.069882937, 0.000709471,
0.025186236, 0.015253636, 0.029265697, 0.013125222, 0.06385243,
0.207875133, 0.106598084, 0.002305782, 0.056580348, 0.000354736,
0.058531394), A673_P6591.1 = c(0.011204482, 0.000186741, 0.008403361,
0.01363212, 0.003174603, 0.002614379, 0.203361345, 0.036414566,
0.002054155, 0.006162465, 0.036788049, 0.006722689, 0.000933707,
0.001680672, 0.000746965, 0.004668534, 0.01363212, 0.000186741,
0.001120448, 0.069467787, 0.000560224, 0.025957049, 0.014752568,
0.029505135, 0, 0.064239029, 0.212885154, 0.108496732, 0.002427638,
0.05751634, 0.000373483, 0.060130719), K562_P535 = c(0.008616975,
0.000143616, 0.007755278, 0.011202068, 0.002441476, 0.003303174,
0.278471923, 0.038776389, 0.00229786, 0.006031883, 0.033031739,
0.00689358, 0.003159558, 0.00229786, 0.000287233, 0.004164871,
0.012638231, 0.000287233, 0.000574465, 0.090621858, 0.000574465,
0.015941405, 0.009478673, 0.021255206, 0.009909522, 0.047536981,
0.181243717, 0.083871894, 0.002441476, 0.055292259, 0.00114893,
0.0583082), K562_P5494.1 = c(0.008692853, 0.000321957, 0.008692853,
0.012395364, 0.002736639, 0.002736639, 0.212813909, 0.032356729,
0.001448809, 0.004990341, 0.033000644, 0.007405023, 0.001448809,
0.001448809, 0.000160979, 0.004990341, 0.013039279, 0, 0.001126851,
0.074050225, 0.000643915, 0.027849324, 0.01545396, 0.029459111,
0, 0.065035415, 0.216355441, 0.111719253, 0.002092724, 0.051996137,
0.000804894, 0.054732775), K562_P5464.1 = c(0.009412153, 0.000495376,
0.008256275, 0.013705416, 0.002476882, 0.002476882, 0.20673712,
0.032529723, 0.001486129, 0.004788639, 0.034180978, 0.007595773,
0.001155878, 0.001321004, 0.000330251, 0.005284016, 0.012714663,
0.000330251, 0.000990753, 0.073811096, 0.000660502, 0.02823646,
0.016017173, 0.029722589, 0, 0.06489432, 0.217635403, 0.110634082,
0.002146631, 0.052179657, 0.000825627, 0.056968296), K562_P5359.1 = c(0.00740349,
0, 0.005288207, 0.005288207, 0.001057641, 0.003172924, 0.225806452,
0.063987308, 0.003172924, 0.004230566, 0.022739291, 0.005817028,
0.002644104, 0.002644104, 0, 0.003701745, 0.013749339, 0, 0.000528821,
0.099947118, 0, 0.015864622, 0.020095188, 0.037546272, 0, 0.080909572,
0.196192491, 0.090957166, 0.002115283, 0.040719196, 0.000528821,
0.043892121), K562_P5359.2 = c(0.007903056, 0, 0.004741834, 0.006322445,
0.001580611, 0.002107482, 0.223393045, 0.062170706, 0.002634352,
0.004741834, 0.023709168, 0.005795574, 0.002634352, 0.002634352,
0, 0.003688093, 0.014752371, 0, 0, 0.103266596, 0, 0.017386723,
0.021601686, 0.036354057, 0, 0.079030558, 0.192834563, 0.090621707,
0.002107482, 0.042676502, 0.00052687, 0.044783983), K562_P5358.1 = c(0.007462687,
0, 0.00533049, 0.005863539, 0.001599147, 0.003731343, 0.229744136,
0.064498934, 0.003198294, 0.004264392, 0.024520256, 0.005863539,
0.003198294, 0.002132196, 0, 0.003731343, 0.015458422, 0, 0,
0.101812367, 0, 0.015991471, 0.019189765, 0.036247335, 0, 0.077292111,
0.191364606, 0.087953092, 0.002132196, 0.041577825, 0.000533049,
0.045309168), K562_P5358.2 = c(0.006546645, 0, 0.005455537, 0.007637752,
0.001636661, 0.003273322, 0.225859247, 0.063829787, 0.003273322,
0.003818876, 0.024549918, 0.007092199, 0.002182215, 0.002727769,
0, 0.003818876, 0.015275505, 0, 0, 0.106382979, 0, 0.016912166,
0.01745772, 0.038188762, 0, 0.074195308, 0.195853792, 0.089470813,
0.001636661, 0.040370977, 0.000545554, 0.042007638), K562_P5357.1 = c(0.007057546,
0, 0.004885993, 0.00597177, 0.001085776, 0.003257329, 0.231813246,
0.06514658, 0.003257329, 0.004343105, 0.024972856, 0.00597177,
0.003257329, 0.002171553, 0, 0.003800217, 0.014115092, 0, 0,
0.118892508, 0, 0.01194354, 0.016829533, 0.030944625, 0, 0.07383279,
0.184039088, 0.086862106, 0.002171553, 0.049402823, 0.000542888,
0.043431053), K562_P5357.2 = c(0.008086253, 0, 0.003773585, 0.006469003,
0.001617251, 0.003234501, 0.23180593, 0.063072776, 0.003773585,
0.003234501, 0.023180593, 0.005929919, 0.003234501, 0.002695418,
0, 0.003773585, 0.014555256, 0, 0, 0.116442049, 0, 0.01509434,
0.017789757, 0.035579515, 0, 0.071698113, 0.189757412, 0.085714286,
0.002156334, 0.044743935, 0.000539084, 0.042048518), K562_P5356.1 = c(0.006292906,
0, 0.005148741, 0.004576659, 0.001716247, 0.002860412, 0.215675057,
0.070366133, 0.003432494, 0.003432494, 0.025743707, 0.005720824,
0.003432494, 0.002860412, 0, 0.004576659, 0.016018307, 0, 0,
0.127002288, 0, 0.01201373, 0.016590389, 0.03375286, 0, 0.076659039,
0.183638444, 0.086956522, 0.001716247, 0.044622426, 0.000572082,
0.044622426), K562_P5356.2 = c(0.00755814, 0, 0.004069767, 0.004651163,
0.001744186, 0.002325581, 0.21627907, 0.070930233, 0.002906977,
0.004069767, 0.025, 0.005813953, 0.003488372, 0.002906977, 0,
0.004069767, 0.015697674, 0, 0, 0.125581395, 0, 0.013953488,
0.015697674, 0.035465116, 0, 0.073837209, 0.190697674, 0.08372093,
0.001744186, 0.045348837, 0.000581395, 0.041860465), K562_P5355.1 = c(0.009320175,
0, 0.003289474, 0.007675439, 0.001096491, 0.002741228, 0.285087719,
0.059210526, 0.003837719, 0.006030702, 0.023026316, 0.003837719,
0.003837719, 0.003289474, 0, 0.003289474, 0.016995614, 0.000548246,
0, 0.094298246, 0, 0.014254386, 0.012061404, 0.026315789, 0,
0.052631579, 0.175438596, 0.08497807, 0.001096491, 0.057017544,
0.000548246, 0.048245614), K562_P5355.2 = c(0.008210181, 0, 0.004378763,
0.009304871, 0.001094691, 0.002189382, 0.280788177, 0.056376574,
0.003284072, 0.005473454, 0.024630542, 0.004378763, 0.003284072,
0.003284072, 0, 0.004378763, 0.016967707, 0, 0, 0.100164204,
0, 0.014778325, 0.012588944, 0.028461959, 0, 0.053639847, 0.172961138,
0.084838533, 0.001642036, 0.054187192, 0.000547345, 0.048166393
), K562_P5269.1 = c(0.007308161, 0, 0.003045067, 0.007917174,
0.001218027, 0.00365408, 0.228989038, 0.071863581, 0.00365408,
0.004263094, 0.017661389, 0.007308161, 0.002436054, 0.003045067,
0, 0.002436054, 0.017052375, 0, 0, 0.107186358, 0, 0.015834348,
0.020097442, 0.033495737, 0, 0.085261876, 0.18453106, 0.091961023,
0.002436054, 0.040194884, 0.001218027, 0.03593179), K562_P5269.2 = c(0.006234414,
0, 0.00436409, 0.006234414, 0.002493766, 0.002493766, 0.224438903,
0.073566085, 0.003117207, 0.002493766, 0.018703242, 0.006857855,
0.002493766, 0.003117207, 0, 0.001246883, 0.015586035, 0, 0,
0.109725686, 0, 0.015586035, 0.018703242, 0.034289277, 0, 0.082294264,
0.195760598, 0.092892768, 0.003117207, 0.039276808, 0.000623441,
0.034289277), K562_P5268.1 = c(0.004635762, 0, 0.00397351, 0.007284768,
0.001986755, 0.002649007, 0.214569536, 0.071523179, 0.00397351,
0.002649007, 0.01986755, 0.007284768, 0.003311258, 0.003311258,
0, 0.003311258, 0.016556291, 0, 0, 0.104635762, 0, 0.017880795,
0.018543046, 0.039735099, 0, 0.090066225, 0.195364238, 0.091390728,
0.001986755, 0.039735099, 0.000662252, 0.033112583), K562_P5268.2 = c(0.005242464,
0, 0.002621232, 0.00655308, 0.002621232, 0.00327654, 0.216251638,
0.070117955, 0.00327654, 0.00327654, 0.020969856, 0.008519004,
0.002621232, 0.001965924, 0, 0.002621232, 0.018348624, 0, 0,
0.108781127, 0, 0.015727392, 0.020314548, 0.040629096, 0, 0.087811271,
0.190039319, 0.090432503, 0.001965924, 0.040629096, 0.000655308,
0.034731324)), .Names = c("subcellular_location", "HAP1_P5242",
"HAP1.wt_P8255.1", "HAP1.wt_P8255.2", "HAP1.wt_P8254.1", "HAP1.wt_P8254.2",
"HAP1.kd_P8253.1", "HAP1.kd_P8253.2", "HAP1.kd_P8252.1", "HAP1.kd_P8252.2",
"HAP1.kd_P8249.1", "HAP1.kd_P8249.2", "HAP1.kd_P8248.1", "HAP1.kd_P8248.2",
"HAP1.wt_P8247.1", "HAP1.wt_P8247.2", "HAP1.wt_P8246.1", "HAP1.wt_P8246.2",
"HAP1_P7964.1", "MDS_P7246", "MDS.L_P7246.1", "A673_P6591", "A673_P6591.1",
"K562_P535", "K562_P5494.1", "K562_P5464.1", "K562_P5359.1",
"K562_P5359.2", "K562_P5358.1", "K562_P5358.2", "K562_P5357.1",
"K562_P5357.2", "K562_P5356.1", "K562_P5356.2", "K562_P5355.1",
"K562_P5355.2", "K562_P5269.1", "K562_P5269.2", "K562_P5268.1",
"K562_P5268.2"), class = "data.frame", row.names = c(NA, -32L
))

3D plot in R using persp3D - Axis issues

I have a matrix (m) and I'm trying to plot a 3D representation of that matrix.
> dput(head(m))
structure(c(21930, 21844, 21758, 21672, 21586, 21500, 22016,
21930, 21844, 21758, 21672, 21586, 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,
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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(6L, 26L), .Dimnames = list(
NULL, c("freq.min", "freq.max", "X0", "X1", "X2", "X3", "X4",
"X5", "X6", "X7", "X8", "X9", "X10", "X11", "X12", "X13",
"X14", "X15", "X16", "X17", "X18", "X19", "X20", "X21", "X22",
"X23")))
I managed to plot a 3D surface plot, but both the axes and the axis labels are incorrect. Note that the 3D surface plot below uses the entire matrix rather than just the header, which I only include here as the dput for brevity.
persp3D(z = m[,3:26], col = "lightgrey", shade = 0.5, ticktype = "detailed", axes=T)
Let's start with the axes themselves: the axis that goes from X0 to X23 should be the X axis of the matrix (column names), but here it is regarded as the Y axis. The Y axis, regarded here as the X axis, ranges from 0 to 22016 in 256 intervals of 86.
I've spent the last many hours scouring the internet for answers on how to change the axis labels, but have not succeeded. From what I understand, if I turn off the axis argument in persp3D (axes=F) I can then customize the axis in a subsequent line, like so:
axis3d(edge= 'y+-', at =seq(0,23,by=1) ,
labels = seq(0,23,by=1))
However, a RGL device pops up, and only the axis is plotted without the actual plot itself, which stays unchanged in the built-in R graphics device.
How do I successfully change the axes?
Does this what you're looking for?
clab <- 0:23
rlab <- seq(0, 21586, 86)
cnum <- length(clab)
rnum <- length(rlab)
m <- matrix(
c(runif(0.5*cnum*rnum)-1, runif(0.5*cnum*rnum)+1),
rnum, cnum,
dimnames = list(rlab, clab))
library(rgl)
plot3d(
clab, rlab, t(m),
type="n",
aspect = c(100, 200, 20),
xlab = "x", ylab = "y", zlab = "z",
sub = "Grab me and rotate me!"
)
surface3d(
clab, rlab, t(m),
color = c("black", "white"),
alpha = 0.5,
add = TRUE
)
To change the axis you can interchange x and y and transpose z with t().
As a side note: I wrote two functions to transform 3D point cloud data from tall to wide format and vice versa: recexcavAAR::spatialwide and recexcavAAR::spatiallong. I find them quite useful to go back and forth between plotting and analysis. Maybe they're useful for you.
Edit:
Alternative solution with single call to persp3d
clab <- 0:23
rlab <- seq(0, 21586, 86)
cnum <- length(clab)
rnum <- length(rlab)
m <- matrix(
c(runif(0.5*cnum*rnum)-1, runif(0.5*cnum*rnum)+1),
rnum, cnum,
dimnames = list(rlab, clab))
library(rgl)
persp3d(
clab, rlab, t(m),
color = c("black", "white"),
alpha = 0.5,
aspect = c(100, 200, 20),
xlab = "x", ylab = "y", zlab = "z",
sub = "Grab me and rotate me!"
)

Resources