format X-axis of bars in ggplot2

Asked

Viewed 756 times

0

I generated a bar graph in ggplot2 , but the X axis data was not in the number sequence of "1 - 30", I would like to know how to get the sequence right?

inserir a descrição da imagem aqui

here are the Scripts

c<-  c ("Casal 1", "Casal 2", "Casal 3", "Casal 4", "Casal 5", "Casal 6", 
                       "Casal 7", "Casal 8", "Casal 9", "Casal 10", "Casal 11", "Casal 12",
                       "Casal 13", "Casal 14", "Casal 15", "Casal 16", "Casal 17", "Casal 18",
                       "Casal 19", "Casal 20", "Casal 21", "Casal 22", "Casal 23", "Casal 24", 
                       "Casal 25", "Casal 26", "Casal 28", "Casal 29", "Casal 30")
r <-  c (1.0, 0.5, 0.7, 0.3,
0.5, 0.4, 0.5, 0.6, 0.7, 0.7, 0.7, 0.5, 1.0, 0.7, 0.4, 0.6,
0.6, 0.6, 1.0, 0.9, 0.5, 0.7, 0.6, 0.0, 0.6, 1.0, 0.6, 0.8, 0.5)

R<- data.frame(c,r)
require(ggplot2)
ggplot(R, aes(c,r))+ geom_bar(stat="identity")+
  theme_classic()+theme_light()+
  theme(axis.text.x = element_text(angle = 75, vjust = .5)) 

1 answer

1

You need to sort the column as a factor, indicating the required order.

c<-  c ("Casal 1", "Casal 2", "Casal 3", "Casal 4", "Casal 5", "Casal 6", 
        "Casal 7", "Casal 8", "Casal 9", "Casal 10", "Casal 11", "Casal 12",
        "Casal 13", "Casal 14", "Casal 15", "Casal 16", "Casal 17", "Casal 18",
        "Casal 19", "Casal 20", "Casal 21", "Casal 22", "Casal 23", "Casal 24", 
        "Casal 25", "Casal 26", "Casal 28", "Casal 29", "Casal 30")
r <-  c (1.0, 0.5, 0.7, 0.3,
         0.5, 0.4, 0.5, 0.6, 0.7, 0.7, 0.7, 0.5, 1.0, 0.7, 0.4, 0.6,
         0.6, 0.6, 1.0, 0.9, 0.5, 0.7, 0.6, 0.0, 0.6, 1.0, 0.6, 0.8, 0.5)

R<- data.frame(c,r)

### Ordenando coluna c
levels(R$c)
R$c = factor(R$c, levels = c)

library("ggplot2")
ggplot(R, aes(c,r))+ geom_bar(stat="identity")+
  theme_classic()+theme_light()+
  theme(axis.text.x = element_text(angle = 75, vjust = .5)) 

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.