Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal distance among all points on an axis

I am trying to plot a simple scatter plot like this: enter image description here

The minimal code I am using for generating this plot is as follows:

ggplot(fig1c, aes(x=xvar, y=yvar)) +    
  scale_x_log10(breaks=unique(fig1c$xvar)) +
  scale_y_continuous(breaks=seq(0, 10, by=1), labels=as.character(seq(0, 10, by=1))) +   
  geom_point(aes(fill=Method, color=Method, shape=Method), size = 5, guides=FALSE) + 
  scale_colour_brewer(palette="Set1") + 
  geom_line(aes(fill=Method, color=Method)) +

What I would like to achieve is to have all the points on the x axis (5884, 13957, ...) equally spaced. I.e. I want a break between point 5884 and 13957 to be the same size as between 13957 and 21013, and so on. Any help?

Thank you

like image 343
lukas Avatar asked Jun 03 '15 10:06

lukas


2 Answers

You might want to convert your x-values into factors. Right now, R assumes that your x-values are numbers and hence puts the appropriate space between them (the difference between 5,884 and 13,957 is larger than the difference between 21,013 and 28,708). However, you probably think of the numbers as names for your datapoints. If you tell R to treat the numbers as factors or text, it will put equal spacing between them.

ggplot(fig1c, aes(x=as.factor(xvar), y=yvar)) +    
  scale_x_log10(breaks=unique(fig1c$xvar)) +
  scale_y_continuous(breaks=seq(0, 10, by=1), labels=as.character(seq(0, 10, by=1))) +   
  geom_point(aes(fill=Method, color=Method, shape=Method), size = 5, guides=FALSE) + 
  scale_colour_brewer(palette="Set1") + 
  geom_line(aes(fill=Method, color=Method)) +
like image 81
Nadja Simons Avatar answered Sep 18 '22 21:09

Nadja Simons


Your x-values should be transformed to factors for ggplot not to consider them as continuous variables. Furthermore, you should suppress the line:

scale_y_continuous(breaks=seq(0, 10, by=1), labels=as.character(seq(0, 10, by=1)))

What you should have at the end is:

fig1c$xvar <- factor(fig1c$xvar)

p <- ggplot(fig1c, aes(x=xvar, y=yvar)) 
p <- p + geom_point(aes(fill=Method, color=Method, shape=Method), size = 5, guides=FALSE) 
p <- p + scale_colour_brewer(palette="Set1") 
p <- p + geom_line(aes(group=Method, color=Method))
p
like image 20
Pop Avatar answered Sep 19 '22 21:09

Pop