R confusion heatmap - r

I have created my first confusion heatmap using the code I found here.
As I result I got a very nice plot with an "increasing" diagonal showing that the predicted and actual data are closely related.
Now, when I look up other confusion matrices, all of them show a "decreasing" diagonal and I'm wondering whether I should adapt my plot in that way (and if so: how?).
Any ideas on that?

By default, R displays a heatmap with the row names ordered from bottom to top, rather than top to bottom.
Here's how to change the ordering

Related

order dotplot x-axis along diagonal line

I produced this dotplot using Seurat, as shown in the picture
but I would to order the plot to show the dots starting from bottom left to top right.
I could do this manually with
my_levels <- c(names, names, names,..........) Idents(obj_name) <- factor(Idents(obj_name), levels= my_levels)
but I would to find a faster way to do it.
thanks
I am guessing this answer is a little late. But If I understood the question correctly you could just order the list of genes differently to obtain the desired outcome.

Scatterplot looks strange

my scatterplot that shows the relationship between my principal component and one of my questionnaire items looks strange.
The scatterplot between two of my principal components looks great (see second image).
The item is measured on a Likert scale from 1 to 5 (1 = strongly disagree, 5 = strongly agree). The histogram of this item also has many gaps between the bars, which is strange to me too.
Please let me know which specific output you still need to figure this out.
Thanks
That is because the principal components are both metric and the Likert scale is ordinal, so the values can only be integers from 1-5. In this regard the Scatterplot does not look strange but it might be the wrong type of plot. If you anyway want to use a scatterplot you can use jitter(var, 0.3) on the ordinal variable when creating the plot. 0.3 is just a suggestion, it's best if you just try what works. As you didn't provide a reproducible example I wasn't able to try it out.
For reproducibility, it is best if you provide a piece of the database you are using to make these scatterplots. It would also be great if you include the axes titles inside the graph.
To be honest, I dont see anything wrong with the first figure. Variable y (the questionare item) is a discrete variable between 1 and 5. It make sense that one result from the PC1 is match with more than one of the values of the item questionare.

Graphing multiple variables in R

I am currently attempting to graph multiple columns in a matrix in R. So far, I have figured things out, but here is my problem- when I submit a matrix with 5 columns, I only get a graph with 4 lines. I've noticed that the missing line is always the line closest to the x-axis. I've been working on this for several hours now, and I have tried several different things. Any advice or help on how to get R to produce that 5th line (with a corresponding color filling the space between the x-axis and the line) would be greatly appreciated.
gender=cbind(matrix(malepop),matrix(femalepop))
plotmat(year,gender)
#a sample set
biggen=cbind(malepop,femalepop,malepop,femalepop)
#start of the function
plotmat2=function(years,m,colors){
n=m/1000000
#create a plot with the base line
plot(years,n[,1],type='l',ylim=c(0,10))
##create a for loop to generate all other lines and fill in the spaces
for (i in ncol(n):2) {
newpop=matrix(rowSums(n[,1:i]))
lines(year,newpop)
cord.xmat=c(min(years),years,max(years))
cord.ymat=c(-1,newpop[,1],-1)
polygon(cord.xmat,cord.ymat,col=clrs[i])
next
cord.xmat=c(min(years),years,max(years))
cord.ymat1=c(-1,n[,1]/1000000,-1)
polygon(cord.xmat,cord.ymat,col="purple")
}
}
#sample color set
clrs=c("red","blue","yellow","pink","purple", "cyan", "hotpink")
#run the function
plotmat2(year,biggen,clrs)
Thanks for any and all help you can provide!
It might be that you are unintentionally covering up your first line with the other colored sections, and that you may be skipping the creation of the polygon for n[,1].
From the way you tried to graph the columns in descending order, I am assuming you know that your columns are in ascending size order (the section that is pink in your example plot would be the final column in the matrix "biggen"). In case I am wrong about this, it might be a good idea to change your polygon shading using the density argument, which may help you see if you are covering up other sections by accident.
## plotmat2 function
plotmat2=function(years,m,colors){
n=m/1000000
#create a blank plot based on the baseline
plot(years,n[,1],type='n',ylim=c(0,10))
##create a for loop to generate all other lines and fill in the spaces
for (i in ncol(n):1) {
newpop=matrix(rowSums(n[,1:i]))
lines(year,newpop)
cord.xmat=c(min(years),years,max(years))
cord.ymat=c(-1,newpop[,1],-1)
polygon(cord.xmat,cord.ymat,col=colors[i], density=10)
}
}
P.S. If this doesn't help fix the problem, it might help if you provided a portion of your dataset. I am still learning about R and about StackOverflow, but that seems to be sensible advice that is given on a lot of the threads I have read on here. Good luck!

cluster: :clusplot axis in wrong direction

I'm trying to plot the cluster obtained from fuzzy c-means clustering.
The plot should look like this.
code for the plot
plot(data$Longitude, data$Latitude, main="Fuzzy C-Means",col=data$Revised, pch=16, cex=.6,
xlab="Longitude",ylab="Latitude")
library(maps)
map("state", add=T)
However, when I tried to use clusplot the plot is displaying in opposite direction(both top and bottom and left and right) as below.
I wanna know if there's a way to reverse the plot to show in the order as the above picture.
Also, for the very dense area, it's hard to find the ellipse label. I wanna know if there's a way to show the label inside the ellipse instead of outside.
code for 2nd pic
library(cluster)
clusplot(cbind(Geocode$Longitude, Geocode$Latitude), cluster, color=TRUE,shade=TRUE,
labels=4, lines=0,col.p=cluster,
xlab="Longitude",ylab="Latitude",cex=1)
clusplot is a function that performs a lot of magic for you. In particular it projects the data set - which happens in a way you don't like, unfortunately. (Also note the scales - it centered and scaled the data, too)
clusplot.default: Creates a bivariate plot visualizing a partition (clustering) of the data. All observation are represented by points in the plot, using principal components or multidimensional scaling.
As far as I can tell, clusplot doesn't have map support, but you will want such a map I guess...
While maybe you can use the s.x.2d parameter to specify the exact projection (and this way disable automatic scaling), it probably is still difficult to add the map. Maybe look at the source of clusplot instead, and take only the parts you want?

plot igraph in a big area

Just wondering if it is possible to increase the size of the plot so that the nodes and edges can be more scattered over the plot.
Original plot:
What are expected:
I tried many parameters in the layout function such as area, niter, and so on, but all of them do not work. By the way, I am using 'igraph' package in R.
If you are referring to the actual size of the produced output (pdf, png, etc), you can configure it with the width and height parameters. Check this link for png,bpm, etc, and this link for PDF format.
A MWE is something like this:
png("mygraph.png", heigh=400, width=600)
#functions to plot your graph
dev.off()
If you are referring to the size of the graphic produced by the layout function, as #MrFlick referred, you should check the parameters of the particular layout you are using.
Hope it helps you.
In your second graph, it's obviously the graph can be divided into several clusters (or sections). If I understood you correctly, you want to have a layout that separates your clusters more visibly.
Then you can draw this by calculating a two-level layout:
First, calculate the layout of the graph in order to find a place for each cluster.
Second, calculate the layout in each cluster according to first step and plot nodes in the corresponding place.

Resources