jsguy
jsguy

Reputation: 2179

R ggplot2: Can't change the names of the values in x axis without ruining the graph

Asssume this is the dataset:

n   value
100000  20
200000  30
300000  25
400000  40
500000  12

Here is the code that creates the plot:

require(ggplot2)
data <- read.table("test", sep = "\t", header = TRUE,)
ggplot(data, aes(n, value)) + 
geom_point(aes(n,value)) + geom_line(aes(n,value)) + ylim(0,60)+ 
dev.off()

enter image description here

I would like to make the values on the x axis be like this: 100k, 200k, 300k, 400k, 500k. I have tried the following code:

require(ggplot2)
data <- read.table("test", sep = "\t", header = TRUE,)
ggplot(data, aes(n, value)) + 
geom_point(aes(n,value)) + 
geom_line(aes(n,value)) + 
ylim(0,60)+
scale_x_discrete(limit=c(100000,200000,300000,400000,500000), 
labels=c("100k","200k","300k","400k","500k"))
dev.off()

The values of the x axis change nicely, but the left most point can barely be seen, same for the right most point:

enter image description here

Is it possible to fix this problem? I would like the graph to look like in the first picture but only with the names on the axis axis changed. Note that this is a small dataset, so I could probably change the values inside the dataset by hand, my actual dataset is very large which makes this approach impossible.

Upvotes: 0

Views: 540

Answers (1)

neilfws
neilfws

Reputation: 33782

Use scale_x_continuous, not scale_x_discrete:

+ scale_x_continuous(limits = c(100000, 500000),
                     labels = c("100k","200k","300k","400k","500k"))

Upvotes: 2

Related Questions