francoiskroll
francoiskroll

Reputation: 1098

R ggplot: stacked barplots go beyond values

In some cases, stacked barplots create bars that go beyond values.

Here is a reproducible example showing the issue:

# creating some dataframe for the example
eg <- matrix(nrow=4, ncol=3)
eg[,1] <- c('a', 'b', 'a', 'b')
eg[,2] <- c('catA', 'catA', 'catB', 'catB')
eg[,3] <- c(0.7, 0.3, 0.8, 0.4)
colnames(eg) <- c('ID', 'type', 'proportion')
eg <- as.data.frame(eg)

# plotting
ggplot (data=eg, aes (x=ID, y=proportion, fill=type)) +
  geom_bar(stat='identity')

enter image description here

It should generate the plot hereabove. In ID a for instance, what I am expecting is the blue bar to go to 0.8, but the pink bar to go from 0.0 to 0.7, not be akwardly added on top with no values or grid. What am I doing wrong?

Upvotes: 1

Views: 49

Answers (2)

StupidWolf
StupidWolf

Reputation: 46898

The problem lies with proportion being coerced into a character matrix

eg <- matrix(nrow=4, ncol=3)
eg[,1] <- c('a', 'b', 'a', 'b')
eg[,2] <- c('catA', 'catA', 'catB', 'catB')
# this part is the problem
eg[,3] <- c(0.7, 0.3, 0.8, 0.4)
colnames(eg) <- c('ID', 'type', 'proportion')
eg <- as.data.frame(eg)
str(eg)

gives

'data.frame': 4 obs. of 3 variables:

$ ID : Factor w/ 2 levels "a","b": 1 2 1 2

$ type : Factor w/ 2 levels "catA","catB": 1 1 2 2

$ proportion: Factor w/ 4 levels "0.3","0.4","0.7",..: 3 1 4 2

you need to have it in the correction format:

eg <- data.frame(
    ID <- c('a', 'b', 'a', 'b'),
    type <- c('catA', 'catA', 'catB', 'catB'),
    proportion <- c(0.7, 0.3, 0.8, 0.4)
    )

ggplot (data=eg, aes (x=ID, y=proportion, fill=type)) +
  geom_bar(stat='identity')

enter image description here

Upvotes: 0

vuzun
vuzun

Reputation: 952

Try adding a position argument in geom_bar like this:

ggplot (data=eg, aes (x=ID, y=proportion, fill=type)) +
  geom_bar(stat='identity', position = position_dodge())

Upvotes: 1

Related Questions