Julia: Gadfly size of colorbar - julia

I'm currently trying to dive deeper into Gadfly.jl for plotting in Julia. I made a plot of a few stations on a map showing daily precipitation sums, but I'm rather unhappy with the size of the colorbar. I couldn't find a way to manipulate the size of the colorbar in the documentation. Does anyone know how to do this?
Here is the plotted map:

Use themes to change the style of the colour bar.
The simplest way just to change the font size of the key label and the title (RRsum on your image). E.g. add Theme(key_label_font_size=12pt, key_title_font_size=12pt) argument to your plot call.
For example:
Smaller colorbar:
plot(
z=(x,y) -> x*exp(-(x-round(Int, x))^2-y^2),
x=range(-8,stop=8,length=150),
y=range(-2, stop=2,length=150),
Geom.contour,
Theme(key_label_font_size=10pt, key_title_font_size=10pt)
)
Smaller colorbar
Bigger colorbar:
plot(
z=(x,y) -> x*exp(-(x-round(Int, x))^2-y^2),
x=range(-8,stop=8,length=150),
y=range(-2, stop=2,length=150),
Geom.contour,
Theme(key_label_font_size=15pt, key_title_font_size=15pt)
)
Bigger colorbar

Related

How to decrease padding between lines and points in R "both" type plots

I have tried to plot a series of points in R, and I use type="b" as a plot option. However, there is a lot of padding (white space) between the points and the lines between them, so much that the line disappears entirely between some points. Her is a picture of how it looks:
I have tried to make the points smaller with the cex plot option, but this does not help, as it only changes the size of the points and not where the lines between the points between these starts and ends. I do not know if this makes a difference, but the symbols I am using are pch=1.
I am interested in knowing if it is possible to reduce this padding, and how you do so. I am not interested in using type=o as a plot option instead.
Any particular reason why you don't want to use type="o"? It seems like the easiest way to get the effect you want:
# Fake data
set.seed(10)
dfs = data.frame(x=1:10, y=rnorm(10))
plot(y~x,data=dfs, type="o", pch=21, bg='white')
pch=21 is a circle marker like pch=1, but with both border and fill. We set the fill to white with bg="white" to "cover up" the lines that go through the point markers.
You can also use cex to change the marker size to avoid overlap and make the lines between nearby points visible:
set.seed(10)
dfs = data.frame(x=1:100, y=cumsum(rnorm(100)))
plot(y~x,data=dfs, type="o", pch=21, bg="white", cex=0.6)
Using a dataframe named dfs this seems to deliver a mechanism for adjusting the surrounding "white halo" to whatever size of point an halo you want by adjusting the 'cex' values of the white and black points :
plot(y~x,data=dfs, type="l")
with(dfs, points(x,y, pch=16,col="white",cex=1.4))
with(dfs, points(x,y,cex=1) )

How to display a calculation on data values in R legend

Here's a fiddle for a simplified version of a plot I am trying to generate.
On line 44 the plot points are sized according to 1/Error:
main_aes = aes(x = Date, y = Popular_Support, size=1/Error)
But instead of displaying 1/Error values in the legend, I want it to display Sample Size which is 1/Error^2, which the legend title being Sample Size.
I only want this displayed in the legend, but I still want the original values to weight the point sizes.
How can I do this? How can I perform a calculation on the legend text that is displayed and change the legend title?
You can do this as follows:
plot + scale_size_continuous(breaks=seq(40,70,10), labels=seq(40,70,10)^2,
name="Sample Size")
Also, plot is an R function, so it's probably better to use a different name for your plot objects.

add multiple legends to a heatmap in R

My question is that I want to add the categorical variables' legends onto the graph but somehow when I used "topright", it did not work for me (see the image below). Basically my idea is to fill the blank area with the legends for my categorical variables on the side of the heatmap. My codes look like
heatmap.3(performance, Colv =NA,RowSideColors=row_annotation,col=my_palette)
par(lend = 1) # square line ends for the color legend
legend("topright", # location of the legend on the heatmap plot
legend = c("category1", "category2", "category3"), # category labels
col = c("gray", "blue", "black"), # color key
lty= 1, # line style
lwd = 10 # line width
)
Also I want to put multiple legend onto the plot but don't know how to specify their positions using x and y as there are no coordinates in my plot.
Thank you so much!
A simple fix to your problem would be to use the option inset=# (where # is some floating point number with a comma after it) following your "topright" specification after its respective comma, which would indicate how to place your legend relative to your graph.
Instead of specifying the default position "topright", perhaps you may want to try restructuring your code in a more personally customized manner, namely utilizing an x-y axis approach, such as for instance use :
dev.new(xpos=#,ypos=#)
Or you may also consider using:
legend.position=c(#,#)
Try these options, even though you say you have no access to coordinates, which is rare for graphical utilities in R, but may well be a future edit to heatmap.3. You could also use a fancy R utility called locator(1) to point and click with your mouse where you want to see your legend.
In general, the legend option is formally defined as:
legend(location, title, legend, ...)
If you have any more questions about the legend utility in R, please type in help(legend) in your R command line (in R Studio, for instance, if you use that).
To address your question on multiple legends, please consult: Plotting multiple legends

heatmap.2 (gplots) how to change horizontal size of the color key and add a legend

I am producing heatmaps with heatmap.2. I know how to controls many of the parameters but still I have not found a way of making the key of color only wider or putting it as a strip in a side or bottom of the plot.
With keysize it modifies both height and width proportionally.
Also when using ColSideColors I am using legend() to put the color labels, but 'topright' is not at the top-right. I know that this is something about the plot area, margins etc, but I have not found yet a good explanatory text of how heatmap.2 plot is structured and how to positioned things by coordinates and how to deal with oma, mar etc. Depending on the margins, samples, tree depth, etc. the legend could be placed in an open area or overlaps a bit of the heatmap. Any point to good texts for understanding theses issues with R graphics would be truly appreciated.
The coded used is:
df<- data.frame( x1=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x2=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x3=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x4=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x5=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x6=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x7=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x8=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,y1=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y2=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y3=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y4=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y5=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y6=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y7=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y8=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
)
dataMatrix <- as.matrix(df)[sample(1:120),]
heatmap.2(dataMatrix
, col=rev(brewer.pal(11,"RdBu"))
, density.info="none"
, key=TRUE
, symkey=FALSE
, trace="none"
, cexRow=1
, scale='row'
, margins =c(10,9)
, ColSideColors=c(rep("red", ncol(df)/2), rep("green", ncol(df)/2))
, main="Log2_intensities median centered"
, keysize=0.9)
legend('topright', c("x", "y"),lty=1, col=c("red", "green"), cex=0.8)
There is the way to organise your plot described here: Moving color key in R heatmap.2 (function of gplots package)
When having a ColSideColors, heatmap.2 will draw the plot as
1 ColSideColors
2 heat map
3 ... so on as in the link above
Never tested with the RowSideColors. In the other words you can organise the plot using lmat, lwid, lhei parameters of heatmap.2. this could give you some space for your legend.
You could try adding the 'inset' parameter and playing with the sizes to move the legend further right (first value) and/or further towards the top (second value) e.g:
legend('topright', inset = c(.02,.02), etc...)

Change the size of the text in legend according to the length of the legend vector in the graph

I have to draw a 20 plots and horizontally place a legends in each plots.
I gave the following command for the first plot:
plot(x=1:4,y=1:4)
legend("bottom",legend = c("a","b","c","d"),horiz=TRUE,text.font=2,cex=0.64)
then for the second plot I tried :
plot(x=1:2,y=1:2)
legend("bottom",legend = c("a","b"),horiz=TRUE,text.font=2,cex=0.64)
But because the size of the character vector passed to legend argument are different I get the size of the legend different.
Since I have to plot so many different plots having varying sizes of legends,I would want to do it in an automated fashion.
Is there a way to do this which can fix the size of the legend in all the plots and fit it to graph size?
par(cex=.64) at the beginning should suffice
op <- par(cex=.64) # this fix the legend size for all plots
plot(x=1:4,y=1:4)
legend("bottom",legend = c("a","b","c","d"),horiz=TRUE,text.font=2) # no need to set cex anymore
plot(x=1:2,y=1:2)
legend("bottom",legend = c("a","b"),horiz=TRUE,text.font=2)
par(op) # At end of plotting, reset to previous settings

Resources