user4100013 - 4 months ago 19

R Question

When I plot the example below the x-axis labels are confusing since the ticks labeled 2008 and 2012 are 2008.5 and 20012.5. I fiddled and realized that the number format gets truncated because it's so long. What's a good way to floor the ticks and labels?

`library(ggplot2)`

foo <- data.frame(x=2005:2014,y=rnorm(10))

p1 <- ggplot(data = foo, aes(x = x, y = y))

p1 <- p1 + geom_point(size=4)

p1

EDIT: THis is indeed a duplicate question:

How to display only integer values on an axis using ggplot2

My apologies.

Answer

This two options could be an idea....

option 1:

```
foo <- data.frame(x=2005:2014,y=rnorm(10))
p1 <- ggplot(data = foo, aes(x = x, y = y))
p1 <- p1 + geom_point(size=4)
p1 <- p1 + scale_x_continuous(breaks = foo$x)
p1
```

option 2:

```
foo <- data.frame(x=2005:2014,y=rnorm(10))
p1 <- ggplot(data = foo, aes(x = x, y = y))
p1 <- p1 + geom_point(size=4)
p1 <- p1 + scale_x_continuous(breaks = foo$x[seq(1, length(foo$x), 2)])
p1
```