elynagh
elynagh

Reputation: 95

ggplot grouped geom_bar - adding labels of factor categories to the x-axis label

I have this R code to produce a vertical grouped bar chart:

library(ggplot2)
exampledf <- data.frame(
  subchar = c("facebook", "twitter", "snapchat", "male", "female"),
  superchar = c("social media", "social media", "social media", "gender", "gender"),
  cweight = c(.2, .4, .4, .7, .3)
)

ggplot(exampledf, aes(x = superchar, y = cweight, fill = subchar)) +
  geom_bar(stat='identity', position = position_dodge()) +
  #scale_fill_manual(values = c(col.tint(color_in, .7), col.tint(color_in, .8), col.tint(color_in, .9), col.tint(color_in, 1))) +
  scale_fill_discrete()+
  coord_flip() +
  theme_minimal() +
  geom_text(aes(label = signif(cweight, digits=3)), position=position_dodge(width=0.9), hjust=.5, size=2.5)+
  theme(
    legend.position="none",
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.y=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )

However I would like to add the 'subchar' labels to here on the graph (to the right of the 'superchar' label, but to the left of the bars): Image of graph with location

Is there a way to do this?

Upvotes: 4

Views: 2153

Answers (2)

eipi10
eipi10

Reputation: 93761

Use faceting for superchar and make subchar the x-axis aesthetic:

ggplot(exampledf, aes(x=subchar, y = cweight, fill = subchar)) +
  geom_col() +
  geom_text(aes(label = signif(cweight, digits=3)), 
            position=position_stack(vjust=0.5), 
            size=3, colour="white")+
  theme_void() +
  theme(
    axis.text.y=element_text(size=10),
    strip.placement="outside",
    strip.text.y=element_text(angle=180, hjust=1, face="bold", size=11,
                              margin=margin(r=10))
  ) +
  coord_flip() +
  facet_grid(superchar ~ ., scales="free_y", space="free_y", switch="y") +
  scale_y_continuous(expand=c(0,0)) +
  guides(fill=FALSE)

enter image description here

Upvotes: 4

Calum You
Calum You

Reputation: 15062

A hacky method that requires you to rotate the labels to see the whole thing can be done with geom_text. I'd only use this if you already need to facet on some other variable.

library(ggplot2)
exampledf <- data.frame(
  subchar = c("facebook", "twitter", "snapchat", "male", "female"),
  superchar = c("social media", "social media", "social media", "gender", "gender"),
  cweight = c(.2, .4, .4, .7, .3)
)

ggplot(exampledf, aes(x = superchar, y = cweight, fill = subchar)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  coord_flip() +
  theme_minimal() +
  geom_text(
    aes(label = signif(cweight, digits = 3)),
    position = position_dodge(width = 0.9),
    hjust = .5, size = 2.5
  ) +
  geom_text(
    aes(label = subchar, y = 0),
    position = position_dodge(width = 0.9),
    vjust = 1.5, size = 2.5, angle = -90
  ) +
  theme(
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.y = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )

Created on 2019-09-19 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions