R - Boxplot with label in the left bottom corner - r

I want to put some labels into a boxplot, to show significance values. My favorite would be the position left bottom. I tried to do it with legend, but had the problem that I don't have any symbols to show and couldn't find anything to show the legend without symbols. I found this, but thought it's way too complicated. There should exist an easier solution.
If it's possible I would prefer to use the base R functionality and no plugin.
So this is how it looks like now and I want the labels (in rows) in the bottom left corner without the gap in the beginning.
Maybe the legend command isn't the command I'm looking for?

One way to do this is to write the text directly using the text function.
Since you do not provide your data, I will illustrate with some built-in data, but you will need to adjust the placement for your specific plot.
boxplot(Sepal.Width ~ Species, data=iris, ylim=c(1,5))
text(0.45,0.95, "1960-2002**\n2002-2012**\nt-test verb.", adj=c(0,0))
The adj=c(0,0) part is to left-justify the text.
If you want the box around the text, add:
rect(0,0,1.2,1.6)
You also can do this with legend and get rid of the gap by specifying the x,y coordinates of the legend. Once again, you will need to adjust for your plot.
boxplot(Sepal.Width ~ Species, data=iris, ylim=c(1,5))
legend(0.23, 1.65, c("1960-2002**", "2002-2012**", "t-test verb."))

Related

Can I place my ggplot2 legend right beside the horizontal axis label?

In order to save as much as white space as I can, I would like to place the legend entry at the same height as the horizontal axis label. Can this be done and if so, how?
Here's a plot illustrating what I am hoping to achieve, with current and hoped-for legend position illustrated using a (manually added) green box.
I currently place the legend using theme(legend.position=c(0.87,0.1)) (noting that the exact coordinates are not relevant). Ideally, this route would allow for values outside of the [0,1] domain but it appears not to allow for that.
theme(legend.position="bottom") places the legend well outside of the plotting area, thus taking up more white space than I am willing to spare.
You just might have to play around with negative values regarding the y-coordinates of your legend.position-vector.
Here's an example:
library(ggplot2)
ggplot(iris, aes(Sepal.Width, Sepal.Length, color=Species))+
geom_line()+
facet_wrap(~Species)+
theme(legend.position=c(0.87,-0.01))
Note the -0.01 value. Is this what you're looking for?

How to plot just one axis label in R?

A beginner question: I currently have some plots that look like this.
I'm keeping the plots free of other annotation because I have to fit a lot of them onto one page and specific values aren't important, just the general trend that the red line is to the right of the black one. However, I'd like to be able to indicate that the dashed line in my plot actually represents zero, like so:
Is there any way to tell R to only display that value on the x-axis labels? I don't think using text() would be a good idea because it relies on absolute coordinates, but I'm not really sure how to work with axis options to make it do what I want.
Try:
axis(side = 1, at = 0)
See ?axis for details.

quantmod barChart (or chartSeries) formatting options

I have just started playing with the quantmod package. The documentation is however, quite sparse (perhaps understandably, since it is OSS).
I am currently using barChart() which is a nice wrapper around chartSeries() and does most of what I want, but the default chart it produces are not quite what I want. To be specific, I want to tweak the charts produced by barChart() to suit my needs - however, since I am a newbie, I don't know whether my "tweaks" can be provided as options to the wrapper barChart(), or if I need to call chartSeries() directly, with specific arguments.
I have been tearing my hair out trying to do the following:
replace the horrible {start date}/{end date} text in the top right hand of the chart produced by barChart() with text of my own choosing
specify the formating to be used on the X axis (for example, show only the last two digits of the century. i.e. '98, '99, '00, '01 etc)
'Force' both top chart and the bottom chart to have their Y values printed on the left hand side of the chart
Add an aditional series to the bottom chart
Use different up/down colors for the bottom chart (defaults the using the same up/down colors for both top and bottom charts)
Plot just the top chart (no bottom chart)
Specify X axis, Y axis grid line spacings for top chart, for bottom chart
Write the image to an alternative output (e.g. png image or pdf document) instead of the graphics device
Can anyone help with any (or all) of the above?.
This functionality isn't available (patches welcome).
This functionality isn't available (patches welcome).
This functionality isn't available (patches welcome).
See the sparse documentation for ?addTA, specifically the on argument.
Plot the bottom chart as two separate up/down series, using two different colors, or perhaps chartTheme.
Not sure what you mean; just don't plot the bottom chart...
See the sparse documentation for the major.ticks argument to chartSeries. I don't think you can change the y axis grid line spacings, and the x axis spacing will be the same for the top and bottom chart.
See ?png and ?pdf.
To change or remove the bottom chart,
check the TA argument of chartSeries function
(there is an example in the manual);
to change the colours,
check the theme argument
(there is an example in the manual);
to write to a png or pdf file,
use the png or pdf functions,
as with other plotting functions.
To fine-tune the axes and labels, it is probably easier to bypass
chartSeries altogether and plot the data yourself, with base graphics,
lattice or ggplot2.

Adjusting the relative space of facets (without regard to coordinate space)

I have a primary graph and some secondary information that I want to facet in another graph below it. Facetting works great except I do not know how to control the relative space used by one facet versus another. Am aware of space='free' but this is only useful if the ranges correspond to the desired relative sizing.
So for instance, I may want a graph where the first facet occupies 80% and the second 20%. Here is an example:
data <- rbind(
data.frame(x=1:500, y=rnorm(500,sd=1), type='A'),
data.frame(x=1:500, y=rnorm(500,sd=5), type='B'))
ggplot() +
geom_line(aes(x=x, y=y, colour=type), data=data) +
facet_grid(type ~ ., scale='free_y')
The above creates 2 facets of equal vertical dimension. Adding in space='free' in the facet_grid function changes the dimensions such that the lower facet is roughly 5x larger than the upper (as expected).
Supposing I want the upper to be 2x as large, with the same data set and ordering of facets. How can I accomplish this?
Is the only way to do this with some trickery in rescaling the data set and manually overriding axis labels (and if so, how)?
Alternative
As indicated below can use viewports to render as multiple graphs. I had considered this and in-fact had implemented using this approach in the past with standard plot and viewports.
The problem is that it is very difficult to get x-axis to align with this approach. So if there is a way to fix the size of the y-axis label region and the size of the legend region, can produce 2 graphs that have the same rendering area.
You don't need to use facets for this - you can also do this by using the viewport function.
> ratio = 1/3
> v1 = viewport(width=1,height=ratio,y=1-ratio/2)
> v2 = viewport(width=1,height=1-ratio,y=(1-ratio)/2)
> print(qplot(1:10,11:20,geom="point"),vp=v1)
> print(qplot(1:10,11:20,geom="line"),vp=v2)
Ratio is the proportion of the top panel to the whole page. Try 2/3 and 4/5 as well.
This approach can get ugly if your legend or axis labels in the two plots are different sizes, but for a fix, see the align.plots function in the ggExtra package and ggplot2 author Hadley Wickam's notes on this very topic.
There's no easy way to do this with facets currently, although if you are prepared to go down to editing the Grid, you can modify the ggplot graph after it has been plotted to get this effect.
See also this question on using grid and ggplot2 to create join plots using R.
Kohske Takahashi posted a patch to facet_grid that allows specification of the relative sizing of facets. See the thread:
http://groups.google.com/group/ggplot2/browse_thread/thread/7c5454dcc04bc7b8
With luck we'll see this in a future version of ggplot2.

IDL: Can IDL add a colorbar/other legend info below a contour plot, so that it doesn't overlap anything?

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.

Resources