How do I label my bar plots in R? - r

I have created my bar plot in R and am moving on from using two-bar plots to three-bars.
I want to label the bars '1','2' and '3'along the x-axis
I understand how to use the axis function, and also that the 'at' function requires a numeric value or vector, it's just thatI don't know exactly what value it is that it requires!
Thanks very much in advance

Great news! You can use whatever values you like!
mat <- matrix(c(14,9,7))
barplot(mat[ ,1])
axis(1, at = c(.5,1.5,3.5), labels = 1:3, tick = FALSE, las = 2)

Related

Why is `ann=FALSE` not working in the boxplot call in R?

Trying to produce both a stripchart and a boxplot of the same (transformed) data but (because the boxplot is shifted down a tad) I don't want the axis labels twice:
set.seed(3121975)
bee = list(x1=rnbinom(50, mu = 4, size = .1),
x2=rnbinom(30,mu=6,size=.1),
x3=rnbinom(40,mu=2,size=.1))
f = function(x) asinh(sqrt(4*x+1.5))
stripchart(lapply(bee,f),method="stack",offset=.13,ylim=c(.8,3.9))
boxplot(lapply(bee,f),horizontal=TRUE,boxwex=.05,at=(1:3)-.1,add=TRUE,ann=FALSE)
Other things that don't work include: (i) leaving ann to take its default value of !add, (ii) specifying labels for ylab.
I presume I have missed something obvious but I am not seeing what it might be.
Just add yaxt = 'n' into boxplot() to suppress plotting of the y-axis. The argument ann controls axis titles and overall titles, not the axis itself.

How can I add labels in a plot where I've used the function `plot` and `points`?

How can I add labels in a plot where I've used the function plot and points?
I have the following code
x<-seq(0,1,.01)
alpha=2
beta=3
y<-dbeta(x,alpha,beta)
plot(x,y,ylim=c(0,5.9))
n=10
y1<-dbeta(x,alpha+x,beta+n-x)
points(x,y1)
And the display is
I would like to indicate with labels this is prior and this is posterior .Also indicating which parameters are being used.
I know how to add labels when you only use plot(x,y,xlab="y axis")
but not when combined with points also this form of labeled would not be that clear in the plot.
The labels not in the usual form as is x and y labels in the plot, but indicating inside the plot, this is the prior and this the posterior.
Could you please help?
Thank you in advance.
By the comment of #Chase,
Using the function text().
Example:
text(x = .2, y = 5, label = "Posterior distribution").

How to save information from barplot() and not displaying the graph

I have a small question:
let's assume I want to assign the positions of the x ticks of a barplot to a variable for plots later on, however the barplot should not be displayed in the graphical device. How can I assign without plotting?
mat = matrix(ncol=5,nrow=3,rnorm(n = 15))
mids = barplot(mat)
Thank you very much for your time and help!
Cheers,
tokami
For barplot, you can provide the plot = FALSE parameter:
mids = barplot(mat, plot = FALSE)

R histogram-reducing y range

I'm trying to draw a graph looks like below using r, and was wondering if there is a way to
1) omit the lower range of y values, but still start from 0.
2) Also, instead of numerical values, how do I label each bar in the histogram with texts?
I would appreciate any help. Thanks!
The worth-a-read answer in the comments was using the same example of plotrix::gap.barplot that I picked but I've been working on those "squiggly lines":
require(plotrix)
twogrp<-c(rnorm(10)+4,rnorm(10)+20)
gap.barplot(twogrp, gap=c(8,16), xlab="Index", ytics=c(3,6,17,20),
ylab="Group values", main="Barplot with gap")
polygon(y=c( 7.5+c(-1,1)*.2*rep(1,length(twogrp)+2),
8.5+ c(-1,1)*.2*rep(1,length(twogrp)+2) ) ,
x=c(0,seq_along(twogrp), rep(length(twogrp)+1, 2), # going to the right...
rev(seq_along(twogrp)) ,0) , # and coming back to the left
col="white", border="white") # could also try border="lightblue"
There is also an axis.break function in plotrix that will give you the annotation on the axis. You would use the text function for labels inside the plot area.

How can I make my vertical labels fit within my plotting window?

I'm creating a histogram in R which displays the frequency of several events in a vector. Each event is represented by an integer in the range [1, 9]. I'm displaying the label for each count vertically below the chart. Here's the code:
hist(vector, axes = FALSE, breaks = chartBreaks)
axis(1, at = tickMarks, labels = eventTypes, las = 2, tick = FALSE)
Unfortunately, the labels are too long, so they are cut off by the bottom of the window. How can I make them visible? Am I even using the right chart?
Look at help(par), in particular fields mar (for the margin) and oma (for outer margin).
It may be as simple as
par(mar=c(5,3,1,1)) # extra large bottom margin
hist(vector, axes = FALSE, breaks = chartBreaks)
axis(1, at = tickMarks, labels = eventTypes, las = 2, tick = FALSE)
This doesn't sound like a job for a histogram - the event is not a continuous variable. A barplot or dotplot may be more suitable.
Some dummy data
set.seed(123)
vec <- sample(1:9, 100, replace = TRUE)
vec <- factor(vec, labels = paste("My long event name", 1:9))
A barplot is produced via the barplot() function - we provide it the counts of each event using the table() function for convenience. Here we need to rotate labels using las = 2 and create some extra space of the labels in the margin
## lots of extra space in the margin for side 1
op <- par(mar = c(10,4,4,2) + 0.1)
barplot(table(vec), las = 2)
par(op) ## reset
A dotplot is produced via function dotchart() and has the added convenience of sorting out the plot margins for us
dotchart(table(vec))
The dotplot has the advantage over the barplot of using much less ink to display the same information and focuses on the differences in counts across groups rather than the magnitudes of the counts.
Note how I've set the data up as a factor. This allows us to store the event labels as the labels for the factor - thus automating the labelling of the axes in the plots. It also is a natural way of storing data like I understand you to have.
Perhaps adding \n into your labels so they will wrap onto 2 lines? It's not optimal, but it may work.
You might want to look at this post from Cross Validated

Resources