![]() You can do this directly from RStudio, but then it is difficult to get the resolution and aspect ratio right. ![]() Here, I have performed both of these operations in one go:įor writing reports and papers, you often need to export graphics from R as a image file. First, we have to run the density function on the data, and then plot the result of this function. ![]() I introduced some new options here: 1) When plotting a histogram, the breaks parameter can be used to manually specify the bins within which to count values (here, I specified bins from values of 10 to 34 in increments of 2) 2) The xaxp parameter specifies where on the x-axis you want to place tick marks - in this case from 10 to 34 in 12 increments, matching the breaks in our histogram (the xaxp and yaxp parameters can also be used for scatterplots) 3) the main parameter allows us to include a title on our graph - a title is included by default in histograms, and so here I have used main=NA to remove this title 4) the box function allows us to add lines immediately around the plot - here, I have added an l-shaped box as in the scatterplot above (this is not included by default with histograms).Īnother way to show a distribution of values within a variable is using a density plot. The final result is reasonably aesthetically pleasing. The default is c(5.1,4.1,4.1,2.1) # Since we have removed other white space, we would now have a lot of white space # around our figure, so we will shrink these margins a bit par( mar= c( 3.2, 3.2, 0.2, 0.2)) plot(months,AirPassengers, type= "b", pch= 16, col= "#581845", bty= "l", xaxt= "n", xlab= "Year", ylab= "Number of passengers (thousands)") axis( side = 1, at = seq( from= 1, to= 144, by= 12), labels = 1949 : 1960) # The las parameter changes the orientation of the x labels # The default is las = 0, which makes all axis text parallel to the axis line # las = 1 makes all axis text horizontal par( las= 1) # The tck parameter determines the length of the tick marks on the axes # The default is tck=-0.5 (negative numbers give ticks outside the axis) # We will make the tick marks a bit shorter par( tck= - 0.01) # The mgp parameter changes the spacing between the axis and the axis labels # The default is c(3,1,0), which leaves a lot of white space # We will shrink it to c(1.8,0.2,0) par( mgp= c( 1.8, 0.2, 0)) # The cex parameters control the sizes of points and text in the graph # The overall cex parameter controls everything together, cex.axis the axis text, # cex.lab the axis labels, and cex.main the graph title (if you have one) # We will increase the size of the axis labels slightly from the default of 1 par( cex.lab= 1.2) # Finally for now (although there are many other graphical parameters), the mar # parameter sets the margin spacing around the figure.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |