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))
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, "")))
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With