R Euler Plot Basic Questions - r

My question is about the inputs into library(eulerr) in r. I am trying to plot something I think would be simple but I am having trouble and it stems from my lack of knowledge of r and this library.
For this code :
fit1 <- euler(c("A" = 25, "B" = 5, "A&B" = 5))
plot(fit1)
I would think it would produce two circles with the following characteristics:
Circle A would be large
Circle B would be small
Circle B would be completely inside of A since they "share" all 5
However, circle B is not entirely inside of A and I am not sure why. More to the point I would like to get my intended output which would be a figure that satisfied the three bullet points above. Please help. Thanks.

To get more sense of the eulerr parameters you can check this shiny app. For the particular diagram you want to make you need to do:
fit1 <- euler(c("A" = 25, "B" = 0, "A&B" = 5))
plot(fit1)

Related

Align plotly plots in R-Markdown

Can someone tell me if there is a way to align plotly plots in R-Markdown?. More specifically: Currently my plots are being placed one after another. I would like to have a grid-like format. I achieved this before, using the simple plot function, but that doesn't seem to work with plotly.
The following is an example of my prior code, that worked with the simple plot function. Can I make that work, assuming I were using plotly?
Thanks in advance.
{r comment = NA,fig.width=14, fig.height=14}
layout(matrix(c(1,2,3,4,5,6,7,8), 4, 2, byrow = TRUE))
m_alt <- miete[miete$baujahr <= 22,]
m_neu <- miete[miete$baujahr > 22,]
plot(table(m_alt$bezirk),main="",ylab="Frequency",xlab="Bezirk")
plot(table(m_neu$bezirk),main="",ylab="Frequency",xlab="Bezirk")
plot(table(m_alt$wohnflaeche),main="",ylab="Frequency",xlab="Wohnfläche")
plot(table(m_neu$wohnflaeche),main="",ylab="Frequency",xlab="Wohnfläche")
plot(table(m_alt$wohnlage,m_alt$zimmerzahl),main="Altbau: Wohnlage u. Zimmerzahl")
plot(table(m_neu$wohnlage,m_neu$zimmerzahl),main="Neubau: Wohnlage u. Zimmerzahl")
plot(table(miete$bezirk,miete$zimmerzahl),main="Bezirk u. Zimmerzahl")
plot(table(miete$warmwasser,miete$bezirk),main="Warmwasser u. Bezirk")

Lines don't align in rastertoPolygons

I am having trouble with aligning grids on a plot I made. Basically the plots show the result of a 34x34 matrix where each point has a value of 0,1,2,3 and is colored based on this. The lines which outline the cells do not match up perfectly with the coloring of the cells. My code and image are below.
library(raster)
r<-raster(xmn=1,xmx=34,ymn=1,ymx=34,nrows=34,ncols=34)
data1<-read.csv(file ="mat_aligned.csv",row.names = 1)
numbers<-data.matrix(data1)
r[]<-numbers
breakpoints<-c(-1,0.1,1.1,2.1,3.1)
colors<-c("white","blue","green","red")
plot(r,breaks=breakpoints,col=colors)
plot(rasterToPolygons(r),add=TRUE,border='black',lwd=3)
I would appreciate any help with this!
The problem is that the base R plot and the drawing of the grid use different plotting systems. The polygons will stay constant relative to the plotting window (they will appear narrower as the window shrinks), and won't preserve their relationship to the underlying plot axes, whereas the coloured squares will resize to preserve shape. You'll probably find that you can get your grid to match better by resizing your window, but of course, this isn't ideal.
The best way to get round this is to use the specific method designed for plotting SpatialPolygonDataFrame, which is the S4 class produced by rasterToPolygons. This is, after all, how you're "meant" to create such a plot.
Here's a reprex (obviously I've had to make some random data as yours wasn't shared in the question) :
library(raster)
r <- raster(xmn = 1, xmx = 34, ymn = 1, ymx = 34, nrows = 34, ncols = 34)
r[] <- data.matrix(as.data.frame(replicate(34, sample(0:3, 34, TRUE))))
colors <- c("white","blue","green","red")
spplot(rasterToPolygons(r), at = 0:4 - 0.5, col.regions = colors)
Created on 2020-05-04 by the reprex package (v0.3.0)
It is difficult to help if you not provide a minimal self-contained reporducible example. Something like this
library(raster)
r <- raster(xmn=1,xmx=34,ymn=1,ymx=34,nrows=34,ncols=34)
values(r) <- sample(4, ncell(r), replace=T)
p <- rasterToPolygons(r)
plot(r)
lines(p)
I see what you describe, even though it is minimal. A work-around could be to only plot the polygons
colors<-c("white","blue","green","red")
plot(p, col=colors[p$layer])

Adding a Sample Space to a Venn Diagram in R

I am trying to insert of a simple Venn diagram in an jupyter notebook on R. I have been able to generate a simple 2-set diagram using the VennDiagram library. However I can't seem to figure out how to work the triple diagram in way that yields 2 intersecting sets that are in a subset of another larger set.
Maybe I'm using the wrong library?
Edit:
This is for an illustration, I just need to draw an example of a Venn Diagram. The data would be something like:
S=(1,2,3)
A=(1,2)
B=(2,3)
The latest development version of my r package eulerr is now able to take a list of sample spaces as input. It, however, produces euler diagrams (proportional Venn diagrams) (which is why your specifications won't result in two diagrams intersecting within another).
# devtoools::install_github("jolars/eulerr")
library(eulerr)
ll <- list(S = c(1, 2, 3), A = c(1, 2), B = c(2, 3))
fit <- euler(ll)
plot(fit)
If you want two intersecting circles within a third, try the following:
plot(euler(c(S = 5, "A&B&S" = 3, "A&S" = 1, "B&S" = 1)))

Trying to determine why my heatmap made using heatmap.2 and using breaks in R is not symmetrical

I am trying to cluster a protein dna interaction dataset, and draw a heatmap using heatmap.2 from the R package gplots. My matrix is symmetrical.
Here is a copy of the data-set I am using after it is run through pearson:DataSet
Here is the complete process that I am following to generate these graphs: Generate a distance matrix using some correlation in my case pearson, then take that matrix and pass it to R and run the following code on it:
library(RColorBrewer);
library(gplots);
library(MASS);
args <- commandArgs(TRUE);
matrix_a <- read.table(args[1], sep='\t', header=T, row.names=1);
mtscaled <- as.matrix(scale(matrix_a))
# location <- args[2];
# setwd(args[2]);
pdf("result.pdf", pointsize = 15, width = 18, height = 18)
mycol <- c("blue","white","red")
my.breaks <- c(seq(-5, -.6, length.out=6),seq(-.5999999, .1, length.out=4),seq(.100009,5, length.out=7))
#colors <- colorpanel(75,"midnightblue","mediumseagreen","yellow")
result <- heatmap.2(mtscaled, Rowv=T, scale='none', dendrogram="row", symm = T, col=bluered(16), breaks=my.breaks)
dev.off()
The issue I am having is once I use breaks to help me control the color separation the heatmap no longer looks symmetrical.
Here is the heatmap before I use breaks, as you can see the heatmap looks symmetrical:
Here is the heatmap when breaks are used:
I have played with the cutoff's for the sequences to make sure for instance one sequence does not end exactly where the other begins, but I am not able to solve this problem. I would like to use the breaks to help bring out the clusters more.
Here is an example of what it should look like, this image was made using cluster maker:
I don't expect it to look identical to that, but I would like it if my heatmap is more symmetrical and I had better definition in terms of the clusters. The image was created using the same data.
After some investigating I noticed was that after running my matrix through heatmap, or heatmap.2 the values were changing, for example the interaction taken from the provided data set of
Pacdh-2
and
pegg-2
gave a value of 0.0250313 before the matrix was sent to heatmap.
After that I looked at the matrix values using result$carpet and the values were then
-0.224333135
-1.09805379
for the two interactions
So then I decided to reorder the original matrix based on the dendrogram from the clustered matrix so that I was sure that the values would be the same. I used the following stack overflow question for help:
Order of rows in heatmap?
Here is the code used for that:
rowInd <- rev(order.dendrogram(result$rowDendrogram))
colInd <- rowInd
data_ordered <- matrix_a[rowInd, colInd]
I then used another program "matrix2png" to draw the heatmap:
I still have to play around with the colors but at least now the heatmap is symmetrical and clustered.
Looking into it even more the issue seems to be that I was running scale(matrix_a) when I change my code to just be mtscaled <- as.matrix(matrix_a) the result now looks symmetrical.
I'm certainly not the person to attempt reproducing and testing this from that strange data object without code that would read it properly, but here's an idea:
..., col=bluered(20)[4:20], ...
Here's another though which should return the full rand of red which tha above strategy would not:
shift.BR<- colorRamp(c("blue","white", "red"), bias=0.5 )((1:16)/16)
heatmap.2( ...., col=rgb(shift.BR, maxColorValue=255), .... )
Or you can use this vector:
> rgb(shift.BR, maxColorValue=255)
[1] "#1616FF" "#2D2DFF" "#4343FF" "#5A5AFF" "#7070FF" "#8787FF" "#9D9DFF" "#B4B4FF" "#CACAFF" "#E1E1FF" "#F7F7FF"
[12] "#FFD9D9" "#FFA3A3" "#FF6C6C" "#FF3636" "#FF0000"
There was a somewhat similar question (also today) that was asking for a blue to red solution for a set of values from -1 to 3 with white at the center. This it the code and output for that question:
test <- seq(-1,3, len=20)
shift.BR <- colorRamp(c("blue","white", "red"), bias=2)((1:20)/20)
tpal <- rgb(shift.BR, maxColorValue=255)
barplot(test,col = tpal)
(But that would seem to be the wrong direction for the bias in your situation.)

Venn diagram proportional and color shading with semi-transparency

I have following type of count data.
A 450
B 1800
A and B both 230
I want to develop a colorful (possibly semi-transparency at intersections) like the following Venn diagram.
Note: This figure is an example hand drawn in PowerPoint, and it is not to scale.
Here is a post which discusses Venn diagram from list of clusters and co-occurring factors.
For easy solution use package venneuler:
require(venneuler)
v <- venneuler(c(A=450, B=1800, "A&B"=230))
plot(v)
For more advanced and customized solutions check package VennDiagram.
library(VennDiagram)
venn.diagram(list(B = 1:1800, A = 1571:2020), fill = c("lightblue", "green"),
alpha = c(0.5, 0.5), lwd =0, "venn_diagram.tiff")
I have recently published a new R package, eulerr, which does what you want. It is quite similar to venneuler but without its inconsistencies.
library(eulerr)
fit <- euler(c(A = 450, B = 1800, "A&B" = 230))
plot(fit)
Or you could try the shiny application for the same r package at eulerr.co
Based on second answer by Geek On Acid second suggestion ( thanks once again ) I would able sove the line problem as well. I am posting if this is relevent to other googlers !
require(VennDiagram)
venn.diagram(list(B = 1:1800, A = 1571:2020),fill = c("red", "green"),
alpha = c(0.5, 0.5), cex = 2,cat.fontface = 4,lty =2, fontfamily =3,
filename = "trial2.emf");
Even though this doesnt answer your question completely. I thought that this will be useful for other people looking to plot Venn Diagram.
One can use the venn() function from the gplots package:
http://www.inside-r.org/packages/cran/gplots/docs/venn
## modified slightly from the example given in the documentation
## Example using a list of item names belonging to the
## specified group.
##
require(gplots)
## construct some fake gene names..
oneName <- function() paste(sample(LETTERS,5,replace=TRUE),collapse="")
geneNames <- replicate(1000, oneName())
##
GroupA <- sample(geneNames, 400, replace=FALSE)
GroupB <- sample(geneNames, 750, replace=FALSE)
GroupC <- sample(geneNames, 250, replace=FALSE)
GroupD <- sample(geneNames, 300, replace=FALSE)
venn(list(GrpA=GroupA,GrpB=GroupB,GrpC=GroupC,GrpD=GroupD))
Afterwards I just add colours and transparency using illustrator.
There is an intuitive and flexible proportional plotter that you can download and run. Find it at:
http://omics.pnl.gov/software/VennDiagramPlotter.php
and
jvenn: an interactive Venn diagram viewer - GenoToul Bioinfo: http://bioinfo.genotoul.fr/jvenn/
I know that the OP asks about a solution in R but I would like to point to a web-based solution called BioVenn. It takes up to 3 lists of elements and draws a Venn diagram so that each surface is proportional to the number of elements - like this one:
In this diagram I have changed manually (via PhotoShop) the placement of the numbers as I did not like the locations chosen by BioVenn. But you can chose not to have numbers.
In theory the lists used with BioVenn shall consist of gene IDs but, in practice, it doesn't matter - the lists simply have to contain strings.
FWIW: found this package for python that does the same thing.

Resources