1

I'm trying to create a nicely formatted 2-state bump chart in GGPlot2

In the following plot I'd like to reduce the size of the 'white space' between the y axis and the first factor value "old" and increase the size of the space to the right of the second value "new". In the real data my text is full sentences so only the first part is currently being shown.

bump chart with too much white space on left

My code:

old <- data.frame(Group = "old", Rank = 1:5, Text = c("Text1","Text2","Text3","Text4","Text5"))
new <- data.frame(Group = "new", Rank = c(4,2,1,5,3), Text = c("Text1","Text2","Text3","Text4","Text5"))
df <- rbind(old,new)

library(ggplot2)

ggplot(df, aes(x=Group, y= Rank, group =  Text, label = Text)) +
  geom_line() +
  scale_y_reverse() +
  geom_text(data = subset(df, Group == "new"), size=3, hjust=0) 
AndrewMinCH
  • 710
  • 5
  • 8

1 Answers1

3

You can convert your x variable to numeric inside the ggplot() call and then use scale_x_continuous() to modify the axis.

ggplot(df, aes(x=as.numeric(Group), y= Rank, group =  Text, label = Text)) +
  geom_line() +
  scale_y_reverse() +
  geom_text(data = subset(df, Group == "new"), size=3, hjust=0) +
  scale_x_continuous(limits=c(0.95,5),breaks=c(1,2),labels=levels(df$Group),
                     expand=c(0,0))
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201