0

The following example produces a bar graph:

f1 <- c("Con", rep("T1",3), rep("T2",3), rep("T3",3))
f2 <- c("Con", rep(c("A", "B", "C"),3))
y <- rnorm(10)

df <- as.data.frame(cbind(f1,f2,y))

library(ggplot2)

ggplot(df, aes(x = f1, y = y, fill = f2)) +
  geom_bar(stat = "identity", position = position_dodge(width=0.9))

How can I edit the outer left bar (Con) to be as wide as one of the other bars?

Thanks in advance.

Hendrik
  • 321
  • 1
  • 14

2 Answers2

1

Your y-axis values are factors (should not they be numeric?), you can try something like the following (fill all the missing levels of the factors for f1 and f2):

f1 <- c("Con", rep("T1",3), rep("T2",3), rep("T3",3))
f2 <- c("Con", rep(c("A", "B", "C"),3))
y <- rnorm(10)

df <- cbind.data.frame(f1,f2,y)
library(ggplot2)

df <- rbind(df, cbind.data.frame(expand.grid(f1=levels(df$f1),f2=levels(df$f2)), y=NA))

ggplot(df, aes(x = f1, y = y, fill = f2)) +
  geom_bar(stat = "identity", position = position_dodge(width=0.9))

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
0

I do not have the complete answer, since the position now is messed up:

library(dplyr)

df2 <- df %>% group_by(f1) %>% mutate(w = n()/3)

library(ggplot2)

ggplot(df2, aes(x = f1, y = y, fill = f2, width=w)) +
  geom_bar(stat = "identity", position = "dodge")

enter image description here

Edit: with also manual positions you can do this. Only edit the script so the correct labels are added (to the factor)

f1 <- c("Con", rep("T1",3), rep("T2",3), rep("T3",3))
f2 <- c("Con", rep(c("A", "B", "C"),3))
y <- rnorm(10)

df <- as.data.frame(cbind(f1,f2 = factor(f2),y))

library(dplyr)

df2 <- df %>% group_by(f1) %>% mutate(w = 1/3) %>% mutate(pos=as.numeric(f1)+(row_number()-1)/3)


library(ggplot2)

ggplot(df2, aes(x = pos, y = y, fill = f2, width=w)) +
  geom_bar(stat = "identity", position = "dodge")

enter image description here

Wietze314
  • 5,942
  • 2
  • 21
  • 40