We can fix that setting the xpd argument to NA, which will let us draw text below the main plot by changing the way plotting is clipped (see the help page for par for more info on how xpd works). That’s not quite what we want! The labels are more or less in the right spot, but the bottom half is cut off. labels = names ( dat ), # Increase the label size a bit. y = par ( "usr" ), # Use the names from the list. text ( x = 1 : length ( dat ), # Draw labels at the bottom of the chart. boxplot ( dat, xaxt = "n", yaxt = "n" ) axis ( side = 1, labels = FALSE ) axis ( side = 2, las = 2, mgp = c ( 3, 0.75, 0 )) # Draw the x-axis labels. Since we want to draw the x-axis labels at the bottom of the chart, we will use par("usr") as a starting point for the y values. par("usr") will return a vector of the form c(x1, x2, y1, y2) where x1 and x2 are the coordinates of the far left and far right of the plot (the x range), and y1 and y2 are the coordinates of the bottom and top of the plot (the y range). To get the coordinates for the edges of the plotting region, we need the usr argument. There are a TON of possible arguments so I encourage you to take a look at the documentation by typing ?par or help("par") at the R command line. The par function is used to set and query graphical parameters. Rather than manually trying to figure out what the y coordinates for the axis should be, we can get the coordinates of the plotting region directly with the par function. On our chart, the y coordinates run from about -2 to 8. So the x argument will be x = 1:length(dat). These represent numeric vectors of coordinates where the labels will be drawn.įor boxplots, the x coordinates start at 1 and run to the length of the data (in this case 4). Now, we need to position the labels with the x and y arguments of the text function. To tell the text function about our labels, we use the labels parameter (the same one we used for the axis function) like this: labels = names(dat). set.seed ( 1234 ) dat names ( dat ) "Cool dataset one" "Cool dataset two" "Another long name" "Really really long" Maybe something like this: # Set the random seed so your data will be the same as mine. Sometimes you have a plot with axis labels that are too long and end up getting clipped or dropped from the figure. O’Reilly Media.If you want to learn more about styling plots with base R graphics, check out my step-by-step guide to matching the ggplot2 classic theme using base R. Help on all the ggplot functions can be found at the The master ggplot help site.Ī useful cheat sheet on commonly used functions can be downloaded here.Ĭhang, W (2012) R Graphics cookbook. To further customise the aesthetics of the graph, including colour and formatting, see our other ggplot help pages: Print(IrisPlot + myblanktheme + labs(title = "Petal and sepal \nlength of iris", y = "Petal length (cm)", x = "Sepal length (cm)")) Legend.text = element_text(face = "italic", colour = "steelblue4", family = "Helvetica"),Īxis.title = element_text(family = "Helvetica", size = (10), colour = "steelblue4"), Plot.title = element_text(family = "Helvetica", face = "bold", size = (15)), The following code would remove the legend title and axis text. To do this you use the code = element_blank(), remembering those open and closed brackets. For example, size = (3).Īnother option is to remove the text from the plot entirely. Remember to include “” before and after the colour name. the colour can be changed to any of the colours listed here. the type of emphasis, with options including bold, italic and “alic”. Examples of fonts include: “Palatino”, “Helvetica”, “Courier”, “Times”. The font, colour, size and emphasis of any of these labels can be altered by arguments within element_text(your format). legend categories - legend.text = element_text().Where “title type” specifies which particular text you want to edit. The basic format is: mytheme <- theme(title type = element_text(your formats)) To do this, use the code theme() and customise with element_text() to alter these properties. The font, colour, size and emphasis of your labels and text can all be altered. One Continuous and One Categorical VariableĪltering the text style of your legend, axis or title
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |