I'm trying to create a pie-chart, like here, but the legend and the actual pie are overlapping.
The code I used is here:
library(ggplot2)
library(grid)
par(mai = c(1,1,1,1))
numb <- c(41, 30, 21, 8)
colors <- c("black", "grey70", "grey30","white")
numb_labels <- round(numb/sum(numb) * 100, 1)
numb_labels <- paste(numb_labels, "%", sep="")
xx <- c("the process of familiarizing with the codebase",
"the patch review process",
"the impact on the users of the project",
"high degree visibility of the contributions")
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend("top", legend = xx, fill=colors, bty="n")
Can someone help me?
I don't know why you attach ggplot2.
You can use layout:
par(mai = c(0,0,0,0))
layout(c(1,2),heights=c(0.3,1))
plot.new()
legend("bottom", legend = xx, fill=colors, bty="n")
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
One solution is to use argument xpd=TRUE with function par() and then set legend outside the plot region with coordinates of legend.
par(xpd=TRUE)
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend(-1.4,1.6, legend = xx, fill=colors, bty="n")
par(mai = c(1,1,4,1), xpd=TRUE)
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend("top", inset=c(0,-0.5),cex=0.9,legend = xx, fill=colors, bty="n")
Note: First element of inset shift your legend horizontally and the second one vertically.
cex parameter can resize your legend. So may adjust your legend by changing those parameter values.
Related
Is it possible to add a grey box for the title to a base R plot, similar to the ones that come with facet_wrap in ggplot?
I cannot use ggplot for a graphic, but I would like to add the gray title box when combining several plots.
you can try a very hackish one.
plot(1)
legend(x = c(0.567, 1.432), y = c(1.542015, 1.432), legend="", xpd=T, bg="grey")
mtext("this is a title", side=3, line=1)
More general approach...but still need to find out the ratios to expand the limits manually.
xlim = c(1,11)
ylim = c(1,15)
plot(1, xlim=xlim, ylim=ylim)
legend(x = xlim*c(0.84,1.032), y = c(ylim[2]*1.04, ylim[2]*1.16), legend="", xpd=T, bg="grey")
mtext("this is a title", side=3, line=1)
I'm creating a graphic that has a few different graph elements, using layout() to define plotting regions. I have a separate region for labels that need to align to bars on a barplot in an adjacent plotting region.
I can take a guess at where to plot the labels so that they line up - but the number of these locations will vary so this is not an ideal solution.
Here's an example of what I'm trying to do:
labs <- paste("Some text", letters[1:9])
datA <- table(sample(letters[1:9], size=200, replace=TRUE, prob=rep(c(0.1,0.2,0.3),3)))
layout(matrix(c(1,2,3,3), 2, 2, byrow=TRUE), widths=c(1,2), heights=c(6,1))
plot.new()
text(x=1, y=seq(0.05,1.0,0.111), labels=labs, adj=1, cex=1.4)
barplot(datA, horiz=TRUE, las=1, axes=F, yaxt="n")
How can I find the correct values to plot the labels?
(I'm aware that it looks like this can be solved by just plotting the labels with the barplot - this is not a viable solution for what I'm doing).
The output of barplot gives the heights so:
bp <- barplot(datA, horiz=TRUE, las=1, axes=F, yaxt="n")
text(0*bp, bp, labs, col = "blue", pos = 4)
I am using vegan library to make some plots, with this code:
raremax <- min(colSums(mydata))
col <- palette()
lty <- c("solid", "dashed", "longdash", "dotdash")
pars <- expand.grid(col = col, lty = lty, stringsAsFactors = FALSE)
out <- with(pars[1:18, ], rarecurve(mydata, step = 100, sample = raremax,
cex =0.6, ylab="OTUs", label=F, col=col, lty=lty, lwd=2))
Then I add a legend using this code:
legend("bottomright", names(mydata), col=pars[1:18,1], lty= pars[1:18,2],
lwd=2, cex=0.5, xjust=1, ncol=2, x.intersp=0.5, y.intersp=0.5, bg="white")
The resulting graph looks like this:
I would like to reduce the space between legend columns, also reducing the size of the legend box, but I can't find a way to do that.
Anyone could provide me some help?
A combination of the legend() parameters "x.intersp" and "text.width" should be helpful.
Decreasing "x.intersp" (default value = 1, for me 0.25 looked good) should move your the legend labels closer to their respective points. Decreasing "text.width" (default value=NULL, for me 0.045 looked good) moves the columns closer together.
I like to use it for prepare the plot, add horizontal lines and finally plot the bars. When I do it like below, R plots every thing twice and this looks not so nice. For a normal plot the equivalent I search is: plot(NULL, xlim=c(1,2), ylim=c(3,5), axes=F)
data = c(1,4,2)
barplot(data)
abline(h=seq(0,5,1), col="red")
barplot(data, add=T)
I prefer to have a base solution.
Plotting with col and border set to NA and axes = FALSE will effectively create an empty plot. Then you can add abline and actual barplot
data = c(1,4,2)
barplot(data, col = NA, border = NA, axes = FALSE)
abline(h=0:5, col="red")
barplot(data, add = TRUE)
I am not entirely sure if this is what you are looking for. You can "plot" with white bar and white boarder line. For example,
data = c(1,4,2)
# "white" / rgb(1,1,1) / rgb(255,255,255, maxColorValue=255) all identical
barplot(data, col=rgb(255, 255, 255, maxColorValue=255), border="white")
abline(h=seq(0,5,1), col="red")
barplot(data, add=T, xaxt="n", yaxt="n")
axis(2, at=c(0:4), label=c(0:4))
axis(1)
but this will produce same plot that is produced by your sample code.
I want to make a legend on my graph, which is generated by plot() function. The original legend() function will generate a list which has only 1 column. How can I make a legend which has 2 columns?
I could not find a way to do that within a single call to legend for standard plots.
Here's an option, drawing two separate legends: one with lines and points, one with labels. x.intersp can be used to tweak distance between labels and lines.
plot(cumsum(runif(n = 100)))
# draw legend with lines and point but without labels and box. x.intersp controls horizontal distance between lines
L = legend(x = 'bottom', legend = rep(NA,4), col=1:2, lty=c(1,1,2,2), ncol=2, bty='n', x.intersp=0.5, pch=c(1,2,1,2), inset=0.02)
# use position data of previous legend to draw legend with invisble lines and points but with labels and box. x.intersp controls distance between lines and labels
legend(x = L$rect$left, y = L$rect$top, legend = c('Group A', 'Group B'), col=rep(NA,2), lty=c(1,1), ncol=1, x.intersp = 3, bg = NA)
Check this:
library(lattice)
myPCH <- 15:17
Data <- rnorm(50)
Index <- seq(length(Data))
xyplot(Data ~ Index,
pch = myPCH, col=1:2,
key = list(space = "right", adj=1,
text = list(c("a", "b", "c"), cex=1.5),
points = list(pch = myPCH),
points = list(pch = myPCH,col=2)))
It looks like Victorp answered this in the comments of the original post. The ncol argument in the legend function works for me:
legend(locator(1), legend=c("name1","name2", "name3", "name4"), lty=2, col=c("black", "blue", "dark green", "orange"), ncol=2)
enter image description here