Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set axis breaks to only a part of the axis?

In R, I plotted the following histogram. The problem is on the X axis. The majority of data fall into the interval [0, 10]. Very few have an X value larger than 10, although the largest one is 34.

Therefore, instead of displaying 0, 1, 2, ... all the way till 34 on the X axis, I would to display 0, 1, 2, ..., 10, 15, 20, 25, 30. In other words, when X > 10, only show the labels at an interval of 5, so that the labels won't overlap.

example figure

Here is my R code. How to revise it?

d<-read.table("16_prop_s.tsv", header=T, sep="\t")
library(ggplot2)
library(scales)
ggplot(d, aes(x=factor(NRB))) + 
   geom_histogram(aes(y=(..count..)/sum(..count..))) + 
   scale_y_continuous(labels=percent_format()) + 
   xlab("Rotatable bonds") + opts(axis.title.y = theme_blank()) +
   opts(axis.title.x = theme_text(size = 24)) + 
   opts(axis.text.x = theme_text(size = 18)) + 
   opts(axis.text.y = theme_text(size = 18))
like image 413
Jacky Lee Avatar asked Dec 04 '22 02:12

Jacky Lee


2 Answers

Use scale_x_discrete with custom breaks:

d <- data.frame(NRB = c(abs(round(rnorm(1000, 5, 4))), 1:34))
library(ggplot2)
library(scales)
p <- ggplot(d, aes(x=factor(NRB))) +
  geom_histogram(aes(y=(..count..)/sum(..count..))) +
  scale_y_continuous(labels=percent_format()) +
  xlab("Rotatable bonds") + opts(axis.title.y = theme_blank()) +
  opts(axis.title.x = theme_text(size = 24)) +
  opts(axis.text.x = theme_text(size = 18)) +
  opts(axis.text.y = theme_text(size = 18))

p + scale_x_discrete(breaks = c(1:10, seq(15, 35,5)), 
                     labels = c(1:10, seq(15, 35,5)))

enter code here

If you want evenly distributed grid lines but with the same number use empty labels.

x <- 11:35
p + scale_x_discrete(breaks = c(1:35)), 
                     labels = c(1:10, ifelse(x%%5 == 0, x, "")))
like image 145
Luciano Selzer Avatar answered Dec 14 '22 23:12

Luciano Selzer


I like @lselzer's solution, but it may not be technically what you're looking for. The reader gets the impression that the scale is maybe on a log scale, which is probably not what you want. You can add labels that have empty values in-between like so.

p + scale_x_discrete(breaks = 1:34, 
   labels = c(1:10, "", "", "", "", 15, "", "", "", "", 20, "", "", "", "", 25, "", "", "", "", 30, "", "", "", 34))

enter image description here

like image 39
Roman Luštrik Avatar answered Dec 14 '22 23:12

Roman Luštrik