Search code examples
rplotggplot2bar-chartfacet

Add vertical separator and labels to R barplot


I have the following data frame from which I make a bar plot. Then, I am trying to add Group separators with vertical lines and to place labels in between these lines by creating a new x-axis:

Group = c("1_1", "1_2", "1_3", "2_1", "2_2", "3_1", "3_2", "3_3", "3_4")
Value = as.numeric(c("-1.23", "2.34", "0.56", "1.87", "-2.40", "5.54", "-0.98", "-2.31", "6"))
data = data.frame(Group, Value)
data
Group   Value
1_1   -1.23
1_2   2.34
1_3   0.56
2_1   1.87
2_2   -2.40
3_1   5.54
3_2   -0.98
3_3   -2.31
3_4   6.0

barplot(data$Value, xaxs="i",xaxt="n")

I am trying to add vertical lines as Group separators:

. between "1_3" and "2_1"

. between "2_2" and "3_1"

And place labels:

. "1" between origin and first separator

. "2" between first and second separator

. "3" between second separator and end of axis

It works fine with a continuous axis when making a scatter plot for instance, but it does not work with a categorical axis. Is there any way to sort of converting the Group categories to number depending on their position on the plot?


Solution

  • I think this is what you are describing:

    barplot(data$Value, xaxs="i",xaxt="n")
    abline(v = 3.7)
    abline(v = 6.1)
    text(3.7/2, 5.9, label = '1')
    text(4.85, 5.9, label = '2')
    text(8.5, 5.9, label = '3')