0

How can I add a space or break between every second label on the x-axis in ggplot, to introduce some distance between each set of platforms (FB, IG, and TW)?

If I try adding blank rows to the dataframe, ggplot drops them (even after adding drop=FALSE to scale_x_discrete).

I also do not want to fix this by saving the figure larger (because there are more variables than what I include here).

Here is the code:

Platform <- c("FB Left-wing", "FB Right-wing",
              "IG Left-wing", "IG Right-wing",
              "TW Left-wing", "TW Right-wing")

Ideology <- c("Left", "Right", "Left", "Right","Left", "Right")

CI_low <- c(1.049, 0.906, 1.212, 0.989, 1.122, 1.080)
CI_high <- c(1.299, 1.144, 1.483, 1.235, 1.362, 1.335)
CI_mid <- c(1.167, 1.018, 1.340, 1.105, 1.236, 1.201)

dat_figure <- data.frame(Platform, Ideology, CI_low, CI_high, CI_mid)

figure <- ggplot(dat_figure, aes(x = Platform, y = CI_mid, colour = Ideology)) + 
  scale_colour_manual(values = c("blue", "red")) +
  geom_pointrange(aes(ymin = CI_low, ymax = CI_high)) + 
  ylim(0, 4) + 
  ylab("Odds Ratio") + 
  xlab("Platform and Ideology") + 
  theme_bw() 

figure

And the output:

Figure

I would like to add a space between 'FB Right-wing and IG Left-wing', and 'IG Right-wing and TW Left-wing'.

Thanks.

benson23
  • 16,369
  • 9
  • 19
  • 38

2 Answers2

1

This solution isn't exactly what you asked for, but I think it is one way to visualize the structure of data:

library(ggplot2)

Platform <- c("FB Left-wing", "FB Right-wing",
              "IG Left-wing", "IG Right-wing",
              "TW Left-wing", "TW Right-wing")

Party <- c("FB", "FB", "IG", "IG", "TW", "TW")
  
Ideology <- c("Left", "Right", "Left", "Right","Left", "Right")

CI_low <- c(1.049, 0.906, 1.212, 0.989, 1.122, 1.080)
CI_high <- c(1.299, 1.144, 1.483, 1.235, 1.362, 1.335)
CI_mid <- c(1.167, 1.018, 1.340, 1.105, 1.236, 1.201)

dat_figure <- data.frame(Platform, Party, Ideology, CI_low, CI_high, CI_mid)

ggplot(dat_figure, aes(x = Party, y = CI_mid, colour = Ideology)) + 
  scale_colour_manual(values = c("blue", "red")) +
  geom_pointrange(aes(ymin = CI_low, ymax = CI_high), position = position_dodge(0.5)) + 
  ylim(0, 4) + 
  ylab("Odds Ratio") + 
  xlab("Platform and Ideology") + 
  theme_bw() 

enter image description here

Skaqqs
  • 4,010
  • 1
  • 7
  • 21
  • Thanks! This is a neat solution that helps readability. Appreciate it! – Michael Bossetta May 18 '22 at 13:28
  • You're welcome! Here's a similar question that may be helpful to you: https://stackoverflow.com/questions/15472158/how-to-dodge-pointrange-ggplots-on-two-levels – Skaqqs May 18 '22 at 14:02
1

One common way to display categorical data is to facet it. Here the media column is created for the facet.

library(tidyverse)

dat_figure <- dat_figure %>% mutate(media = sub("\\s.*", "", Platform))

ggplot(dat_figure, aes(x = Platform, y = CI_mid, colour = Ideology)) + 
  scale_colour_manual(values = c("blue", "red")) +
  geom_pointrange(aes(ymin = CI_low, ymax = CI_high)) + 
  ylim(0, 4) + 
  ylab("Odds Ratio") + 
  xlab("Platform and Ideology") + 
  theme_bw() +
  facet_wrap(. ~ media, scale = "free")

Created on 2022-05-18 by the reprex package (v2.0.1)

benson23
  • 16,369
  • 9
  • 19
  • 38