Control the gap of pie labels in R? - r

t = table(iris$Species)
pie(t, labels=rownames(t))
This draws a simple pie. I want that the labels are a little bit more away from the pie. I checked the par() docu but I think I don't understand it completly and I missed the option for that.
This question is explicite about R's own pie() and not related to any other extern R package.

I don't think you can really do this with the pie function. If you look at View(pie) you'll see that the labels are drawn using the text function. This means that they are not really axis labels, and that par has little effect on them. You could try to do stuff by using the arguments of the text function (i.e. pos = 2, offset = 1) but this will affect all labels in the exact same way and results in warnings. To me it seems that the only way is the stupid way by adding some spaces before/ after labels. ie:
t = table(iris$Species)
nms = rownames(t)
# spaces needed after the labels
nms[2] = paste0(nms[2], strrep(' ', 7))
# spaces needed before the labels
nms[c(1, 3)] = paste0(strrep(' ', 7), nms[c(1, 3)])
pie(t, labels = nms)
If you want to a better solution, you could rewrite the pie function to be a bit more flexible or use a different package.

Related

Controlling margins in a genoPlotR plot_gene_map

I'm producing a plot_gene_map figure by the genoPlotR R package, which gives a horizontal phylogenetic tree where aligned with each leaf is a genomic segment.
Here's a simple example that illustrates my usage and problem:
The plot_gene_map function requires an ade4s' package phylog object which represents the phylogenetic tree:
tree <- ade4::newick2phylog("(((A:0.08,B:0.075):0.028,(C:0.06,D:0.06):0.05):0.0055,E:0.1);")
A list of genoPlotR's dna_seg objects (which are essentially data.frames with specific columns), where the names of the list elements have to match the names of the leaves of tree:
dna.segs.list <- list(A=genoPlotR::as.dna_seg(data.frame(name=paste0("VERY.LONG.NAME.A.",1:10),start=seq(1,91,10),end=seq(5,95,10),strand=1,col="black",ly=1,lwd=1,pch=1,cex=1,gene_type="blocks",fill="red")),
B=genoPlotR::as.dna_seg(data.frame(name=paste0("VERY.LONG.NAME.B.",1:10),start=seq(1,91,10),end=seq(5,95,10),strand=1,col="black",ly=1,lwd=1,pch=1,cex=1,gene_type="blocks",fill="blue")),
C=genoPlotR::as.dna_seg(data.frame(name=paste0("VERY.LONG.NAME.C.",1:10),start=seq(1,91,10),end=seq(5,95,10),strand=1,col="black",ly=1,lwd=1,pch=1,cex=1,gene_type="blocks",fill="green")),
D=genoPlotR::as.dna_seg(data.frame(name=paste0("VERY.LONG.NAME.D.",1:10),start=seq(1,91,10),end=seq(5,95,10),strand=1,col="black",ly=1,lwd=1,pch=1,cex=1,gene_type="blocks",fill="yellow")),
E=genoPlotR::as.dna_seg(data.frame(name=paste0("VERY.LONG.NAME.E.",1:10),start=seq(1,91,10),end=seq(5,95,10),strand=1,col="black",ly=1,lwd=1,pch=1,cex=1,gene_type="blocks",fill="orange")))
And a list of genoPlotR's annotation objects, which give coordinate information, also named according to the tree leaves:
annotation.list <- lapply(1:5,function(s){
mids <- genoPlotR::middle(dna.segs.list[[s]])
return(genoPlotR::annotation(x1=mids,x2=NA,text=dna.segs.list[[s]]$name,rot=30,col="black"))
})
names(annotation.list) <- names(dna.segs.list)
And the call to the function is:
genoPlotR::plot_gene_map(dna_segs=dna.segs.list,tree=tree,tree_width=2,annotations=annotation.list,annotation_height=1.3,annotation_cex=0.9,scale=F,dna_seg_scale=F)
Which gives:
As you can see the top and right box (gene) names get cut off.
I tried playing with pdf's width and height, when saving the figure to a file, and with the margins through par's mar, but they have no effect.
Any idea how to display this plot without getting the names cut off?
Currently genoPlotR's plot_gene_map does not have a legend option implemented. Any idea how can I add a legend, let's say which shows these colors in squares aside these labels:
data.frame(label = c("A","B","C","D","E"), color = c("red","blue","green","yellow","orange"))
Glad that you like genoPlotR.
There are no real elegant solution to your problem, but here are a few things you can attempt:
- increase annotation_height and reduce annotation_cex
- increase rotation (“rot”) in the annotation function
- use xlims to artificially increase the length of the dna_seg (but that’s a bad hack)
For the rest (including the legend), you’ll have to use grid and its viewports.
A blend of the first 3 solutions:
annotation.list <- lapply(1:5,function(s){
mids <- genoPlotR::middle(dna.segs.list[[s]])
return(genoPlotR::annotation(x1=mids, x2=NA, text=dna.segs.list[[s]]$name,rot=75,col="black"))
})
names(annotation.list) <- names(dna.segs.list)
genoPlotR::plot_gene_map(dna_segs=dna.segs.list,tree=tree,tree_width=2,annotations=annotation.list,annotation_height=5,annotation_cex=0.4,scale=F,dna_seg_scale=F, xlims=rep(list(c(0,110)),5))
For the better solution with grid: (note the "plot_new=FALSE" in the call to plot_gene_map)
# changing rot to 30
annotation.list <- lapply(1:5,function(s){
mids <- genoPlotR::middle(dna.segs.list[[s]])
return(genoPlotR::annotation(x1=mids,x2=NA,text=dna.segs.list[[s]]$name,rot=30,col="black"))
})
names(annotation.list) <- names(dna.segs.list)
# main viewport: two columns, relative widths 1 and 0.3
pushViewport(viewport(layout=grid.layout(1,2, widths=unit(c(1, 0.3), rep("null", 2))), name="overall_vp"))
# viewport with gene_map
pushViewport(viewport(layout.pos.col=1, name="geneMap"))
genoPlotR::plot_gene_map(dna_segs=dna.segs.list,tree=tree,tree_width=2,annotations=annotation.list,annotation_height=3,annotation_cex=0.5,scale=F,dna_seg_scale=F, plot_new=FALSE)
upViewport()
# another viewport for the margin/legend
pushViewport(viewport(layout.pos.col=2, name="legend"))
plotLegend(…)
upViewport()
Hope that helps!
Lionel
Which function or package could I use to add the legend? The R base functions did not seem to work for me. The following message is displayed:
Error in strheight(legend, units = "user", cex = cex) :
plot.new has not been called yet"

R: Change strip text of effect plot

I have a simple effect plot in R using the effects package. Here is an example:
require(effects)
A <- rnorm(100)
B <- rnorm(100)
C <- factor(rep(c("right", "left"), 50))
eff <- effect("B:C", lm(A~B*C))
plot(eff)
I want to change the strip text of the panels (in this case C:left and C:right) to any text (eg. text:left). There is an unelegant way to achieve this by changing the data using:
names(eff$x)[names(eff$x)=="C"] <- "text"
I don't want to change the data though. Also changing the element name restricts me from using spaces or other special symbols. Using the function to change the strip names in plot.eff
plot(..., strip = function(...) strip.default(..., strip.names = c(factor.names, TRUE)))
gives me an error though ("formal argument "strip" matched by multiple actual arguments") since this seems to be used by the xyplot too.
Is there a way to change the strip text without modifying the data? Any help would be highly appreciated!

Expression in R

I want to use expression for my ylab= when plotting in R. How do I get characters, using expression using the \mathcal{} style font? I am running Ubuntu and I don't want to use ggplot.
To clarify: part of the ylab will contain mathcal characters and part of it will not (and will be a formula). That's why I want to use expression
It is this symbol which I want:
= \mathcal{E}
One possible solution is to use tikz, which is kind of awesome, here is a much less beautiful example than the one they generate in the help of function tikzDevide::tikz:
require(filehash)
require(tikzDevice)
tikz("sinhplot.tex", width = 8, height = 4,
standAlone = TRUE,
packages = c("\\usepackage{tikz}",
"\\usepackage[active,tightpage,psfixbb]{preview}",
"\\PreviewEnvironment{pgfpicture}",
"\\setlength\\PreviewBorder{0pt}",
"\\usepackage{amssymb}")
plot(sinh, col="steelblue", lwd=2, xlim=c(-3,3), xlab="$\\mathcal E= [-3 , 3]$")
grid()
dev.off()
tools::texi2dvi(sinhplot,pdf=T)
system(paste(getOption('pdfviewer'),'sinhplot.pdf'))
The result looks quite nice!
Another solution is to just give up having beautiful characters in the labels of your figures...

Combine two plots created with effects package in R

I have the following Problem. After running an ordered logit model, I want to R's effects package to visualize the results. This works fine and I did so for two independent variables, then I tried to combine the two plots. However, this does not seem to work. I provide a little replicable example here so you can see my problem for yourself:
library(car)
data(Chile)
mod <- polr(vote ~ age + log(income), data=Chile)
eff <- effect("log(income)", mod)
plot1 <- plot(eff, style="stacked",rug=F, key.args=list(space="right"))
eff2 <- effect("age", mod)
plot2 <- plot(eff2, style="stacked",rug=F, key.args=list(space="right"))
I can print these two plots now independently, but when I try to plot them together, the first plot is overwritten. I tried setting par(mfrow=c(2,1)), which didn't work. Next I tried the following:
print(plot1, position=c(0, .5, 1, 1), more=T)
print(plot2, position=c(0,0, 1, .5))
In this latter case, the positions of the two plots are just fine, but still the first plot vanishes once I add the second (or better, it is overwritten). Any suggestions how to prevent this behavior would be appreciated.
Reading down the long list of arguments to ?print.eff we see that there are some arguments for doing just this:
plot(eff, style="stacked",rug=F, key.args=list(space="right"),
row = 1,col = 1,nrow = 1,ncol = 2,more = TRUE)
plot(eff2, style="stacked",rug=F, key.args=list(space="right"),
row = 1,col = 2,nrow = 1,ncol = 2)
The reason par() didn't work is because this package is using lattice graphics, which are based on the grid system, which is incompatible with base graphics. Neither par() nor layout will have any effect on grid graphics.
This seems to work:
plot(eff,col=1,row=2,ncol=1,nrow=2,style="stacked",rug=F,
key.args=list(space="right"),more=T)
plot(eff2,col=1,row=1,ncol=1,nrow=2,style="stacked",rug=F,
key.args=list(space="right"))
edit: Too late...

lattice auto.key - how to adjust lines and points?

When I use barchart() and I get something like this (I know the image is not a bar chart but my auto.key produces the same legend):
I would like to fill the points and make them larger or set them to rectangles with the corresponding color.
When I use densityplot() and I get something like this:
I would like to make the lines "thicker" if possible.
See ?xyplot. Some details:
For your first question about changing colors use col argument, e.g.
barplot(table(mtcars$am, mtcars$gear), col = c("green", "yellow"))
But if you want to deal with a scatterplot instead of barplot (confused here) with modified symbols, then auto.key is not on option unfortunately, but something like this would work without problems:
xyplot(mtcars$hp ~ mtcars$wt, groups = mtcars$gear,
key = list(text = list(as.character(unique(mtcars$gear))),
points = list(pch = 10:12, col = 12:14)), pch = 10:12, col = 12:14)
For your second question use lwd:
densityplot(mtcars$hp, lwd = 3)
I just spent a good chunk of time on essentially this same problem. For some reason, the #daroczig style approach wasn't working for changing line types (including for the key) in a densityplot.
In any case, I think the "right" approach is to use trellis.par.set along with auto.key like so:
# Maybe we'll want this later
old.pars <- trellis.par.get()
trellis.par.set(superpose.symbol=list(pch = 10:12, col = 12:14))
xyplot(hp ~ wt, data=mtcars, groups = gear, auto.key=TRUE)
# Optionally put things back how they were
trellis.par.set(old.pars)
There's actually less typing this way (especially if you don't count my saving and restoring the original trellis pars), and less redundancy (allowing for DRY coding). Also, for the life of me, I can't figure out how to easily make multiple columns using key, but you can add columns as one of the elements of the auto.key list.
Also, make sure you're changing the right element! For example, if you changed plot.symbol (which sure sounds like the right thing), it would not do anything. Generally, for things based on xyplot, I believe superpose.* are the right elements to actually modify the symbols, lines, etc.
daroczig's answer is what I typically do when I face this kind of situation. In general, however, I prefer to use lattice default colors instead of specifying my own colors.
You can do that by doing this:
lattice.theme <- trellis.par.get()
col <- lattice.theme$superpose.symbol$col
pl <- xyplot(X ~ Y, groups=Z, data=dframe, pch=1:nlevels(dframe$Z),
type='o', key=list(text=list(levels(dframe$Z)), space='top',
points=list(pch=1:nlevels(dframe$Z), col=col),
lines=list(col=col),
columns=nlevels(dframe$Z)))

Resources