Ggplot Lines Stack Together Not Easy to Read
Customizing Graphs
Graph defaults are fine for quick data exploration, but when yous desire to publish your results to a blog, paper, article or poster, you'll probably want to customize the results. Customization can improve the clarity and attractiveness of a graph.
This chapter describes how to customize a graph'due south axes, gridlines, colors, fonts, labels, and legend. It also describes how to add together annotations (text and lines).
Axes
The 10-axis and y-axis represent numeric, categorical, or engagement values. You can modify the default scales and labels with the functions below.
Quantitative axes
A quantitative axis is modified using the scale_x_continuous or scale_y_continuous function.
Options include
-
breaks- a numeric vector of positions
-
limits- a numeric vector with the min and max for the scale
# customize numerical ten and y axes library(ggplot2) ggplot(mpg, aes(x=displ, y=hwy)) + geom_point() + scale_x_continuous(breaks = seq(1, 7, 1), limits= c(1, vii)) + scale_y_continuous(breaks = seq(10, 45, v), limits= c(x, 45))
Effigy 10.one: Customized quantitative axes
Numeric formats
The scales package provides a number of functions for formatting numeric labels. Some of the almost useful are
-
dollar
-
comma
-
percent
Let'due south demonstrate these functions with some constructed data.
# create some data gear up.seed(1234) df <- data.frame(xaxis = rnorm(50, 100000, 50000), yaxis = runif(l, 0, 1), pointsize = rnorm(50, one thousand, k)) library(ggplot2) # plot the axes and fable with formats ggplot(df, aes(x = xaxis, y = yaxis, size=pointsize)) + geom_point(colour = "cornflowerblue", blastoff = .6) + scale_x_continuous(label = scales::comma) + scale_y_continuous(label = scales::percent) + scale_size(range = c(1,ten), # point size range label = scales::dollar)
Figure 10.2: Formatted axes
To format currency values as euros, you can use
label = scales::dollar_format(prefix = "", suffix = "\u20ac").
Categorical axes
A chiselled axis is modified using the scale_x_discrete or scale_y_discrete function.
Options include
-
limits- a character vector (the levels of the quantitative variable in the desired club) -
labels- a graphic symbol vector of labels (optional labels for these levels)
library(ggplot2) # customize categorical ten axis ggplot(mpg, aes(x = grade)) + geom_bar(fill = "steelblue") + scale_x_discrete(limits = c("pickup", "suv", "minivan", "midsize", "meaty", "subcompact", "2seater"), labels = c("Pickup \n Truck", "Sport Utility \northward Vehicle", "Minivan", "Mid-size", "Compact", "Subcompact", "2-Seater"))
Figure ten.3: Customized categorical centrality
Appointment axes
A appointment axis is modified using the scale_x_date or scale_y_date function.
Options include
-
date_breaks- a cord giving the distance betwixt breaks like "2 weeks" or "ten years" -
date_labels- A string giving the formatting specification for the labels
The table below gives the formatting specifications for date values.
| Symbol | Significant | Example |
|---|---|---|
| %d | twenty-four hour period as a number (0-31) | 01-31 |
| %a | abbreviated weekday | Monday |
| %A | unabbreviated weekday | Monday |
| %grand | calendar month (00-12) | 00-12 |
| %b | abbreviated month | Jan |
| %B | unabbreviated month | January |
| %y | 2-digit twelvemonth | 07 |
| %Y | 4-digit year | 2007 |
library(ggplot2) # customize appointment scale on x axis ggplot(economics, aes(10 = appointment, y = unemploy)) + geom_line(color= "darkgreen") + scale_x_date(date_breaks = "5 years", date_labels = "%b-%y")
Figure ten.four: Customized appointment centrality
Hither is a help canvass for modifying scales developed from the online assist.
Colors
The default colors in ggplot2 graphs are functional, just often non as visually appealing every bit they can be. Happily this is easy to change.
Specific colors tin be
- specified for points, lines, bars, areas, and text, or
- mapped to the levels of a variable in the dataset.
Specifying colors manually
To specify a color for points, lines, or text, use the color = "colorname" option in the appropriate geom. To specify a color for bars and areas, utilise the fill = "colorname" option.
Examples:
-
geom_point(color = "blue")
-
geom_bar(fill = "steelblue")
Colors can exist specified by name or hex code.
To assign colors to the levels of a variable, apply the scale_color_manual and scale_fill_manual functions. The quondam is used to specify the colors for points and lines, while the later is used for bars and areas.
Here is an example, using the diamonds dataset that ships with ggplot2. The dataset contains the prices and attributes of 54,000 round cut diamonds.
# specify fill color manually library(ggplot2) ggplot(diamonds, aes(x = cut, fill = clarity)) + geom_bar() + scale_fill_manual(values = c("darkred", "steelblue", "darkgreen", "gilt", "chocolate-brown", "purple", "grey", "khaki4"))
Figure 10.v: Transmission color selection
If you are aesthetically challenged like me, an alternative is to utilize a predefined palette.
Color palettes
At that place are many predefined color palettes available in R.
RColorBrewer
The most popular alternative palettes are probably the ColorBrewer palettes.
Figure 10.half dozen: RColorBrewer palettes
You can specify these palettes with the scale_color_brewer and scale_fill_brewer functions.
# use an ColorBrewer make full palette ggplot(diamonds, aes(ten = cut, make full = clarity)) + geom_bar() + scale_fill_brewer(palette = "Dark2")
Figure 10.7: Using RColorBrewer
Adding direction = -i to these functions reverses the order of the colors in a palette.
Viridis
The viridis palette is another popular pick.
For continuous scales use
-
scale_fill_viridis_c
-
scale_color_viridis_c
For discrete (categorical scales) utilize
-
scale_fill_viridis_d
-
scale_color_viridis_d
# Apply a viridis fill up palette ggplot(diamonds, aes(ten = cut, fill = clarity)) + geom_bar() + scale_fill_viridis_d()
Figure 10.8: Using the viridis palette
Other palettes
Other palettes to explore include dutchmasters, ggpomological, LaCroixColoR, nord, ochRe, palettetown, pals, rcartocolor, and wesanderson.
If you desire to explore all the palette options (or about all), take a look at the paletter packet.
To larn more than well-nigh colour specifications, see the R Cookpage page on ggplot2 colors. Also see the color choice advice in this book.
Points & Lines
Points
For ggplot2 graphs, the default indicate is a filled circle. To specify a different shape, use the shape = # option in the geom_point office. To map shapes to the levels of a categorical variable apply the shape = variablename option in the aes function.
Examples:
-
geom_point(shape = i)
- geom_point(
aes(shape = sexual activity))
Availabe shapes are given in the tabular array below.
Figure x.9: Bespeak shapes
Shapes 21 through 26 provide for both a make full color and a border color.
Lines
The default line type is a solid line. To change the linetype, use the linetype = # option in the geom_line function. To map linetypes to the levels of a categorical variable apply the linetype = variablename option in the aes function.
Examples:
-
geom_line(linetype = one)
- geom_line(
aes(linetype = sex))
Availabe linetypes are given in the tabular array below.
Figure 10.x: Linetypes
Fonts
R does not take dandy support for fonts, just with a scrap of piece of work, you tin alter the fonts that appear in your graphs. First y'all need to install and set-up the extrafont bundle.
# one time install install.packages("extrafont") library(extrafont) font_import() # see what fonts are at present available fonts() Utilize the new font(s) using the text option in the theme part.
# specify new font library(extrafont) ggplot(mpg, aes(x = displ, y=hwy)) + geom_point() + labs(championship = "Diplacement by Highway Mileage", subtitle = "MPG dataset") + theme(text = element_text(size = 16, family unit = "Comic Sans MS"))
Figure 10.11: Culling fonts
To learn more virtually customizing fonts, see Working with R, Cairo graphics, custom fonts, and ggplot.
Labels
Labels are a central ingredient in rendering a graph understandable. They're are added with the labs role. Available options are given below.
| choice | Apply |
|---|---|
| title | main championship |
| subtitle | subtitle |
| explanation | explanation (bottom right past default) |
| x | horizontal axis |
| y | vertical axis |
| color | color legend title |
| fill | fill legend championship |
| size | size legend title |
| linetype | linetype fable title |
| shape | shape legend title |
| alpha | transparency fable title |
| size | size fable championship |
For instance
# add together plot labels ggplot(mpg, aes(x = displ, y=hwy, color = class, shape = factor(twelvemonth))) + geom_point(size = 3, alpha = .5) + labs(title = "Mileage by engine displacement", subtitle = "Data from 1999 and 2008", caption = "Source: EPA (http://fueleconomy.gov)", x = "Engine displacement (litres)", y = "Highway miles per gallon", color = "Motorcar Class", shape = "Yr") + theme_minimal()
Effigy 10.14: Graph with labels
This is non a great graph - it is besides busy, making the identification of patterns hard. It would better to facet the twelvemonth variable, the form variable or both. Trend lines would too be helpful.
Annotations
Annotations are addition data added to a graph to highlight important points.
Adding text
There are 2 primary reasons to add text to a graph.
One is to place the numeric qualities of a geom. For example, nosotros may want to identify points with labels in a scatterplot, or label the heights of bars in a bar nautical chart.
Another reason is to provide additional information. We may want to add notes about the data, point out outliers, etc.
Labeling values
Consider the following scatterplot, based on the car data in the mtcars dataset.
# basic scatterplot data(mtcars) ggplot(mtcars, aes(10 = wt, y = mpg)) + geom_point()
Figure 10.xv: Uncomplicated scatterplot
Let's label each bespeak with the proper name of the automobile information technology represents.
# scatterplot with labels data(mtcars) ggplot(mtcars, aes(ten = wt, y = mpg)) + geom_point() + geom_text(label = row.names(mtcars))
Figure 10.sixteen: Scatterplot with labels
The overlapping labels make this chart difficult to read. There is a package called ggrepel that tin can help us hither.
# scatterplot with not-overlapping labels information(mtcars) library(ggrepel) ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_text_repel(label = row.names(mtcars), size= iii)
Effigy 10.17: Scatterplot with not-overlapping labels
Much ameliorate.
Adding labels to bar charts is covered in the aptly named labeling bars section.
Adding boosted information
We tin can place text anywhere on a graph using the annotate function. The format is
annotate("text", ten, y, label = "Some text", color = "colorname", size=textsize) where x and y are the coordinates on which to place the text. The color and size parameters are optional.
By default, the text volition be centered. Apply hjust and vjust to change the alignment.
-
hjust0 = left justified, 0.5 = centered, and 1 = right centered. -
vjust0 = above, 0.5 = centered, and 1 = below.
Continuing the previous case.
# scatterplot with explanatory text information(mtcars) library(ggrepel) txt <- paste("The relationship between car weight", "and mileage appears to be roughly linear", sep = " \n ") ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(colour = "red") + geom_text_repel(label = row.names(mtcars), size= 3) + ggplot2:: annotate("text", 6, 30, label=txt, color = "ruby-red", hjust = 1) + theme_bw()
Figure 10.eighteen: Scatterplot with arranged labels
Meet this blog post for more than details.
Adding lines
Horizontal and vertical lines can exist added using:
-
geom_hline(yintercept = a) -
geom_vline(xintercept = b)
where a is a number on the y-axis and b is a number on the x-axis respectively. Other option include linetype and color.
# add annotation line and text label min_cty <- min(mpg$cty) mean_hwy <- mean(mpg$hwy) ggplot(mpg, aes(x = cty, y=hwy, color=drv)) + geom_point(size = iii) + geom_hline(yintercept = mean_hwy, color = "darkred", linetype = "dashed") + ggplot2:: annotate("text", min_cty, mean_hwy + 1, characterization = "Mean", color = "darkred") + labs(title = "Mileage past bulldoze blazon", x = "City miles per gallon", y = "Highway miles per gallon", colour = "Drive")
Figure 10.19: Graph with line annotation
We could add together a vertical line for the mean city miles per gallon as well. In any instance, always label annotation lines in some manner. Otherwise the reader will not know what they mean.
Highlighting a single group
Sometimes you desire to highlight a single grouping in your graph. The gghighlight function in the gghighlight package is designed for this.
Here is an case with a scatterplot.
# highlight a set of points library(ggplot2) library(gghighlight) ggplot(mpg, aes(x = cty, y = hwy)) + geom_point(color = "red", size= 2) + gghighlight(class == "midsize")
Effigy 10.20: Highlighting a grouping
Below is an instance with a bar chart.
# highlight a unmarried bar library(gghighlight) ggplot(mpg, aes(x = class)) + geom_bar(fill up = "blood-red") + gghighlight(class == "midsize")
Figure x.21: Highlighting a grouping
There is goose egg here that could non exist done with base of operations graphics, but information technology is more convenient.
Themes
ggplot2 themes control the advent of all non-data related components of a plot. You lot can change the look and feel of a graph by altering the elements of its theme.
Altering theme elements
The theme function is used to modify individual components of a theme.
The parameters of the theme function are described in a cheatsheet adult from the online assist.
Consider the following graph. It shows the number of male and female person kinesthesia past rank and discipline at a detail university in 2008-2009. The data come from the Salaries for Professors dataset.
# create graph data(Salaries, package = "carData") p <- ggplot(Salaries, aes(x = rank, fill up = sexual activity)) + geom_bar() + facet_wrap(~subject) + labs(title = "Academic Rank by Gender and Discipline", x = "Rank", y = "Frequency", fill = "Gender") p
Effigy x.22: Graph with default theme
Allow'south make some changes to the theme.
- Modify label text from black to navy blue
- Change the panel background color from grey to white
- Add solid greyness lines for major y-axis grid lines
- Add together dashed grey lines for minor y-centrality grid lines
- Eliminate x-axis grid lines
- Modify the strip groundwork color to white with a gray edge
Using the cheat sail gives usa
p + theme(text = element_text(colour = "navy"), panel.background = element_rect(fill = "white"), console.grid.major.y = element_line(color = "grayness"), panel.grid.pocket-sized.y = element_line(color = "grey", linetype = "dashed"), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), strip.background = element_rect(make full = "white", color= "greyness"))
Figure 10.23: Graph with modified theme
Wow, this looks pretty awful, just you get the idea.
ggThemeAssist
If you would like to create your own theme using a GUI, take a look at ggThemeAssist. Subsequently you lot install the bundle, a new menu item will appear under Addins in RStudio.
Highlight the code that creates your graph, then choose the ggThemeAssist option from the Addins drop-down menu. You tin alter many of the features of your theme using bespeak-and-click. When you're done, the theme lawmaking will exist appended to your graph lawmaking.
Pre-packaged themes
I'g not a very good creative person (only look at the last example), then I frequently look for pre-packaged themes that tin can exist applied to my graphs. At that place are many available.
Some come with ggplot2. These include theme_classic, theme_dark, theme_gray, theme_grey, theme_light theme_linedraw, theme_minimal, and theme_void. We've used theme_minimal frequently in this volume. Others are bachelor through add-on packages.
ggthemes
The ggthemes package come up with 19 themes.
| Theme | Description |
|---|---|
| theme_base | Theme Base of operations |
| theme_calc | Theme Calc |
| theme_economist | ggplot color theme based on the Economist |
| theme_economist_white | ggplot color theme based on the Economist |
| theme_excel | ggplot color theme based on old Excel plots |
| theme_few | Theme based on Few'due south "Applied Rules for Using Color in Charts" |
| theme_fivethirtyeight | Theme inspired by fivethirtyeight.com plots |
| theme_foundation | Foundation Theme |
| theme_gdocs | Theme with Google Docs Chart defaults |
| theme_hc | Highcharts JS theme |
| theme_igray | Inverse gray theme |
| theme_map | Clean theme for maps |
| theme_pander | A ggplot theme originated from the pander parcel |
| theme_par | Theme which takes its values from the current 'base' graphics parameter values in 'par'. |
| theme_solarized | ggplot color themes based on the Solarized palette |
| theme_solarized_2 | ggplot color themes based on the Solarized palette |
| theme_solid | Theme with nothing other than a background color |
| theme_stata | Themes based on Stata graph schemes |
| theme_tufte | Tufte Maximal Data, Minimal Ink Theme |
| theme_wsj | Wall Street Journal theme |
To demonstrate their employ, we'll first create and salvage a graph.
# create basic plot library(ggplot2) p <- ggplot(mpg, aes(x = displ, y=hwy, color = grade)) + geom_point(size = 3, alpha = .5) + labs(championship = "Mileage past engine displacement", subtitle = "Data from 1999 and 2008", explanation = "Source: EPA (http://fueleconomy.gov)", x = "Engine displacement (litres)", y = "Highway miles per gallon", color = "Car Class") # display graph p
Figure ten.24: Default theme
At present let's utilise some themes.
# add together economist theme library(ggthemes) p + theme_economist()
Figure 10.25: Economist theme
# add fivethirtyeight theme p + theme_fivethirtyeight()
Figure 10.26: 5 30 Eight theme
# add together wsj theme p + theme_wsj(base_size= 8)
Effigy x.27: Wall Street Journal theme
Past default, the font size for the wsj theme is usually as well big. Irresolute the base_size option can help.
Each theme also comes with scales for colors and fills. In the next example, both the few theme and colors are used.
# add together few theme p + theme_few() + scale_color_few()
Figure 10.28: Few theme and colors
Endeavour out dissimilar themes and scales to observe one that you similar.
hrbrthemes
The hrbrthemes package is focused on typography-centric themes. The results are charts that tend to have a clean look.
Continuing the example plot from to a higher place
# add together few theme library(hrbrthemes) p + theme_ipsum()
Figure ten.29: Ipsum theme
See the hrbrthemes homepage for additional examples.
ggthemer
The ggthemer package offers a wide range of themes (17 as of this printing).
The package is not available on CRAN and must be installed from GitHub.
# one time install install.packages("devtools") devtools:: install_github('cttobin/ggthemr') The functions work a bit differently. Employ the ggthemr("themename") function to set future graphs to a given theme. Use ggthemr_reset() to return future graphs to the ggplot2 default theme.
Electric current themes include flat, flat dark, camoflauge, chalk, copper, dust, globe, fresh, grape, grass, greyscale, lite, lilac, pale, body of water, sky, and solarized.
# set graphs to the flat nighttime theme library(ggthemr) ggthemr("flat dark") p
Effigy 10.30: Ipsum theme
I would not actually utilise this theme for this particular graph. It is difficult to distinguish colors. Which greenish represents compact cars and which represents subcompact cars?
Select a theme that best conveys the graph's information to your audience.
Source: https://rkabacoff.github.io/datavis/Customizing.html
0 Response to "Ggplot Lines Stack Together Not Easy to Read"
Post a Comment