3

Given a plot where the x axis is discrete, given by a ordered factor, like this plot:

enter image description here

with the levels:

D E F G H I J
1 1 1 2 2 3 4

Is there a way to do:

scale_x_discrete(breaks=c("D","G", "I", "J"), lables=c(1,2,3,4))

without manually copying the breaks?

Hengrui Jiang
  • 861
  • 1
  • 10
  • 23

1 Answers1

0

Is this what you're after?

keeps<-c(1,4,6,7) # Indices for levels you want to show
ggplot(data=diamonds)+
  geom_bar(aes(x=color,y=carat),stat="identity")+
  scale_x_discrete(
    breaks=levels(d$color)[keeps],
    labels=table(d$color)[keeps])
johnson-shuffle
  • 1,023
  • 5
  • 11
  • The problem here is that I have to manually pick the breaks, I.e. c(1,4,6,7) in this example. I referred to it as "manually copying the breaks" in my question. Is there any dynamic way to group the ticks by the levels of a factor? – Hengrui Jiang Jul 13 '15 at 07:17
  • How are you picking the levels? – johnson-shuffle Jul 14 '15 at 13:01
  • The idea is to place the breaks when the level of the factor changes. G would be the first one with level 2, I first one with 3, J first one with 4. Thus D,G,I,J – Hengrui Jiang Jul 17 '15 at 14:40