How do I set the stroke color for column charts within IcCube - iccube

I'm looking for a way to not only draw a fill color for my column chart, but also set it's stroke so I can have a border for each slice. This would look good for forecasts / actuals comparisons, like this example:

Not sure if I fully grasp your question, but to create such a chart in icCube, you'll need to use the chart/Type: Combo and combine several column graphs.
assumption: account is an expense account
In this case:
first column, data: actuals, color red, miscellaneous: new stack = on
second column (will plot over the 1st): data: budget/forecast, color: conditional - if it's in the future > white, else it is green, set the linecolor & thickness, miscellaneous: new stack = on
third column: (will plot over the 1st and 2nd): data: min(actuals & budget/forecast), color: grey

Related

R heatmaply: Show high-expression values on top

Heatmap with high expression values on the bottom
I'm quite new to Rstudio and I'm trying to make a heatmap using the heatmaply function in r, but in some heatmaps (with different data) the high expression values (in red) show on top, and with another dataset the high expression values show up at the bottom, with low expression values on top, as in the image.
I use the same code for the different datasets
heatmaply(Heatmap_DEXFORM, dendrogram = "row", scale_fill_gradient_fun = scale_fill_gradient2(low="blue",high="red", midpoint=0,limits=c(-4,6))
Is this a result of the way my data is shaped? Is there a command where I can make the heatmap flip so the high expression values show on top, as in my other heatmaps?
Thanks in advance!
heatmaps are typically ordered based on hierarchical clustering rather than the magnitude of the values. To order based on magnitude (high at the top or vice versa) you would need to supply a dendrogram (as Tal suggested) or manually re-order your data (for example, based on the row sums or row means (or column sums/means)).
See the toy example below.
mat <- scale(mtcars)
heatmaply(mat, dend = "none")
heatmaply(mat[order(rowSums(mat)), ], dend = "none")

Coloring and Labeling points in geom_point

I am trying to create a polar chart with two levels as similar to:
However I am having a bit of difficulty with coloring the points, then coloring the labels without losing the color on the original points. I do not know if I should ask my questions in multiple questions or all together. I figured all together since they relate to the same graph, but if that is not allowed, please let me know and I can edit before it gets down voted. That is simply unfair. I have posted a reproducable example down below with comments to make it easier.
I have two dataframes whioh basically are the same. One of them has an extra column, df2$plotter that I use to create a subset of the data to then plot the second level. The color vector, cdf, is a vector of where I have HEX values as colors.
Coloring Points
If it was one level I would use the scale_color_manual and fill/color the points that way. However, since I have two dataframes I thought I could call a color vector so to say that would be used to color based on the values within the vector. Yet it does not use those colors I assigned. Instead it labels, points D to O as a murky green and not greyas indicated by the HEX code: #A9A9A9 and uses the color as part of the legend. I would prefer a mapping as below. I do not know how to create a color vector such that is the values for each cell is used as the actual color, this vector also needs to work to color the labels. Secondly when I try to pass the same vector for the second level, the aesthetics in geom_point as Error: Aesthetics must be either lenght 1 or the same as the data This is both with adding plotter to the color paletter, but most likely I am guessing is do to the size of the vector itself. I would also prefer not to create another color vector, but simply refer to the first one.
• Alice (both Alice and Alice2) is #b79f00
• Bob is #00ba38
• Charlie is #00bfc4
• Peter is#619cff`
• Quin is #F8766D
• Roger is#f564e3`
• Then D to O is #A9A9A9
Labeling and Coloring said labels
I can add labels with geom_text. Then I call the same data and aesthetics. My issue is partially the coloring as mentioned above, but now when I color them, I lose my color but keep the fill of my points. Observe below. I do not know why my color gets lost down the way or how to fix them. I tried to plot the text first then the points, but that didn't change anything nor would I have guessed it to. Each label should be the same color as its point in short.
Reproducible Data:
k<-18
ct<-12
x_vector<-seq(1,k,1)
radius<-rep(5,k,1)
name<-c('Alice','Bob','Charlie','D','E','F','G','H','I','J','K','L','M','N','O','Peter','Quin','Roger')
df<-data.frame(x_vector,radius,name)
name2<-c('Alice2','Bob2','Charlie2','D','E','F','G','H','I','J','K','L','M','N','O','Peter2','Quin2','Roger2')
plotter<-c(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1)
radius2<-rep(7,k,1)
df2<-data.frame(x_vector,radius2,name2,plotter)
color1<-c('#F8766D'
,'#F564E3'
,'#B79F00'
)
other_color<-c(rep('#A9A9A9',ct))
color2<-c('#00BFC4'
,'#619CFF'
,'#00BA38'
)
cdf<-c(color1,other_color,color2) #color palette
df$label_radius<-df$radius+0.5 ##used to adjust the labels by a radius of 0.5
p<-ggplot()+
## Level1
geom_point(data=df,aes(x=x_vector,y=radius,color=cdf,fill=cdf),size=3,shape=21)+
geom_text(data=df,aes(x=x_vector,y=label_radius,label=name,color=name))+
## Level2
geom_point(data=df2[(df2$plotter>0),], aes(x=x_vector,y=radius2,color=name2,fill=name2),size=3,shape=21)+
geom_text(data=df2[(df2$plotter>0),], aes(x=x_vector,y=radius2,label=name2,color=name2))+
## transform into polar coordinates
coord_polar(theta='x',start=0,direction=-1,clip='on')+
## sets up the scale to display from 0 to 7
scale_y_continuous(limits=c(0,7))+
## Used to 'push' the points so all 'k' show up.
expand_limits(x=0)
p
In general, I'd personally prefer to have all data in one dataframe, and add another variable to it: So instead of Bob1 and Bob2 as df$name, having df$name=Bob and creating, say, df$nr.
It is then easier to assign the same color to all Bob occurrences.
But sticking to your example: You can set colors values manually with scale_color_manual and scale_fill_manual (see link).
library(ggplot2)
k<-18
ct<-12
x_vector<-seq(1,k,1)
radius<-rep(5,k,1)
name<-c('Alice','Bob','Charlie','D','E','F','G','H','I','J','K','L','M','N','O','Peter','Quin','Roger')
df<-data.frame(x_vector,radius,name)
name2<-c('Alice2','Bob2','Charlie','D','E','F','G','H','I','J','K','L','M','N','O','Peter2','Quin2','Roger2')
plotter<-c(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1)
radius2<-rep(7,k,1)
df2<-data.frame(x_vector,radius2,name2,plotter)
color1<-c(rep('#F8766D',2), # Alice and Alice2
rep('#F564E3',2), # Bob and Bob2
rep('#B79F00',1) # Charlie
)
other_color<-c(rep('#A9A9A9',12))
color2<-c(rep('#00BFC4',2),
rep('#619CFF',2),
rep('#00BA38',2)
)
cdf<-c(color1,other_color,color2) #color palette
df$label_radius<-df$radius+0.5 ##used to adjust the labels by a radius of 0.5
p<-ggplot()+
## Level1
geom_point(data=df,aes(x=x_vector,y=radius,color=name,fill=name),size=3,shape=21)+
scale_color_manual(values=cdf)+
scale_fill_manual(values=cdf)+
geom_text(data=df,aes(x=x_vector,y=label_radius,label=name,color=name))+
## Level2
geom_point(data=df2[(df2$plotter>0),], aes(x=x_vector,y=radius2,color=name2,fill=name2),size=3,shape=21)+
geom_text(data=df2[(df2$plotter>0),], aes(x=x_vector,y=radius2,label=name2,color=name2))+
## transform into polar coordinates
coord_polar(theta='x',start=0,direction=-1)+ #,clip='on')+ # <-- the clip property does not work for me, probably due to my ggplot version
## sets up the scale to display from 0 to 7
scale_y_continuous(limits=c(0,7))+
## Used to 'push' the points so all 'k' show up.
expand_limits(x=0)
p

Rescaling colors palette in r

In R i have a cloud of data around zero ,and some data around 1, i want to "rescale" my heat colors to distinguish lower numbers.This has to be done in a rainbow way, i don't want "discrete colors".I tried with breaks in image.plot but it doesn't work.
image.plot(X,Y,as.matrix(mymatrix),col=heat.colors(800),asp=1,scale="none")
I tried :
lowerbreak=seq(min(values),quantile2,len=80)
highbreak=seq(quantile2+0.0000000001,max(values),len=20)
break=c(lowerbreak,highbreak)
ii <- cut(values, breaks = break,
include.lowest = TRUE)
colors <- colorRampPalette(c("lightblue", "blue"))(99)[ii]
Here's an approach using the "squash" library. With makecmap(), you specify your colour values and breaks, and you can also specify that it should be log stretched using the base parameter. It's a bit complex, but gives you granular control. I use it to colorize skewed data, where I need more definition in the "low end".
To achieve the rainbow palette, I used the built-in "jet" colour function, but you can use any colour set - I give an example for creating a greyscale ramp with "colorRampPalette".
Whatever ramp you use, it will take some playing with the base value to optimize for your data.
install.packages("squash")
library("squash")
#choose your colour thresholds - outliers will be RED
minval=0 #lowest value to get a colour
maxval=2.0 #highest value to get a colour
n.cols=100 #how many colours do you want in your palette?
col.int=1/n.cols
#create your palette
colramp=makecmap(x=seq(minval,maxval,col.int),
n=n.cols,
breaks=prettyLog,
symm=F,
base=10,#to give ramp a log(base) stretch
colFn=jet,
col.na="red",
right=F,
include.lowest=T)
# If you don't like the colFn options in "makecmap", define your own!
# Here's an example in greyscale; pass this to "colFn" above
user.colfn=colorRampPalette(c("black","white"))
Example for using colramp in a plot (assuming you've already created colramp as above somewhere in your program):
varx=1:100
vary=1:100
plot(x,y,col=colramp$colors) #colors is the 2nd vector in the colramp list
To select specific colours, subset from the list via, e.g., colors[1:20] (if you try this with the example above, the first colors will repeat 5 times - not really useful but you get the logic and can play around).
In my case, I had a grid of values that I wanted to turn into a coloured raster image (i.e. colour mapping some continuous data). Here's example code for that, using a made up matrix:
#create a "dummy matrix"
matx=matrix(data=c(rep(2,50),rep(0,500),rep(0.5,500),rep(1,500),rep(1.5,500)),nrow=50,ncol=41,byrow=F)
#transpose the matrix
# the output of "savemat" is rotated 90 degrees to the left
# so savemat(maty) will be a colorized version of (matx)
maty=t(matx)
#savemat creates an image using colramp
savemat(x=maty,
filename="/Users/KeeganSmith/Desktop/matx.png",
map=colramp,
outlier="red",
dev="png",
do.dev.off=T)
When using colorRampPalette, you can set the bias argument to emphasise low (or high) values.
Something like colorRampPalette(heat.colors(100),bias=3) will result focus the 'ramp' on the lower, helping them to be more visually distinguishable.

multiple colors on beanplot in R

I have created a bean plot in R using the following
beanplot(windA, side='both', border='NA',
col=list('gray',c('red','white')),
ylab='Wind Speed (m/s)' ,what=c(1,1,1,0),xaxt ='n')
axis(1,at=c(1:12),labels =c ('Jan','Feb','Mar','apr','may','Jun','Jul','Aug','Sep','Oct','Nov','Dec'))
legend('topright', fill=c('gray','red'), legend= c('Measured', 'calc'))
and I get the following image
Is there a way that I can alternate the colors? For example, can I get Jan to be gray and red then Feb to be gray and blue and continue this alternating color scheme for the year?
you could specify the color order you want, col=list('gray','red','grey','blue'), using a sample dataset USArrests from base R, the colors are cycled till all the points are plotted
require(beanplot)
beanplot(USJudgeRatings, side='both', border='NA',
col=list('gray','red','grey','blue'),
ylab='US Judge Ratings' ,what=c(1,1,1,0),xaxt ='n')

R bar chart colours for groups of bars

Fairly new to R so sorry if this is a dumb question.
I want to plot a bar chart of a lot of data - maybe 100 bars.
I want to use colours and spacing to highlight the "groups", so I might have the first 10 bars in blue, a small gap, the next 20 in red, a small gap and so on.
I can plot the data fine, but how can I do the colouring and gaps in this way?
This can be done quite easily with ggplot2 as provided in links by #Arun.
With base graphics to set space between bars you can use argument space= (sets space before each bar) and argument col= will change color in function barplot().
Here is a example with 20 bars and space between each 5 bars.
df<-sample(1:10,20,replace=T)
barplot(df,space=c(0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0),
col=rep(c("red","blue","green","yellow"),each=5))
If the number of observations in each group is identical then you can convert vector of values to matrix and then plot it (with argument beside=TRUE). In this case you just need to supply colors but bars will be grouped automatically.
df2<-matrix(df,ncol=4)
barplot(df2,beside=TRUE,col=rep(c("red","blue","green","yellow"),each=5))

Resources