acalcino
acalcino

Reputation: 315

How to plot line graph with repetitive x axis lables in ggplot2

I have a dataset in which the x axis labels are not unique and are non-numberic. Here's a sample:

0   )
0   )
0   .
0   )
1   )
420 )
474 )
518 )
567 )
580 )

When I plot this with ggplot2 using this code:

ggplot(data=Figure3b, aes(x=Bracket, y=Counts)) + geom_line()

I get this:

ggplot2_output

Whereas I am after something like this:

desired_output

As far as I can tell, it's somehow grouping all the values with the same x axis label whereas I want it to just plot the points in order.

Upvotes: 2

Views: 177

Answers (1)

Rorschach
Rorschach

Reputation: 32426

Assign an index to your parens and then plot

dat$index <- 1:nrow(dat)
ggplot(dat, aes(index, x)) + geom_line(lwd=2, col="blue") + 
    scale_x_discrete(labels=dat$y) + xlab("brackets")

Data looks like:

> head(dat)
#        x y index
#    1   0 )     1
#    2   0 .     2
#    3   0 )     3
#    4   1 )     4
#    5 420 )     5
#    6 474 )     6

enter image description here

Upvotes: 2

Related Questions