I'm working on a really complicated heatmap figure in R. heatmap.2 from the gplots package is not enough for me, because I want multiple sidebar annotations, such as the heatmap.3 function permits: https://www.biostars.org/p/18211/
Here's my specific plot so far:
When I have multiple sidebar annotations on this heatmap, though, they need to be properly labeled with a legend. The legend quickly becomes unwieldy and starts bleeding off the plot or into other labels on the plot, depending on where I choose to place the legend.
I've tried using the ncols option when placing the legend at the bottom of the plot but the legend contains information about several factors worth of metadata, and I want a separate column in the legend to denote each sidebar's worth of metadata. As far as I know there is no option in the legend command to permit this functionality, so I'm interested in hearing potential ways around this.
Alternatively, I am also open to the idea of simply generating a legend image with R separately if anyone knows how to do this.
Thanks!
I am trying to create a plot in R using ggplot2. The figure looks great when displayed in R, but when I write it to pdf the labels in the legend slightly overlap the different color lines they are defining. How do I add white space between entries so that this does not happen?
Without code example it is hard to say, but if you don't see this problem in the R display and you see it in the pdf, you can try to increase the pdf output size. When rendering a pdf, the font size is kept and the elements of the graph are more squeezed if the output size is smaller than the displayed one.
p<-ggplot(mpg,aes(cyl,year))+geom_point()
ggsave('yourfile.pdf',p,width=10,height=10) # default is 7 on my install
I don't know if it is possible to change the spacing, but I don't see any parameters for that in the theme() or element_text() documentation
I use R and ggplot2 to produce graphs for my thesis. I can port them to tikz objects using the tikzdevice or to a pdf using the pdf device very easily, and in each case, specifying the width and height of the overall plot is straightforward.
However, I am actually more interested in specifying the width of the plot AREA (ie the inner box), since differences in this (particularly for plots on the same page) are more easily detected by the eye, even if they are a couple of points different in the final document.
Of course, the source of this issue can be easily put down to the axis labels that vary depending on the content.
My question is, How can I define 'fixed' axis label widths as a global option, or define the width to be exported as the inner plotting area for ggplot2 objects....
I'm trying to plot a box within a filled.contour plot, but unfortunately, when I plot the lines() after the filled.contour plot is created, the figure is shifted to the right because the scale forces the image to the left, but the box stays at the same coordinates. Here's what my code looks like:
dev.new(width=6,height=7)
mypredict<-matrix(data=mypredict,nrow=20,ncol=25)
filled.contour(x=seq(from=-1.5,to=1.5,length=20),
y=seq(from=1,to=3.75,length=25),
z=mypredict,
col=hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1)
)
top <- 3.42
bot <- 1.56
lines(c(-1,-1),c(bot,top))
lines(c(1,1),c(bot,top))
lines(c(-1,1),c(top,top))
lines(c(-1,1),c(bot,bot))
Does anyone know how I can plot those lines within the filled.contour function? Otherwise, the lines do not plot correctly onto the main image, since the scale/legend of the graph is placed on the right.
Thanks!
The manual page for filled.contour explains the problem (and gives a solution)
This function currently uses the ‘layout’ function and so is restricted
to a full page display. As an alternative consider the ‘levelplot’
and ‘contourplot’ functions from the ‘lattice’ package which work in
multipanel displays.
The output produced by ‘filled.contour’ is actually a combination
of two plots; one is the filled contour and one is the legend.
Two separate coordinate systems are set up for these two plots,
but they are only used internally - once the function has returned
these coordinate systems are lost. If you want to annotate the
main contour plot, for example to add points, you can specify
graphics commands in the ‘plot.axes’ argument. An example is
given below.
So essentially you pass some instructions as the plot.axes parameters to override standard behaviour.
In your example:
filled.contour(x = seq(from=-1.5,to=1.5,length=20),
y = seq(from=1,to=3.75,length=25), z = mypredict,
col = hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1),
plot.axes = {axis(1); axis(2); rect(left, bottom, right, top);})
Note that you have to recreate the two axes otherwise they will not be drawn. Also, no need to use the lines statement, when there is a rect function! :)
Hope this helps
I am using a map_set call to draw a map, and then using contour to plot some data on top of it.
I want to add a legend to this plot to make it useful, but it would have to be below the entire plot, and everything I've tried creates an overlapping legend over top of my image.
Can you do two plots -- one for your contour map, and another for the colorbar/legend,
using the system variable !P.MULTI to lay them out one above the other? You might also
need to specify YMARGIN keywords appropriately on each plot to keep them from overlapping.
You use pre-written colorbar routines (cbar from JHU or colorbar from Coyote, for example) and use the POSITION keyword to put them exactly where you want them. You can also pass POSITION to MAP_SET to position that where you want it.