R: Coloring the contour based on condition - r
I have two variables (z and z1). I need to overlay the contours and find the overlapping and non overlapping area between them.
z<-matrix(c(rep(0.9,8),0.9,0.9,rep(0.8,7),0.9,0.9,0.8,rep(0.7,5),0.8,0.9,
0.9,0.8,0.7,rep(0.6,3),0.7,0.8,0.9,0.9,0.8,0.7,rep(0.6,3),0.7,0.8,0.9
,0.9,0.8,0.7,rep(0.6,3),0.7,0.8,0.9,0.9,0.8,rep(0.7,5),0.8,0.9,
0.9,rep(0.8,7),0.9),9,9)
z1<-matrix(c(rep(0.9,8),0.9,0.9,rep(0.8,7),0.9,0.9,0.8,rep(0.7,5),0.8,0.9,
0.9,0.85,0.7,rep(0.6,3),0.65,0.85,0.9,0.9,0.8,0.7,rep(0.6,3),0.7,0.8,0.9
,0.9,0.85,0.7,rep(0.6,3),0.65,0.8,0.9,0.9,0.8,rep(0.7,5),0.8,0.9,
0.9,rep(0.8,7),0.9),9,9)
levels=c(0.7,0.75,0.8,0.9)
for (lev in length(levels):2){
.filled.contour(seq(1,9,1),seq(1,9,1),z,levels=c(levels[lev-1],levels[lev]),col="red")
.filled.contour(seq(1,9,1),seq(1,9,1),z1,levels=c(levels[lev-1],levels[lev]),col="white")
}
contour(seq(1,9,1),seq(1,9,1),z,levels=c(0.7,0.75,0.8,0.9),add=T)
contour(seq(1,9,1),seq(1,9,1),add=T,z1,levels=c(0.7,0.75,0.8,0.9),col="grey",drawlabels=F)
after running this code, it will generate the following
However, I need the following.
If the contour1 (dark line from z) > contour2(the grey line from z1) the colour should be red. (which i already achieved).
If the contour1 < contor2 the color should be blue and the common area between them should be white. (the white part is also achieved and couldn't colour the blue part (see the figure, the small triangle should be blue)).
I also need to calculate the total red area and total blue area (it can be relative to map as well)
Any help would be much appreciated.
Related
Different color points in R
I have the following R-code, x1=c(3,2,4,1,2,4,4) x2=c(4,2,4,4,1,3,1) Y=c("red","red","red","red","blue","blue","blue") plot(x1,x2,col=Y,pch=8) grid(NULL,NULL,col="cornsilk2") legend("right",c("Point","star"),col=c("red","blue")) That creates a plot as seen below There are two things that I wish to change however I am not sure how to go about it. 1) I want to change the types of points that appear using the pch feature in plot. So for example, I want the red points to appear as a star and the blue points to appear as a triangle. How would I go about this? 2) I want the legend to show those symbols and be coloured respectively correctly. For example, instead of having "Point" it should be a "." that is coloured blue or red depending on what colour I decide to assign it. Many thanks for the help.
You specify a vector like your color: SHAPE = ifelse(Y=="red",8,2) plot(x1,x2,col=Y,pch=SHAPE) legend("right",c("Point","star"),col=c("blue","red"),pch=c(2,8))
R - Make lines() points/line thickness reflect density of points?
I know how to change the thickness of points/lines manually; however I wish to plot 1000+ series on the same graph so that the resulting graph has darker or lighter points depending on the density of points at that location. ie, the more points centered there the darker the area gets. What I have now is just this: plot(df[,1],ylim=c(0,1)) for(i in 2:ncol(df)){ lines(df[,i],col='grey',type='p') } Which gives me the following graph. Unfortunately there's so many points that it just gives me a uniform grey line, instead of telling me how these points are distributed.
PyQtGraph GLScatterPlotItem: How to make colors opaque
In PyQtGraph, GLScatterPlotItem, I would like the points to not blend color together when the points overlap. I want to see the closest point, and not the ones behind. I have asked for the colors to be opaque (alpha = 1.0), but when the dots in a plot overlap, the color just turns a shade of magenta, even if all the points in that region arevery similar color. Here's an example: plt = gl.GLScatterPlotItem(pos=coords, color = colors, size=5, pxMode=True) where colors are a sort of 'heat map' that range from red to blue. The plot I get is this: You can see there is some red, but everywhere the points really overlap, the color goes weird. On the other hand, if I do the simple modification of size=1, then the colors are nice, but the dots are tiny, and can be hard to see: This is exactly the same data both time. You can start to see a little of the magenta color over to the left and rear where point desity is high, but other than that, the colors are correct. How can I prevent the magentification of my plots? Thanks a bunch!
OpenGL is probably rendering dots additively and saturating. Try: plt.setGLOptions('opaque') I ran into this as well; the default options are pretty for volumetric data, but aren't great for dense point clouds from surfaces. If you figure out something even better, i.e. local patches with illumination, post back about it!
shading certain area in R
I met a problem when shading some areas by using function polygon in R. if these areas have common parts, the last shaded one will cover the previous one. But now, I do want do see this phenomenon and want to keep the original boundaries. what should I do? see the following image as an example: the codes for drawing this image are: plot(variance_five[51:60,2],type="n",ylim=c(0,0.1)) for(i in 1:6) { polygon(c(1:10,10:1),c(variance_five[(10*i-9):(10*i),1],variance_five[(10*i): (10*i-9),3]),lty=2,col=i) lines(variance_five[(10*i-9):(10*i),2]) } we can see that the last shaded area(purple) covers some blue and light blue parts, what shall I do keep all the boundaries of all areas and the colour in the common areas are the sum effect of these colours? Thank you!
You want to use transparency. The function alpha in the scales package will add transparency to any color. I'd recommend setting up a vector of colors before the loop and making them transparent, e.g. my_colors <- rainbow(6) require(scales) my_colors <- alpha(my_colors, alpha = 0.5) And then inside the loop replacing col = i with col = my_colors[i]. You can, of course, adjust the alpha level (0 is completely transparent, 1 completely opaque) as necessary. The scales package also provides a function show_col which I find useful in picking colors. Try show_col(rainbow(6)) show_col(heat.colors(6)) show_col(cm.colors(6)) I should note that rainbow et al. take alpha as an argument, so you don't need the alpha function, but it's more general to use the alpha function which will work on other R colors if you've got favorites. (I like dodgerblue2 and firebrick4, personally.)
R/quantmod: how to specify the bollinger bands colour?
This could be more generally be How to change the theme colours? Or maybe TA colours are not controlled by theme? This makes bollinger bands with a nice cloud effect: chartSeries(bars, theme="white") addBBands() (See example of how it looks (near the bottom) ) The cloud effect is dark grey on this next example, so almost invisible. chartSeries(bars, theme="black") addBBands() How do I change it to be, say, a nice bright red, with bright purple for the upper and lower lines? (Yeah, I know, -1 for the colour scheme) I believe I'll be able to specify an 8-hex-digit colour to specify semi-transparency. But can I do anything more exotic? E.g. it would be rather cool to use a gradient and have it #ff0000 at the centre, fading to #330000 at the upper and lower lines. Is there any gradient support in quantmod charting?
A look at chartTheme seems to indicate that a gradient is not possible, but the up/down colours can be specified, as can the respective border colours. Just define your own theme as per the examples. You can start with the predetermined theme and modify certain individual parameters.
Fleshing out Benjamin's answer and my own learnings, here is an example: #bars is an XTS object, OHLC data library(quantmod) chartSeries(bars) addBBands(n=20,sd=2) addBBands(n=50,sd=1) The above draws two bollinger bands, in default colour scheme. The following will change them to be a semi-transparent red (i.e. the red is stronger where they both exist): t=chartTheme() t$BBands$fill="#ff666633" #20% red (i.e. hex 33 is the transparency) reChart(theme=t) From my study of the source this should have worked to change the line colours: t$BBands$col=c('red','blue','green') But it does not. However you can change the top/bottom colours to the same colour with: t$BBands$col='blue' reChart(theme=t) And here is how to do the same with the newer chart_series() function, and notice you can set the line colours individually (NB. there is no reChart function, as far as I can see): t=chart_theme() t$bbands$col$fill="#ff000033" t$bbands$col$upper='red' t$bbands$col$lower='green' t$bbands$col$ma='blue' chart_Series(bars,theme=t) add_BBands(n=50,sd=1) add_BBands(n=20,sd=2) It is not possible, as far as I know, to use a different colour scheme for each of the two bollinger bands. Even changing the colour scheme like this fails, as after the second command it redraws both with the new colours! obj=chart_Series(bars) add_BBands(n=50,sd=1) obj$Env$theme$bbands$col$fill="#00ff0033" add_BBands(n=20,sd=2)