The default styling of histograms does not look great and might not be suitable for publications. In this recipe, we will learn how to improve the look by setting bar colors and borders, and adjusting the axes.
Once again we will use the airpollution.csv
example. So, let's make sure it is loaded by running the following command at the R prompt:
air<-read.csv("airpollution.csv")
Let's visualize the probability distribution of respirable particle concentrations with black bars and white borders:
hist(air$Respirable.Particles, prob=TRUE,col="black",border="white", xlab="Respirable Particle Concentrations", main="Distribution of Respirable Particle Concentrations")
By now, you might have guessed how to do this yourself. We used the col
and border
arguments to set the bar and border colors to black and white, respectively.
You might have noticed that, in all of the previous examples, the x axis is detached from the base of the bars. This gives the graphs a bit of an unclean look. Also notice that the y axis labels are rotated vertically, which makes them harder to read. Let's improve the graph by fixing these two visual settings:
par(yaxs="i",las=1) hist(air$Respirable.Particles, prob=TRUE,col="black",border="white", xlab="Respirable Particle Concentrations", main="Distribution of Respirable Particle Concentrations") box(bty="l") grid(nx=NA,ny=NULL,lty=1,lwd=1,col="gray")
So, we used a couple of extra function calls to change the look of the graph. First, we called the par()
function and set yaxs
to "i"
so that the y axis joins the x axis instead of having a detached x axis. We also set las
equal to 1
to make all the axis labels horizontal, thus making it easier to read the y axis labels. Then, we ran the hist()
function call as before and called box()
with the type equal to "l"
to make an L-shaped box running along the axes. Finally, we added horizontal grid lines using the grid()
function.
3.145.20.132