ggplot2: aspect_ratio and placing title/axis label in the right place - r

Using ggplot2 under R (and the code below uses rpy2, I don't actually know straight R, sorry).
I'm trying to add an aspect_ratio to my plot (so it'll fit nicely on a slide). The below example is pretty minimal. Aspect_ratio does the right thing EXCEPT the title and x-axis label are placed way above and below the plot, respectively, with a giant white space between the title and plot and between the plot and x-axis label, as in (plot here):
TITLE TITLE
PLOT PLOT PLOT PLOT
PLOT PLOT PLOT PLOT
PLOT PLOT PLOT PLOT
PLOT PLOT PLOT PLOT
x-axis label
Not cool. What can I do to squeeze them together?
#!/usr/bin/env python2.6
import rpy2.robjects.lib.ggplot2 as ggplot2
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
df = ro.DataFrame({'x': ro.IntVector((1,2)), 'y': ro.IntVector((3,4))})
pp = ggplot2.ggplot(df) + \
ggplot2.aes_string(x='x', y='y') + \
ggplot2.opts(**{'title' : 'Title',
'aspect.ratio' : 0.618033989} ) + \
ggplot2.geom_line()
grdevices = importr('grDevices')
grdevices.pdf(file="aspect.pdf")
pp.plot()
grdevices.dev_off()

This is a limitation in the current layout system, and something I hope to work on soon.

Related

How to avoid broken line in ggplot2

I try to plot a graph in R using the ggplot2 package. Let's assume I have the following data:
df1<-data.frame(xpos=c(1,2,3,4),ypos=c(0.222,0.222,0.303,0.285))
Now, I want to plot a simple line graph:
ggplot(data=df1, aes(x=xpos, y=ypos)) +
geom_line()+
geom_point()
Now, when I adjust the y-axis:
+scale_y_continuous("Y rates upd", breaks=seq(0,1,0.2),limits=c(0,1))
The lines get "broken"
That's dependent upon your graphics device that plots are sent to from R. See the same plot below when I instead export it to PDF.

how to plot countour curve on top of scatter plot in Octave

I am using Octave plot() function to plot scatter points on a 2D graph. And then I am using the contour() function to draw a contour on top of the points. But the contour() function is not overlapping on top of the points. What happens is that the scatter plot graph is replaced entirely with the contour, even though I am using the HOLD ON command.
I have something like this:
plot(); %plot the x,y scatter plot
hold on; %hold on to be able to add to the plot
contour(); %Add the contour on top of the scatter plot
I wonder if someone can show some sample code they can show to add a contour to an existing plot.
thanks
Here is an example :
x = [-10:0.1:10];
y = x .^ 2;
z = x' * x;
hold on;
contour(x,y,z);
plot(x,y);
Will yield this graph (in blue you can see the parabol issued by plot).

R plot and barplot how to fix ylim not alike?

I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.

Multiple panel plots with pheatmap

I am trying to do multipanel plots one panel being a heatmap using layout to place plots. I've been drawing heatmaps with pheatmap which provides a very convenient color scheme among other things.
The code for pheatmap is available here.
When I try using pheatmap in this way it always plots on a new page. I imagine this is because of its use of the grid package? Is there a way I can do this with pheatmap?
Example code to produce a heatmap next to a barplot but which doesn't since the heatmap gets plotted on a new page below:
xlay=layout( matrix(c(2,2,1),nrow=1) )
layout.show(xlay)
barplot(rnorm(8),horiz=T)
pheatmap(matrix(rnorm(80),nrow=8))
Make your bar plot in ggplot
bar <- ggplot()
Assign both the barplots and heatmap to a variable
heat <- pheatmap(matrix(rnorm(80),nrow=8))
then use gridExtra package to make panel plot the heatmap is saved as an object and you can plot it again by assessing the 4th item in the object
grid.arrange(bar, heat[[4]], nrow = 1)

R - add plot to facetted ggplot2 object (using annotation_custom)

I'm trying to inset a plot using ggplot2 and annotation_custom (the plot is actually a map that I'm using to replace the legend). However, I'm also using facet_wrap to generate multiple panels, but when used with annotation_custom, this reproduces the plot in each facet. Is there an easy way to insert the plot only once, preferably outside the plotting area?
Here is a brief example:
#Generate fake data
set.seed(9)
df=data.frame(x=rnorm(100),y=rnorm(100),facets=rep(letters[1:2]),
colors=rep(c("red","blue"),each=50))
#Create base plot
p=ggplot(df,aes(x,y,col=colors))+geom_point()+facet_wrap(~facets)
#Create plot to replace legend
legend.plot=ggplotGrob(
ggplot(data=data.frame(colors=c("red","blue"),x=c(1,1),y=c(1,2)),
aes(x,y,shape=colors,col=colors))+geom_point(size=16)+
theme(legend.position="none") )
#Insert plot using annotation_custom
p+annotation_custom(legend.plot)+theme(legend.position="none")
#this puts plot on each facet!
This produces the following plot:
When I would like something more along the lines of:
Any help is appreciated. Thanks!
In the help of annotation_custom() it is said that annotations "are the same in every panel", so it is expected result to have your legend.plot in each facet (panel).
One solution is to add theme(legend.position="none") to your base plot and then use grid.arrange() (library gridExtra) to plot both plots.
library(gridExtra)
p=ggplot(df,aes(x,y,col=colors))+geom_point()+facet_wrap(~facets)+
theme(legend.position="none")
grid.arrange(p,legend.plot,ncol=2,widths=c(3/4,1/4))

Resources