I have a heatmap that looks like this. I want to show correlation values that are only +/- 0.25.
How to do this? Thank you.
I believe you want to do this.
con_corr = df_continuous.corr()
mask_con_corr = con_corr[(con_corr >= 0.25) | (con_corr <= -0.25)]
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(15,15))
fig = sns.heatmap(mask_con_corr, vmin=-1, vmax=1, annot=True, fmt='0.2f')
fig.set_title("Heatmap of Correlation (Weaker Correlation)")
Related
Firstly apologies, if I do not explain this very well, I'm relatively new to the cut function and cannot find a suitable answer to my question.
I have X Y coordinate data, and I know how to create evenly distributed bins for use with stat_bin_2d. I would do something like this:
heatmap$xbin <- cut(heatmap$x, breaks = seq(from=0, to=100, by = 20),include.lowest=TRUE )
heatmap$ybin <- cut(heatmap$y, breaks = seq(from=0, to=100, by = 20),include.lowest=TRUE)
Using ggplot I'd include this in a plot like so:
stat_bin_2d(data = heatmap, aes(x=x, y=y), binwidth = c(20,20))+
However if I want to create custom sized bins of different sizes, I'm not entirely sure how I do this. For example, if I wanted to plot specific zones of interest on a sports pitch, how do I approach the cut function as its not an even distribution?
I tried this, but again I don't believe this is correct:
heatmap$xbin <- cut(heatmap$x, breaks = seq(from=80, to=100, by = 1),include.lowest=TRUE )
heatmap$ybin <- cut(heatmap$y, breaks = seq(from=40, to=60, by = 1),include.lowest=TRUE)
Effectively I'd like bins like this, once I know how to customize the bins sizes:
I am trying to perform sliding correlation with window=11years.
Here's my code:
library(gtools)
var1<-rnorm(52,0.010,0.05)
var2<-rnorm(52,0.015,0.01)
dat<-merge(var1,var2)
dat$year<-seq(1961,2012,1)
rc<-running(dat$x,dat$y,fun=cor, width=11)
I want to plot the output as a simple line plot like this:
plot(rc,type="l")
My problem is the x-axis. How do I match the correlation value with the years? Something like adding a filler value "0"?
Any suggestions on how I can do this correctly in R.
I'll appreciate any help.
Many thanks in advance.
rc <- running(var1, var2, fun = cor, width = 11)
plot(1971:2012, rc, type="l")
I am creating a plot in R and I would like to have points and ellipse of a higher resolution. Could you maybe help me how to do that?
The code I am using is:
autoplot(
pcaAroma,
data = keepRows,
colour = 'Woi',
frame.type = 'norm',
size = 3
)
Desired OutputI want to create a heatmap in R which should consider formatting of colors by all the values in matrix and not just by rows. Ex: I've a following matix
row1 : 1,2,3,4
row2 : 50,50,50,50
When I use heatmap() to plot this matrix, I see row 1 with shades of color, however my row2 is marked as white. I would rather want 50 to be rated in extreme color (eg: Red) and 1,2, 3 & 4 in lighter shades
Hope my question makes sense. Please let me know if anything is unclear.
Thanks in advance!
Regards,
Akshat
If you read ?heatmap carefully, you will see that by default the rows are scaled and centered.
row1 = c(1,2,3,4)
row2 = c(50,50,50,50)
m = t(as.matrix(cbind(row1, row2)))
par(mfrow=c(2,1))
heatmap(m, col=rev(heat.colors(15)))
heatmap(m, scale = "none", col=rev(heat.colors(15)))
Should do the trick.
Centered and Scaled (the default)
Edit: I reversed the color scale so that red maps to larger values.
2nd: Your desired output is going to be difficult to obtain because 50 is so much larger than the rest of your data. To regain the color detail in row1 you would need to set the breaks in the heatmap function until you get the desired result
heatmap(m, scale = "none", col=rev(heat.colors(5)), breaks=c(0,1,2,3,4,50))
This function should work
library(plotly)
hmap <- function(mat, ... ) {
s <- 1:nrow(mat)
plot_ly(x = s, y = s, z = mat[rev(s), ], type = "heatmap", ...)
}
m <- matrix(1:8, 2, byrow = T)
hmap(m)
You can adjust colors with the colors argument, e.g.
hmap(m, colors = c('Red','Yellow','Green'))
Refer to the above plot. I have drawn the equations in excel and then shaded by hand. You can see it is not very neat. You can see there are six zones, each bounded by two or more equations. What is the easiest way to draw inequalities and shade the regions using hatched patterns ?
To build up on #agstudy's answer, here's a quick-and-dirty way to represent inequalities in R:
plot(NA,xlim=c(0,1),ylim=c(0,1), xaxs="i",yaxs="i") # Empty plot
a <- curve(x^2, add = TRUE) # First curve
b <- curve(2*x^2-0.2, add = TRUE) # Second curve
names(a) <- c('xA','yA')
names(b) <- c('xB','yB')
with(as.list(c(b,a)),{
id <- yB<=yA
# b<a area
polygon(x = c(xB[id], rev(xA[id])),
y = c(yB[id], rev(yA[id])),
density=10, angle=0, border=NULL)
# a>b area
polygon(x = c(xB[!id], rev(xA[!id])),
y = c(yB[!id], rev(yA[!id])),
density=10, angle=90, border=NULL)
})
If the area in question is surrounded by more than 2 equations, just add more conditions:
plot(NA,xlim=c(0,1),ylim=c(0,1), xaxs="i",yaxs="i") # Empty plot
a <- curve(x^2, add = TRUE) # First curve
b <- curve(2*x^2-0.2, add = TRUE) # Second curve
d <- curve(0.5*x^2+0.2, add = TRUE) # Third curve
names(a) <- c('xA','yA')
names(b) <- c('xB','yB')
names(d) <- c('xD','yD')
with(as.list(c(a,b,d)),{
# Basically you have three conditions:
# curve a is below curve b, curve b is below curve d and curve d is above curve a
# assign to each curve coordinates the two conditions that concerns it.
idA <- yA<=yD & yA<=yB
idB <- yB>=yA & yB<=yD
idD <- yD<=yB & yD>=yA
polygon(x = c(xB[idB], xD[idD], rev(xA[idA])),
y = c(yB[idB], yD[idD], rev(yA[idA])),
density=10, angle=0, border=NULL)
})
In R, there is only limited support for fill patterns and they can only be
applied to rectangles and polygons.This is and only within the traditional graphics, no ggplot2 or lattice.
It is possible to fill a rectangle or polygon with a set of lines drawn
at a certain angle, with a specific separation between the lines. A density
argument controls the separation between the lines (in terms of lines per inch)
and an angle argument controls the angle of the lines.
here an example from the help:
plot(c(1, 9), 1:2, type = "n")
polygon(1:9, c(2,1,2,1,NA,2,1,2,1),
density = c(10, 20), angle = c(-45, 45))
EDIT
Another option is to use alpha blending to differentiate between regions. Here using #plannapus example and gridBase package to superpose polygons, you can do something like this :
library(gridBase)
vps <- baseViewports()
pushViewport(vps$figure,vps$plot)
with(as.list(c(a,b,d)),{
grid.polygon(x = xA, y = yA,gp =gpar(fill='red',lty=1,alpha=0.2))
grid.polygon(x = xB, y = yB,gp =gpar(fill='green',lty=2,alpha=0.2))
grid.polygon(x = xD, y = yD,gp =gpar(fill='blue',lty=3,alpha=0.2))
}
)
upViewport(2)
There are several submissions on the MATLAB Central File Exchange that will produce hatched plots in various ways for you.
I think a tool that will come handy for you here is gnuplot.
Take a look at the following demos:
feelbetween
statistics
some tricks