Add customized labels onto barplots?

How i can add custom labels onto bar plot?
Here is my code:

#appearance
H <- c(6.36, 3.03, 6.85, 4.07, 4.69, 6.27, 6.67, 3.11, 5.07, 6.14, 5.93, 6.49)
par(mar = c(3, 5.5, 0.5, 2))
barplot(H,
        xlab = "",
        ylab = "",
        xlim = c(0, 9),
        names.arg = c("Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3", "Valery",
                      "Rio Colorado", "CO99076-6R", "Purple Majesty", "AC99330-1P/Y",
                      "CO05068-1RU", "Masquerade", "Canela Russet"),
        col = colorRampPalette(c("#6E5773","pink"))(12),
        font.axis = 2,
        cex.names = .7,
        cex.axis = .5,
        horiz = TRUE, las=1, font.lab=1, font.axis=1, border = NA)
abline(v=7, col = "magenta", lty = 5)
text(6.36, "ab", pos = 2)
text(3.03, "e", pos = 2)
text(6.85, "a", pos = 2)
text(4.07, "d", pos = 2)
text(4.69, "cd", pos = 2)
text(6.27, "ab", pos = 2)
text(6.67, "ab", pos = 2)
text(3.11, "e", pos = 2)
text(5.07, "c", pos = 2)
text(6.14, "ab", pos = 2)
text(5.93, "b", pos = 2)
text(6.49, "ab", pos = 2)
axis(side = 1, at = 1:9,
     labels = c("Dislike\nExtremely", "", "3", "", "5", "", "7", "", "Like\nExtremely"),
     cex.axis = .5)

Created on 2020-07-15 by the reprex package (v0.3.0)

How about trying to creating it in ggplot2 instead of base R?

## create data frame
H <- c(6.36, 3.03, 6.85, 4.07, 4.69, 6.27, 6.67, 3.11, 5.07, 6.14, 5.93, 6.49)
names <- c("Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3", "Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty", "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet")

df <- data.frame(H, names)
df

## build plot in ggplot2
library(ggplot2)

ggplot(df, aes(x = H, y = reorder(names, H))) +
	geom_col(fill = "red",
				alpha = 0.5) +
	geom_label(aes(label = H)) +
	theme_minimal()

I think its good, may be next time i shall go for ggplot2. But i am more concerned here that how i can specify x and y coordinate for text function

Continuing from yesterday's post, hopefully got the right order:

H <- c(6.36, 3.03, 6.85, 4.07, 4.69, 6.27, 6.67, 3.11, 5.07, 6.14, 5.93, 6.49)

par(mar = c(3, 9, 2, 2))

y <- barplot(height = H,
        names.arg = c("Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3", "Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty", "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet"),
        horiz = TRUE,
        col = colorRampPalette(colors = c("#6E5773","pink"))(n = length(x = H)),
        border = NA,
        xlab = "",
        ylab = "",
        xlim = c(0, 9),
        axes = FALSE,
        las = 1)

axis(side = 1,
     at = 1:9,
     labels = c("Dislike\nExtremely", "", "3", "", "5", "", "7", "", "Like\nExtremely"),
     font.axis = 1,
     cex.axis = 0.5)

abline(v = 7,
       col = "magenta",
       lty = 5)

text(x = H,
     y = y,
     labels = c("ab", "e", "a", "d", "cd", "ab", "ab", "e", "c", "ab", "b", "ab"),
     pos = 2)

Hope this helps.


Edit (in response to post #5)

If you print y, you'll get the coordinates of the midpoints. Usually these are x-coordinates, but since you are using horizontal bar plot, it'll have y-coordinates.

I didn't follow your second question. I supplied a sequence of texts that I want to display on the plot, along with their desired x and y coordinates. It'll work in a vectorised manner.

Regarding facets, the obvious answer is use ggplot2, if you want that feature. And at least on this community, you'll get more help on that, as you've already been recommended in this and your last thread. If you don't want that, you can use mfrow for base graphics. Or, lattice is another option, though I can't help much on that.

If you cannot make mfrow work, I'll suggest that you create a new thread with a reproducible example, and maybe with an example of your desired result.

Hope this helps.

1 Like

@Yarnabrina Thanks for the help. I am new to R and have few questions - like what i understood from your code that only thing you changed was this:

Can you please explain what actually you did here, and also this, like if i have to put multiple texts, i can use c(" ", " ") this?

Another thing, so suppose if i have many barplots (like i have 6) of the same type i showed in this thread, can i arrange them into 1 (like what multi facet()) do in ggplot. Where y will be same but x will be new barplots.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.