How to label barcharts in Plots.jl? - julia

I can do a simple barplot
using Plots
bar([1,2,3])
but how do I label the bars as "a", "b", "c"?

To put labels on the x axis you can do the following:
bar(["a","b","c"],[1,2,3])
If you wanted to put the labels anywhere else then it does seem like annotate! would be your best option, as juliohm suggested.

Related

Convert the continouos legend of an image.plot to discrete

I have several plots of the abundance of a particular species over my study area. I have been using image plot successfully. But now, I just want to plot presence/absence, so values from 0 to 1, and the plot stills shows me a continuous legend. Is there a way to modify this?
I have tried with image but the legend is not that cool.
This is my code:
colnames(zz)<-c("x","y","z")
ras<-rasterFromXYZ(zz)
asc<-asc.from.raster(ras)
image.plot(asc,
xlim=c(-9.5,-1),ylim=c(43,48),zlim=c(min(asc,na.rm=T),max(asc,na.rm=T)),
ylab="Latitude (ºN)",xlab="Longitude (ºW)",
col = viridis(2, option = "D"))

Labels on grouped bars in barplot() [duplicate]

This question already has an answer here:
Show element values in barplot
(1 answer)
Closed 4 years ago.
I have a barplot with grouped bars. Is it possible to include a label for each bar ? Example of plot without bar labels:
test <- structure(c(0.431031856834624, 0.54498742364355, 0.495317895592119,0.341002949852507, 0.40229990800368, 0.328769657724329,0.258600583090379,0.343181818181818, 0.260619469026549), .Dim = c(3L, 3L), .Dimnames = list(
c("2015", "2016", "2017"), c("a", "b", "c")))
barplot(test,ylim=c(0,1),beside=T)
p <- barplot(test, ylim=c(0, 1), beside=T)
text(p, test + .05*sign(test), labels=format(round(test, digits=2), nsmall=2))
The last line adds the labeling over the bar plots.
p takes the return values of the barplot() which are the x-axis bar positions.
In this example this is of the format 3x3 matrix.
text() needs then p for his x= argument. And for his y= argument it needs a slightly offsetted value than its bar plot heights (test). sign() determines the direction (above or below, +1 or -1) of the bar and .05 I determined empirically by trying, it is dependent on your values of the table.
So, x= and y= are the x and y coordinates for the labeling.
And finally, labels= determines which text should be printed.
The combination of format() and round() gives you full control over how many digits you want to display and that the display is absolutely regular in turns of number of digits displayed, which is not, if you use only round().
With xpd=T you could determine, whether labeling is allowed to go outside of region or not.
cex= could determine the fontsize of the label,
col= the colouring and font= the font.
alternatively, you can give just test for y= and determine via pos=3 that it should be above and offset=1 how many characterwidths the offset of the text shoul be.
p <- barplot(test, ylim=c(0, 1), beside=T)
text(x=p, y=test, pos=3, offset=1, labels=format(round(test, digits=2), nsmall=2))
You can find plenty of more instructions by looking into the documentation by
?text
# and
?barplot
in the R console
You can add a label using text function by extending your barplot. You can play with the parameters as you wish. Here is the sample code and its output.
x= barplot(test,ylim=c(0,1),beside=T)
text(x, test, labels=test, pos=1, offset=.5, col="red", srt = 90) #srt is used for vertical labels
If you really want to make a better plot, I would recommend ggplot as it has several other features like adding a theme to your plots and it is more easy for customizations.
If you're looking to label ever bar's category not it's value you can do something like this
allPermutations <- unlist(lapply(colnames(test), function(x) paste(x, rownames(test)) ))
barplot(test,ylim=c(0,1),beside=T, names.arg = allPermutations, las=2)
the file line gets all the combinations of categories. The plot call allows you to specify individual values with "names.arg" while las=2 rotates the names so it shows a bit nicer

Omitting some legends in ggplot2

Is there a way to partially suppress legend in ggplot2 ? For example for chart below
There are way too many colours in the legend. Let's say I still want to display all colours, but only want to show the legend for letters b to e. Is there any way to do so?
Keeping in mind the comments above, you may use the following solution. The linked answer proposes changing limits, but another possibility is to override breaks. Here's how:
let <- letters[1:20]
let_be <- let
p <- qplot(1:20, 1:20, colour = let)
let_be[!(let %in% c("b", "e"))] <- NA
p + scale_color_discrete(breaks = let_be)

how to add two line different labels using grid.xaxis in grid package of R

Goal: plot a fig with the x-axis owing two line labels using grid.
such as one line is c("A", "B", "C"),
the other line is c("1", "2", "3").
Maybe how to set the edits parameter?
Also how to add an annotation after each labels, such as the unit if could?
Thank you.
Something like this?
library(grid)
grid.newpage()
pushViewport(viewport(y=1, width=0.8))
grid.xaxis(at = seq(0,1,by=0.5), label = paste(letters[1:3],1:3,sep="\n"))

plot numeric vector with names as x ticks

I have a numeric vector and wish to plot each value on y-axis by their name on the x-axis.
Example:
quantity <- c(3,5,2)
names(quantity) <- c("apples","bananas", "pears")
plot(quantity)
Each value is plotted with it's index number along the x-axis ie. 1,2,3. How can I get it to show ("apples","bananas", "pears")?
You can use function axis() to add labels. Argument xaxt="n" inside plot() will make plot without x axis labels (numbers).
plot(quantity,xaxt="n")
axis(1,at=1:3,labels=names(quantity))
Do you look for barplot?
barplot(quantity)
And another option using lattice:
library(lattice)
barchart(quantity)
I had this same problem so I found this question, but once I looked at the answers and saw you could use names(named.vector) to get the names from the named.vector. Then I tried this and it worked.
plot(x = quantity, y = names(quantity))
I feel this is cleaner and simpler than a lot of the answers on this question. Even the one that was accepted.
You can use either barplot or ggplot2 and have a graph of the following
quantity <- c(3, 5, 2)
names(quantity) <- c("apples", "banans", "pears")
barplot(quantity, main="Fruit Names vs. Quantity", xlab = "Names", ylab="Quantity", col=c("blue", "red", "yellow"))
legend("topright", legend=c("apples", "banas", "pears"), fill=c("blue", "red", "yellow"))

Resources