3

I am new to R. I am having trouble with the ordering of the geom_text elements in the below geom_col chart.

I believe it has something to do with the position = position_dodge2(preserve = "single") line, but I am not sure.

Sample code and outputs attached. As you can see, the labels are wrong - e and b should be switched, as well as a and d.

Can someone with sharper eyes (and probably a sharper mind) see what the issue is?

enter image description here

library(ggplot2)
library(stringr)

data <- data.frame(Importance = c("Not important", "Important", "Critical", "Not important", "Important"), Current.Capability = c("Basic", "Undeveloped", "Advanced", "World class", "World class"), Function = c("PM", "Sales", "PM", "Marketing", "Marketing"), Item = c("a","b", "c", "d", "e"))

str(data)
head(data)

width <- 2
position.width <- width - 0.05

ggplot(data, aes(x = Importance, y = Current.Capability, fill=Function)) +
  geom_col(position = position_dodge2(preserve = "single"), width = width) +
  facet_grid(~Importance, scales = "free_x", space = "free_x") +
  geom_text(
    aes(
    label = stringr::str_wrap(Item, 50)), 
    lineheight = 0.7, 
    angle=90, 
    size = 5, 
    hjust=0, 
    vjust=0.5,
    y = 0.5,
    position = position_dodge2(preserve = "single", width = position.width)) +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  theme(legend.position="bottom")+ 
  scale_y_discrete(limits = c("Undeveloped", "Basic", "Advanced", "World class")) +
  xlab("Importance") + ylab("Current Capability")
markus
  • 25,843
  • 5
  • 39
  • 58

1 Answers1

1

Nice work. Maybe you can try to add group = Importance in the aesthetics of geom_text. That is, to "explicitly define the grouping structure", see grouping. Also, here is a related case.

ggplot(data, aes(x = Importance, y = Current.Capability, fill=Function)) +
  geom_col(position = position_dodge2(preserve = "single"), width = width) +
  facet_grid(~Importance, scales = "free_x", space = "free_x") +
  geom_text(
    aes(
      label = stringr::str_wrap(Item, 50),
      group = Importance), # explicitly define the grouping structure
    lineheight = 0.7, 
    angle=90, 
    size = 5, 
    hjust=0, 
    vjust=0.5,
    y = 0.5,
    position = position_dodge2(preserve = "single", width = position.width)) +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  theme(legend.position="bottom")+ 
  scale_y_discrete(limits = c("Undeveloped", "Basic", "Advanced", "World class")) +
  xlab("Importance") + ylab("Current Capability")

enter image description here

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
  • Thank you! The problem was indeed related to grouping. I solved it by including in the aes for geom_col, however, not geom_text. But you put me on the right track! – Brevan D'Angelo Dec 11 '18 at 23:50
  • Hi @BrevanD'Angelo! Cool! You can also include `group = Importance` in the `aes` of `ggplot` directly, like `ggplot(data, aes(..., group = Importance))` and will also work. – Valentin_Ștefan Dec 12 '18 at 09:37