Different color points in R - 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))

Related

Mixed geom_line & geom_point plot: remove marker from color scale

I often have to use plots mixing lines and points (ggplot2), with the colors of the line representing one variable (here, "Dose"), and the shape of the points another one (here, "Treatment). Figure 1 shows what I typically get:
Figure 1: what I get
I like having different legends for the two variables, but would like to remove the round markers from the color scale, to only show the colors (see legend mockup below, made with Gimp). Doing so would allow me to have a clean legend, with colors and shapes clearly segregated.
Figure 2 (mockup): what I would like
Would anyone know if there is a way to do that? Any help would be much appreciated.
Note: the plots above show means and error bars, but I have the same problem with any plot mixing geom_line and geom_point, even simple ones.
Thanks in advance !

R: Coloring the contour based on condition

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.

how to change the gradient of colour in bplot for R

I was wondering if anybody had much experience with the function bplot in R, I am making a 3d plot and the plot works fine. The only thing I want to change is the gradient of colour which you get from drape=TRUE. At the moment it has a single pink colour fading into blue, I really need a third colour in the middle to highlight the central data better as this is the most important for my study, and at the moment in some of the plots I am doing its too difficult to pick out and correlate with the level of y in the colour scale bar.
Does anybody have any idea how to do this?
I need more reputation to post an image of the plot but you can see what I mean in the second image of this thread.
Plot Regression Surface
Many thanks
Aaron
Try adding a colorRampPalette argument to your plot like so:
col.regions = colorRampPalette(colors=c("red","yellow"))(1000)
This will give you a gradient of 1000 shades between red and yellow, You can use any of the R colors in the color ramp, and you can specify more than two e.g.colors=c("red","orange3","palegoldenrod") if you like. You should put this argument at the same place you are putting drape=TRUE

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)

Finding set of colours that are distinguishable to each other

I am writing a graph plotting program. I thus am looking ways to obtain colours that are distinguishable to each other in order to plot various graphs in one cell.
Is there an algorithm or some website that obtains rgb values that matches this criteria?
EDIT: I am looking for 16 different colours
For an automatic solution, you can take one color as the base color and obtain good matching colors by changing the hue in the HSV/HSB color space. If you need 4 colors for example you could rotate the hue by 90° for each color.
If the number of lines on the graph isn't too large (i.e. you can pick colours by hand), I rather like this (http://colorschemedesigner.com/) for choosing colours that work together, but stand out from each other.

Resources