Miha
Miha

Reputation: 2884

Plot average of values in single plot

I want to plot single bar in a graph so it would look like picture below

enter image description here

I created test data and calculate its mean.

value <- c(99,44,100,120,88)
value_mean <- mean(value)

And plot them using below code

barplot(value_mean, horiz=T, width=30, cex.names=0.5 ,ylim=c(0,200), col="red")

Buth the output is not even close.

enter image description here

I've also looked at this links Single bar barchart in ggplot2, R R Barplot with one bar - how to plot correctly

So my output should be something like the first picture. I was thinking that could solve ggplot.

Upvotes: 1

Views: 266

Answers (1)

lukeA
lukeA

Reputation: 54277

If everything else fails, you can draw a rectangle like this:

par(mar = c(12, 2, 12, 2))
plot(0, type="n", ylim=c(-1, 1), xlim=c(0, 200), axes = F, bty = "n",ylab="", xlab="label")
rect(0, -.7, value_mean, .7, col="red", border=NA)
text(value_mean, 0, pos=4, label=value_mean)
axis(1, at=seq(0, 200, by=40))

enter image description here

Upvotes: 2

Related Questions