R Barplot with one bar - how to plot correctly - r

I want to plot a regular bar plot in R, but with just one bar. What I don't like is the fact that the bar width gets to be the width of the whole plot. I want the bar to be "thinner", but I don't know how to do it.
Default command is:
barplot(percentage, col=c("brown4"))
where percentage is a fraction. I tried using xlim parameter, but it gets very messy (bar goes completely to the right or left). For example, I tried:
barplot(percentage, col=c("brown4"), xlim=c(0.5,1))
but this stretches the bar even more. I am an R noob.

You may try to play around with the width of the device you plotting to. E.g.:
# plot to a Windows graphic device
windows(height = 10, width = 4)
barplot(0.5)
# plot to PDF
pdf(height = 10, width = 2)
barplot(0.5)
dev.off()
You may also try width together with xlim
barplot(0.5, width = 0.1, xlim = c(0, 1))

The width argument to barplot says:
width optional vector of bar widths. Re-cycled to length the number of bars drawn. Specifying a single value will have no visible effect unless xlim is specified.
So specify both width and xlim. The interaction of these two is not obvious (to me) so you will probably need to play around with them until they look like you want them to.
percentage <- 0.25
barplot(percentage, width=0.2, xlim=c(0,1.2), col="brown4")

Related

Rotate labels for histogram bars - shown via: labels = TRUE

Here is shown how to label histogram bars with data values or percents using labels = TRUE. Is it also possible to rotate those labels? My goal is to rotate them to 90 degrees because now the labels over bars overrides each other and it is unreadable.
PS: please note that my goal is not to rotate y-axis labels as it is shown e.g. here
Using mtcars, here's one brute-force solution (though it isn't very brutish):
h <- hist(mtcars$mpg)
maxh <- max(h$counts)
strh <- strheight('W')
strw <- strwidth(max(h$counts))
hist(mtcars$mpg, ylim=c(0, maxh + strh + strw))
text(h$mids, strh + h$counts, labels=h$counts, adj=c(0, 0.5), srt=90)
The srt=90 is the key here, rotating 90 degrees counter-clockwise (anti-clockwise?).
maxh, strh, and strw are used (1) to determine how much to extend the y-axis so that the text is not clipped to the visible figure, and (2) to provide a small pad between the bar and the start of the rotated text. (The first reason could be mitigated by xpd=TRUE instead, but it might impinge on the main title, and will be a factor if you set the top margin to 0.)
Note: if using density instead of frequency, you should use h$density instead of h$counts.
Edit: changed adj, I always forget the x/y axes on it stay relative to the text regardless of rotation.
Edit #2: changing the first call to hist so the string height/width are calculate-able. Unfortunately, plotting twice is required in order to know the actual height/width.

Resize plot created with ggplot in R

I'm creating plot with R for a power point. I have to create picture with size
width = 4.24 inch, height = 2.55 inch . So I create my plot:
graf<-ggplot(...)
Then I save It
win.metafile (file, width = 4.24, height = 2.55)
plot(graf)
dev.off()
This is the result
My problem is that the plot is too mutch "in the center" of the shape. I'd like to shift it near the bottom. I'd like to keep the legend more near the bottom and more near the plot. I expect that doing so and keeping the height fixed, the bar plots will be more thin and tall( I have fixed the scale y to be "8"). I'd like also reduce the white border in the left and in the right. Somebody know the correct way to do that?
you can save your ggplots by a function
ggsave(plot,filename,width,height.....)
I think decreasing the width should solve your problem. If it doesn't then you can use the
graf<-ggplot(...)
graf <- graf+theme(legend.position = c(x,y))
arguments to place the legend exactly where do you want it to be.

Set margins to cater for large legend

I'm trying to figure out a way to calculate the height of a legend for a plot prior to setting the margins of the plot. I intend to place the legend below the plot below the x-axis labels and title.
As it is part of a function which plots a range of things the legend can grow and shrink in size to cater for 2 items, up to 15 or more, so I need to figure out how I can do this dynamically rather that hard-coding. So, in the end I need to dynamically set the margin and some other bits and pieces.
The key challenge is to figure out the height of the legend to feed into par(mar) prior to drawing the plot, but after dissecting the base codes for legend however, it seems impossible to get a solid estimate of the height value unless the plot is actually drawn (chicken and egg anyone?)
Here's what I've tried already:
get a height using the legend$rect$h output from the base legend function (which seems to give a height value which is incorrect unless the plot is actually drawn)
calculate the number of rows in the legend (easy) and multiply this by the line height (in order to do this, seems you'd need to translate into inches (the base legend code uses yinch and I've also tried grconvertY but neither of those work unless a plot has been drawn).
Another challenge is to work out the correct y value for placement of the legend - I figure that once I've solved the first challenge, the second will be easy.
EDIT:
After a day of sweating over how this is (not) working. I have a couple of insights and a couple of questions. For the sake of clarity, this is what my function essentially does:
step 1) set the margins
step 2) create the barplot on the left axis
step 3) re-set the usr coordinates - this is necessary to ensure alignment of the right axis otherwise it plots against the x-axis scale. Not good when they are markedly different.
step 4) create the right axis
step 5) create a series of line charts on the right axis
step 6) do some labelling of the two axes and the x-axis
step 7) add in the legend
Here are the questions
Q1) What units are things reported in? I'm interested in margin lines and coordinates (user-coordinates), inches is self explanatory. - I can do some conversions using grconvertY() but I'm not sure what I'm looking at and what I should be converting to - the documentation isn't so great.
Q2) I need to set the margin in step 1 so that there is enough room at the bottom of the chart for the legend. I think I'm getting that right, however I need to set the legend after the right axis and line charts are set, which means that the user coordinates (and the pixel value of an inch, has changed. Because of Q1 above I'm not sure how to translate one system to the other. Any ideas in this regard would be appreciated.
After another day of sweating over this here's what solved it mostly for me.
I pulled apart the code for the core legend function and compiled this:
#calculate legend buffer
cin <- par("cin")
Cex <- par("cex")
yc <- Cex * cin[2L] #cin(inches) * maginfication
yextra <- 0
ymax <- yc * max(1, strheight("Example", units = "inches", cex = Cex)/yc)
ychar <- yextra + ymax #coordinates
legendHeight <- (legendLines * ychar) + yc # in
Which is essentially mimicking the way the core function calculates legend height but returns the height in inches rather than in user coordinates. legendLines is the number of lines in the legend.
After that, it's a doddle to work out how to place the legend, and to set the lower margin correctly. I'm using:
#calculate inches per margin line
inchesPerMarLine<-par("mai")[1]/par("mar")[1]
To calculate the number of inches per margin line, and the following to set the buffers (for the axis labels and title, and the bottom of the chart), and the margin of the plot.
#set buffers
bottomBuffer = 1
buffer=2
#calculate legend buffer
legBuffer <- legendHeight/inchesPerMarLine
#start the new plot
plot.new()
# set margin
bottomMargin <- buffer + legBuffer + bottomBuffer
par(mar=c(bottomMargin,8,3,5))
The plot is made
barplot(data, width=1, col=barCol, names.arg=names, ylab="", las=1 ,axes=F, ylim=c(0,maxL), axis.lty=1)
And then the legend is placed. I've used a different method to extract the legend width which does have some challenges when there is a legend with 1 point, however, it works ok for now. Putting the legend into a variable allows you to access the width of the box like l$rect$w. trace=TRUE and plot=FALSE stop the legend being written to the plot just yet.
ycoord <- -1*(yinch(inchesPerMarLine*buffer)*1.8)
l<-legend(x=par("usr")[1], y=ycoord, inset=c(0,-0.25), legendText, fill=legendColour, horiz=FALSE, bty = "n", ncol=3, trace=TRUE,plot=FALSE)
lx <- mean(par("usr")[1:2]-(l$rect$w/2))
legend(x=lx, y=ycoord, legendText, fill=legendColour, horiz=FALSE, bty = "n", ncol=3)
For completeness, this is how I calculate the number of lines in the legend. Note - the number of columns in the legend is 3. labelSeries is the list of legend labels.
legendLines <- ceiling(nrow(labelSeries)/3)

Shrink y axis width

Is it possible to shrink y-axis? I mean instead of the plot being square, I want it to be rectangular, with y axis shrinked.
library(ggplot2)
data = data.frame(rnorm(10000))
colnames(data) = "numOfX"
m <- ggplot(data, aes(x=numOfX))
m + geom_histogram(colour = "blue", fill = "white", binwidth = 0.5)
last_plot() + opts(aspect.ratio=1/10)
You can adjust the margins by adding the following to the last line of your code:
+ opts(plot.margin = unit(c(1,1,10,1), "lines"))
The numbers are the number of lines to add to the margin in the order c(top, right, bottom, left).
Update: The methods that baptiste and I discussed will change the size of the plot itself, but not the size of the plot area. Just for completeness, if you want to change the aspect ratio of the plot but still have it fill the whole plot area, then you need to change the size of the plot area itself.
On the Mac you can do quartz(width=w, height=h), with width and height in inches. This will open a plot window of the specified size. Then run your original code (without margin or aspect ratio changes). This will give you whatever plot size you wish and the plot will fill the plotting area. You can use dev.off() to close the Quartz window when you're done with it.
You can do the same thing in Windows using this Stack Overflow answer.
Finally, if you're using RStudio, you can do Export-->Copy-to-Clipboard and then adjust the aspect ratio manually.
Of course, you can use a combination of my or baptiste's original answers along with the methods above to control both the size of the plot area and the size of the margins at the same time.

R: gplots, barplots: how to fix bar width independent of paper setting?

I use the gplots package to output barplots. I use it inside a for-loop, so rest of the code is omitted to make it more clear:
library("gplots")
pdf(file = "/Users/Tim/desktop/pgax.pdf", onefile = TRUE, paper = "special")
par(mfrow = c(4,2)) #figures arranged in 2 rows and 2 columns
par(las=2) #perpendicular labels on x-axis
barplot2(expression,ylab = expression(expression),main = graph.header, cex.names =0.85, beside = TRUE, offset = 0, xpd = FALSE,axis.lty = 0, cex.axis = 0.85, plot.ci = TRUE,ci.l = expression - sd.value, ci.u = expression + sd.value, col = colors,width = 1,names.arg = c(etc))
Now when I specify the papersize at a4, and print out in two columns the bars are made so they fill up the full space assigned. If I only have a few bars in each graws, the width is too big compared to the height. I know I should be using xlimit and width = amongst and perhaps even the aspect ratio?, but I can't get the results I wanted. And unconvenient way is to specify the height and width output of the paper, and manually I adjust it for the number of bars in the plots each time. But this doesnt seem appropiate. Does someone know a convenient way to fix width bars in my plots?
All help is much appreciated!
Although bar plots with wide bars can look silly, a wide bar plot with a few narrow bars in it will likely look even sillier. That leaves you specifying the width of the plot (via the width argument to pdf).
It may be prettiest to keep all your plots the same size, in which case you just give width a fixed value. If you do want narrower plots when there are less bars, you need a line of code like
plot_width <- 3 + 0.5 * nlevels(x_variable)

Resources