how to set axis breaks to only a part of the axis?
ggplot2, histogram, plot, r
Solution
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)))
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, "")))
Problem
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. 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)) ```