7

New to R and trying to figure out the barplot.
I am trying to create a barplot in R that displays data from 2 columns that are grouped by a third column.

DataFrame Name: SprintTotalHours

Columns with data:

OriginalEstimate,TimeSpent,Sprint
178,471.5,16.6.1
210,226,16.6.2
240,195,16.6.3

I want a barplot that shows the OriginalEstimate next to the TimeSpent for each sprint. I tried this but I am not getting what I want:

colours = c("red","blue")

barplot(as.matrix(SprintTotalHours),main='Hours By Sprint',ylab='Hours', xlab='Sprint' ,beside = TRUE, col=colours)

abline(h=200)

I would like to use base graphics but if it can't be done then I am not opposed to installing a package if necessary.

Sweet Paint Skills

0

3 Answers 3

13

Using base R :

DF  <- read.csv(text=
"OriginalEstimate,TimeSpent,Sprint
178,471.5,16.6.1
210,226,16.6.2
240,195,16.6.3")

# prepare the matrix for barplot
# note that we exclude the 3rd column and we transpose the data
mx <- t(as.matrix(DF[-3]))
colnames(mx) <- DF$Sprint

colours = c("red","blue")
# note the use of ylim to give 30% space for the legend
barplot(mx,main='Hours By Sprint',ylab='Hours', xlab='Sprint',beside = TRUE, 
        col=colours, ylim=c(0,max(mx)*1.3))
# to add a box around the plot
box()

# add a legend
legend('topright',fill=colours,legend=c('OriginalEstimate','TimeSpent'))

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Great answer, works as expected and love the notes. I used a combination of this answer and the one from bgoldst to get the look I wanted. Ultimately choose bgoldst because he matched the look and feel of my paint example. However, I like the look of this one better so I took some code from here too. Thanks!
Glad to be of help :)
7

plot

cols <- c('red','blue');
ylim <- c(0,max(SprintTotalHours[c('OriginalEstimate','TimeSpent')])*1.8);
par(lwd=6);
barplot(
    t(SprintTotalHours[c('OriginalEstimate','TimeSpent')]),
    beside=T,
    ylim=ylim,
    border=cols,
    col='white',
    names.arg=SprintTotalHours$Sprint,
    xlab='Sprint',
    ylab='Hours',
    legend.text=c('Estimated','TimeSpent'),
    args.legend=list(text.col=cols,col=cols,border=cols,bty='n')
);
box();

Data

SprintTotalHours <- data.frame(OriginalEstimate=c(178L,210L,240L),TimeSpent=c(471.5,226,
195),Sprint=c('16.6.1','16.6.2','16.6.3'),stringsAsFactors=F);

1 Comment

I took a hybrid of this answer and the one from digEmAll to get the look I wanted. This is ultimately my choice because it looked just like my crappy paint drawing, so requirements met! However, I used code from digEmAll to make it look more like his, classic example of not knowing what I want :) Thanks!
5

You need to melt to long form so you can group. While you can do this in base R, not many people do, though there are a variety of package options (here tidyr). Again, ggplot2 gives you better results with less work, and is the way most people will end up plotting:

library(tidyr)
library(ggplot2)

ggplot(data = SprintTotalHours %>% gather(Variable, Hours, -Sprint), 
       aes(x = Sprint, y = Hours, fill = Variable)) + 
    geom_bar(stat = 'identity', position = 'dodge')

ggplot

Use base R if you prefer, but this approach (more or less) is the conventional approach at this point.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.