Formatting positions on a discrete scale in ggplot2

I am trying to create a nicely formatted 2-state relief diagram in GGPlot2

In the following graph, I would like to reduce the size of the “white space” between the y axis and the value of the first factor “old” and increase the size of the space to the right of the second value “new”. In real data, my text is full sentences, so currently only First part.

bump chart with too much white space on left

My code is:

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) 
+1
source share
1 answer

You can convert the variable x to a numeric number inside the call ggplot(), and then use scale_x_continuous()to change 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))
+3
source

Source: https://habr.com/ru/post/1610478/


All Articles