How can I view the exact values I generated in my Barplot/plot?

Viewed 71

So I have a Barplot using the basic barplot function. But I want to know the exact y values from the plot I generated. Is there a way to do this? This is my graph:

enter image description here

1 Answers

You didn't provide data so I cant help you specifically, but you can use the text() function to add values in base R. You can try playing around with your specific data to fit your needs, or share your data and I can modify the answer.

set.seed(123)
data <- data.frame(name = letters[1:5],
                   value = sample(1:100,5))

a <- barplot(height=data$value, names=data$name)
text(a, data$value/2, labels = data$value)

enter image description here

Or if you have a stacked barplot like in your image:

data <- data.frame(A = 1:5,        # Create example data
                   B = 6:2,
                   C = 3,
                   D = c(3, 5, 1, 1, 2))


cumdata <- apply(data, 2, cumsum)

a <- barplot(as.matrix(data))

text(unlist(data), 
     x = rep(a, each = nrow(cumdata)), 
     y = unlist(cumdata-data/2))

enter image description here

Related