Add error bars to a barplot

Stefan

I have two vectors. I want to make a barplot of the first vector (simple enough, right). The twist is that every element of the second vector is the standard deviation for every element of the first vector (which itself is the average of 4 other values). How can I do that?

The vectors in question:

-4.6521175 0.145839723
 1.1744100 0.342278694
-0.2581400 0.003776341
-0.3452675 0.073241199
-2.3823650 0.095008502
 0.5625125 0.021627196

I.e., how can I add the elements of the second column vector as error bars to the corresponding elements in the first column vector?

enter image description here

Note: Before you ask, yes I did search extensively on this site and did a lot of googling, but my problem is a bit more specific, i.e. what I found didn't match what I needed.

Jaap

An implementation with geom_bar and geom_errorbar of ggplot2:

library(ggplot2)
ggplot(df, aes(x=row.names(df), y=V1)) +
  geom_bar(stat="identity", fill="grey") +
  geom_errorbar(aes(ymin = V1 - V2, ymax = V1 + V2), width=0.6) +
  theme_classic() 

this results in:

enter image description here

If you want to remove the numbers on the x-axis, you can add:

  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

to your ggplot code.


Used data:

df <- read.table(text="-4.6521175 0.145839723
 1.1744100 0.342278694
-0.2581400 0.003776341
-0.3452675 0.073241199
-2.3823650 0.095008502
 0.5625125 0.021627196", header=FALSE)

In response to your comment, two possible solution when you want plot such a large number of bars:

1: Only include a selection of the axis-labels:

ggplot(df2, aes(x=as.numeric(row.names(df2)), y=V1)) +
  geom_bar(stat="identity", fill="grey", width=0.7) +
  geom_errorbar(aes(ymin = V1 - V2, ymax = V1 + V2), width=0.5) +
  scale_x_continuous(breaks=c(1,seq(10,200,10)), expand=c(0,0)) +
  theme_classic() +
  theme(axis.text.x=element_text(size = 6, angle = 90, vjust = 0.5))

this gives:

enter image description here

As can be seen, it is not ideal to cram so many bars in a plot. See therefore alternative 2.

2: Create a grouping variable which you can use for creating facets:

df2$id <- rep(letters[1:20], each=10)

ggplot(df2, aes(x=as.numeric(row.names(df2)), y=V1)) +
  geom_bar(stat="identity", fill="grey", width=0.7) +
  geom_errorbar(aes(ymin = V1 - V2, ymax = V1 + V2), width=0.5) +
  scale_x_continuous(breaks=as.numeric(row.names(df2))) +
  facet_wrap(~ id, scales = "free_x") +
  theme_bw() +
  theme(axis.text.x=element_text(angle = 90, vjust = 0.5))

this gives:

enter image description here

Used data for the two last examples:

df2 <- data.frame(V1=sample(df$V1, 200, replace=TRUE),
                  V2=sample(df$V2, 200, replace=TRUE))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple error bars add to columns in error on barplot in ggplot2

From Dev

Aesthetics of barplot bars and error bars in seaborn

From Dev

Error bars not plotting correctly on barplot

From Dev

Grouped barplot in R with error bars

From Dev

Error bars not plotting correctly on barplot

From Dev

Error bars for barplot only in one direction

From Dev

Issue in Error bars in Seaborn barplot - Python

From Dev

Add shadow effect ggplot2 bars (barplot)

From Dev

Placement of error bars in barplot using ggplot2

From Dev

Ordering bars in barplot()

From Dev

barplot plot extra bars

From Dev

Ordering bars in barplot()

From Dev

barplot plot extra bars

From Dev

how to add error bars to histogram diagram in python

From Dev

python seaborn barplot bars not centered?

From Dev

barplot bars in specified interval width

From Dev

get equal width bars in a barplot

From Dev

get equal width bars in a barplot

From Dev

How to make the error bars move with the barplot when re-ordering factors?

From Dev

Order and color of bars in ggplot2 barplot

From Dev

Seaborn barplot with 2 bars with the same label

From Dev

get the bars in barplot to have the same height

From Dev

R barplot different color for certain bars

From Dev

How to get the x-coordinate of bars in barplot

From Dev

Re-ordering bars in R's barplot()

From Dev

How to get the x-coordinate of bars in barplot

From Dev

R barplot different color for certain bars

From Dev

Re-ordering bars in R's barplot()

From Dev

Remove spaces between bars in barplot 2 factors