1

My data looks like this:

structure(list(Answer = structure(c(4L, 3L, 1L, 2L, 4L, 3L, 1L
), .Label = c("Unsure", "Project-dependent", "No", "Yes"), class = "factor"), 
    Monitoring = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Pre-restortation", 
    "Post-restoration"), class = "factor"), Percent = c(32L, 
    55L, 9L, 4L, 77L, 19L, 2L)), row.names = c(NA, -7L), class = "data.frame")

I've plotted it using ggplot2

ggplot(d1, aes(x=Monitoring, y=Percent)) + 
  geom_bar(stat = "identity", position = position_dodge2(width = 0.9, preserve = "single"), aes(fill = d1$Answer), width = 0.7, color="black") + 
  scale_x_discrete(limits = rev(levels(d1$Monitoring))) +
  scale_y_continuous(name = "Percentage of respondents (%)") +
  scale_fill_manual(values=cols) + theme(panel.grid.major.x = elemdent_blank()) +
  coord_flip() + 
  geom_text(aes(label = Percent, group = Answer),hjust=-0.2, position = position_dodge2(0.7,preserve="single"), size=2.7)

plot

My problem is that I want the Answer levels that are currently listed in the legend, shown on the x-axis (y-axis when flipped) as axis labels for each bar

Fbj9506
  • 231
  • 3
  • 11

1 Answers1

1

Here you go. Map Answer to aes and then facet by Monitoring

library(ggplot2)

d1$Monitoring <- factor(d1$Monitoring, 
                        levels = c("Pre-restortation", "Post-restoration"))

ggplot(d1, aes(x = Answer, y = Percent)) +
  geom_bar(aes(fill = Answer),
           stat = "identity", position = position_dodge2(width = 0.9, preserve = "single"),
           width = 0.7, color = "black") +
  facet_grid(Monitoring ~ ., space = 'free', scales = 'free') +
  scale_y_continuous(name = "Percentage of respondents (%)") +
  scale_fill_brewer(palette = 'Paired') +
  theme_minimal(base_size = 14) +
  theme(legend.position = 'none',
        strip.text = element_text(face = 'bold', vjust = 1)) +
  theme(panel.grid.major.x = element_blank(),
        axis.text.x = element_blank()) +
  coord_flip() +
  geom_text(aes(label = Percent, group = Answer),
            hjust = -0.2, 
            position = position_dodge2(0.7, preserve = "single"), size = 4)

Move strip labels to the left

ggplot(d1, aes(x = Answer, y = Percent)) +
  geom_bar(aes(fill = Answer),
           stat = "identity", position = position_dodge2(width = 0.9, preserve = "single"),
           width = 0.7, color = "black") +
  facet_grid(Monitoring ~ ., space = 'free', scales = 'free', switch = 'y') +
  scale_y_continuous(name = "Percentage of respondents (%)") +
  scale_fill_brewer(palette = 'Paired') +
  theme_minimal(base_size = 14) +
  theme(legend.position = 'none',
        strip.text = element_text(face = 'bold', vjust = 1),
        strip.placement = 'outside') +
  theme(panel.grid.major.x = element_blank(),
        axis.text.x = element_blank()) +
  coord_flip() +
  geom_text(aes(label = Percent, group = Answer),
            hjust = -0.2, 
            position = position_dodge2(0.7, preserve = "single"), size = 4)

Created on 2019-04-08 by the reprex package (v0.2.1.9000)

Tung
  • 26,371
  • 7
  • 91
  • 115
  • Probably better if sorted by values within each facet https://stackoverflow.com/a/52214383/786542 – Tung Apr 09 '19 at 03:07