Line plot in ggplot2

Line plot in ggplot2 DEFAULT

Data Visualization using GGPlot2

In a line plot, observations are ordered by x value and connected by a line.

x value (for x axis) can be :

  • date : for a time series data
  • texts
  • discrete numeric values
  • continuous numeric values

This article describes how to create a line plot using the ggplot2 R package

You will learn how to:

  • Create basic and grouped line plots
  • Add points to a line plot
  • Change the line types and colors by group


Contents:

Related Book

GGPlot2 Essentials for Great Data Visualization in R

Key R functions

  • Key functions:
    • connects the observations in the order in which they appear in the data.
    • connects them in order of the variable on the x axis.
    • creates a stairstep plot, highlighting exactly when changes occur.
  • Key arguments to customize the plot: alpha, color, linetype and size

Data preparation

We’ll create two data frames derived from the datasets.

  • : Tooth length
  • : Dose in milligrams (, 1, 2)
  • : Supplement type (VC or OJ)

Loading required R package

Load the ggplot2 package and set the default theme to with the legend at the top of the plot:

Basic line plots

Note that, the group aesthetic determines which cases are connected together.

Line plot with multiple groups

In the graphs below, line types and point shapes are controlled automatically by the levels of the variable :

Line plot with a numeric x-axis

If the variable on x-axis is numeric, it can be useful to treat it as a continuous or a factor variable depending on what you want to do:

Line plot with dates on x-axis: Time series

time series data sets are used :

Plots :

Change line size :

Plot multiple time series data:

Conclusion

This article shows how to create line plots using the ggplot2 package.



Recommended for you

This section contains best data science and self-development resources to help you on your path.

Coursera - Online Courses and Specialization

Data science

Popular Courses Launched in

Trending Courses

Amazon FBA

Amazing Selling Machine

Books - Data Science

Our Books

Others

Version: Français

Sours: https://www.datanovia.com/en/lessons/ggplot-line-plot/

ggplot2 line plot : Quick start guide - R software and data visualization

This R tutorial describes how to create line plots using R software and ggplot2 package.

In a line graph, observations are ordered by x value and connected.

The functions geom_line(), geom_step(), or geom_path() can be used.

x value (for x axis) can be :

  • date : for a time series data
  • texts
  • discrete numeric values
  • continuous numeric values

ggplot2 line plot - R software and data visualization


Data

Data derived from ToothGrowth data sets are used. ToothGrowth describes the effect of Vitamin C on tooth growth in Guinea pigs.

  • len : Tooth length
  • dose : Dose in milligrams (, 1, 2)

Create line plots with points

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

Read more on line types : ggplot2 line types

You can add an arrow to the line using the grid package :

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

Observations can be also connected using the functions geom_step() or geom_path() :

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization


  • geom_line : Connecting observations, ordered by x value
  • geom_path() : Observations are connected in original order
  • geom_step : Connecting observations by stairs

Data

Data derived from ToothGrowth data sets are used. ToothGrowth describes the effect of Vitamin C on tooth growth in Guinea pigs. Three dose levels of Vitamin C (, 1, and 2 mg) with each of two delivery methods [orange juice (OJ) or ascorbic acid (VC)] are used :

  • len : Tooth length
  • dose : Dose in milligrams (, 1, 2)
  • supp : Supplement type (VC or OJ)

Create line plots

In the graphs below, line types, colors and sizes are the same for the two groups :

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

Change line types by groups

In the graphs below, line types and point shapes are controlled automatically by the levels of the variable supp :

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

It is also possible to change manually the line types using the function scale_linetype_manual().

ggplot2 line plot - R software and data visualization

You can read more on line types here : ggplot2 line types

If you want to change also point shapes, read this article : ggplot2 point shapes

Change line colors by groups

Line colors are controlled automatically by the levels of the variable supp :

ggplot2 line plot - R software and data visualization

It is also possible to change manually line colors using the functions :

  • scale_color_manual() : to use custom colors
  • scale_color_brewer() : to use color palettes from RColorBrewer package
  • scale_color_grey() : to use grey color palettes

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

Read more on ggplot2 colors here : ggplot2 colors

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

The allowed values for the arguments legend.position are : “left”,“top”, “right”, “bottom”.

Read more on ggplot legend : ggplot2 legend

If the variable on x-axis is numeric, it can be useful to treat it as a continuous or a factor variable depending on what you want to do :

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

economics time series data sets are used :

Plots :

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

Change line size :

ggplot2 line plot - R software and data visualization

The function below will be used to calculate the mean and the standard deviation, for the variable of interest, in each group :

Summarize the data :

The function geom_errorbar() can be used to produce a line graph with error bars :

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

Change colors manually :

ggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualizationggplot2 line plot - R software and data visualization

This analysis has been performed using R software (ver. ) and ggplot2 (ver. )


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!





This page has been seen times
Sours: http://www.sthda.com/english/wiki/ggplot2-line-plot-quick-start-guide-r-software-and-data-visualization
  1. Discord id search
  2. Psp 1000 replacement parts
  3. Galaxy note tablet

Line Plot using ggplot2 in R

In a line graph, we have the horizontal axis value through which the line will be ordered and connected using the vertical axis values. We are going to use the R package ggplot2 which has several layers in it. 

First, you need to install the ggplot2 package if it is not previously installed in R Studio. 

Function Used:

  • geom_line connects them in the order of the variable on the horizontal (x) axis.

Syntax:

geom_line(mapping=NULL, data=NULL, stat=”identity”, position=”identity”,…)

  • geom_path connects the observation in the same order as in data

Syntax:



geom_path(mapping=NULL, data=NULL, stat=”identity”, position=”identity”,…)

Single Line Plot

In this section, we will be dealing with a single line chart and will also discuss various attributes that help its appearance.

Data set in Use:

R

Output:

Basic Line Plot

For a simple line chart data is roughly passed to the function with some required attributes. 



Example:

R

 

 

Output:

Formating Line

For this, the command linetype is used. ggplot2 provides various line types. For example : dotted, two dash, dashed, etc. This attribute is passed with a required value.

Example:

R

 

 

Output:

The command color is used and the desired color is written in double quotes [&#; &#;] inside geom_line( ).



Example:

R

 

 

Output:

The line size can be changed using the command size and providing the value of the size inside geom_line( ).

Example:

R

 

 

Output:

Adding Chart Title, Axis Title

ggtitle() with the appropriate title can be usedto add chart title and labs again with appropriate input can be used to add axes title.



Example:

R

 

 

 

Output:

Changing the Theme

Use theme_theme_name() to add the theme. There are a lot of themes available in R library. For example: dark, classic, etc. Values can be provided as desired.

Example:

R

 

 

 

Output:

Adding arrow

To add an arrow in line use the grid library is used. Then to add arrows use the arrow( ) to add an arrow. It is also possible to change the parameters in an arrow like angle, type, ends. 



Example:

R

 

 

 

Output:

Adding Data labels

Use label to get the values in y-axis and nudge_y to place the data label.

Example:

R

 

 

Output:

Scaling axis :

Use xlim( ) to change the x-axis scale and ylim( ) to change the y-axis scale and pass appropriate values to these. 



Syntax:

xlim(min,max)

ylim(min,max)

Example:

R

 

 

 

Output:

Plotting Multiple lines

For plotting multiple plots into one, nothing changes except that group attribute has to set to the name of the column on the basis of which different lines will be drawn.

Example:

R



 

 

Output:

You can also add title, axes title, data labels in the above line plot as discussed in the previous section.

Formatting the plot :

  • Using separate line types based on groups 

To differentiate the lines by changing the type of line provide the line type in geom_line() and shape for the legend in geom_point().

Example:

R

 

 

 

Output:

  • Assigning different line colors on the basis of groups 

The following code automatically controls color using the level of the variable &#;type&#;. It will assign separate colors to each line.

Example:



R

 

 

Output:

To enter color manually you can use :

  • scale_color_brewer( ) : It uses different color palettes from the RColorBrewer package. It has various color palettes.
  • scale_color_manual( ) : It is used to manually add discrete colors.

Example:

R

 

 

 

 

Output:

  • Changing the position of legends 

For changing the legend position legen.position attribute of the theme function is passed with the required value.

Syntax:

theme(legend.position=&#;pos&#;)

pos It can be top, right, bottom, left or none

Example:

R

 

 

 

 

 

 

Output:




Sours: https://www.geeksforgeeks.org/line-plot-using-ggplot2-in-r/

Problem

You want to do make basic bar or line graphs.

Solution

To make graphs with ggplot2, the data must be in a data frame, and in “long” (as opposed to wide) format. If your data needs to be restructured, see this page for more information.

Basic graphs with discrete x-axis

With bar graphs, there are two different things that the heights of bars commonly represent:

  • The count of cases for each group – typically, each x value represents one group. This is done with , which calculates the number of cases in each group (if x is discrete, then each x value is a group; if x is continuous, then all the data is automatically in one group, unless you specifiy grouping with ).
  • The value of a column in the data set. This is done with , which leaves the y values unchanged.
x axis isHeight of bar representsCommon name
ContinuousCountHistogram
DiscreteCountBar graph
ContinuousValueBar graph
DiscreteValueBar graph

In ggplot2, the default is to use , so that the bar height represents the count of cases.

Bar graphs of values

Here is some sample data (derived from the dataset in the reshape2 package):

In these examples, the height of the bar will represent the value in a column of the data frame. This is done by using instead of the default, .

These are the variable mappings used here:

  • : x-axis and sometimes color fill
  • : y-axis

plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3

The desired bar graph might look something like this:

plot of chunk unnamed-chunk-4

See ../Colors (ggplot2) for more information on colors.

Bar graphs of counts

In these examples, the height of the bar will represent the count of cases. This is done by using (which is the default).

We’ll start with the data from the reshape2 package:

To get a bar graph of counts, don’t map a variable to , and use (which is the default) instead of :

plot of chunk unnamed-chunk-6

Line graphs

For line graphs, the data points must be grouped so that it knows which points to connect. In this case, it is simple – all points should be connected, so . When more variables are used and multiple lines are drawn, the grouping for lines is usually done by variable (this is seen in later examples).

These are the variable mappings used here:

  • : x-axis
  • : y-axis

plot of chunk unnamed-chunk-7plot of chunk unnamed-chunk-7plot of chunk unnamed-chunk-7

The desired line graph might look something like this:

plot of chunk unnamed-chunk-8

See ../Colors (ggplot2) for more information on colors, and ../Shapes and line types for information on shapes and line types.

Graphs with more variables

This data will be used for the examples below:

This is derived from the dataset in the reshape2 package.

Bar graphs

These are the variable mappings used here:

  • : x-axis
  • : color fill
  • : y-axis.

plot of chunk unnamed-chunkplot of chunk unnamed-chunkplot of chunk unnamed-chunkplot of chunk unnamed-chunk

It’s easy to change which variable is mapped the x-axis and which is mapped to the fill.

plot of chunk unnamed-chunk

See ../Colors (ggplot2) for more information on colors.

Line graphs

These are the variable mappings used here:

  • : x-axis
  • : line color
  • : y-axis.

To draw multiple lines, the points must be grouped by a variable; otherwise all points will be connected by a single line. In this case, we want them to be grouped by sex.

plot of chunk unnamed-chunkplot of chunk unnamed-chunkplot of chunk unnamed-chunkplot of chunk unnamed-chunk

It’s easy to change which variable is mapped the x-axis and which is mapped to the color or shape.

plot of chunk unnamed-chunk

See ../Colors (ggplot2) for more information on colors, and ../Shapes and line types for information on shapes and line types.

Finished examples

The finished graphs might look like these:

plot of chunk unnamed-chunkplot of chunk unnamed-chunk

In the line graph, the reason that the legend title, “Sex of payer”, must be specified three times is so that there is only one legend. The issue is explained here.

With a numeric x-axis

When the variable on the x-axis is numeric, it is sometimes useful to treat it as continuous, and sometimes useful to treat it as categorical. In this data set, the dose is a numeric variable with values , , and It might be useful to treat these values as equal categories when making a graph.

This is derived from the dataset included with R.

With x-axis treated as continuous

A simple graph might put dose on the x-axis as a numeric value. It is possible to make a line graph this way, but not a bar graph.

plot of chunk unnamed-chunk

With x-axis treated as categorical

If you wish to treat it as a categorical variable instead of a numeric one, it must be converted to a factor. This can be done by modifying the data frame, or by changing the specification of the graph.

plot of chunk unnamed-chunkplot of chunk unnamed-chunk

It is also possible to make a bar graph when the variable is treated as categorical rather than numeric.

plot of chunk unnamed-chunkplot of chunk unnamed-chunk

Sours: http://cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/

In ggplot2 plot line

Data transformation

Consider the following data frame where each column represents the path of a brownian motion.

In order to use your data frame in ggplot2 you will need to transform it into long format. You can achieve it making use of the function of the package.

Line chart of several variables

Given a data frame in long format like it is possible to create a line chart with multiple lines in ggplot2 with the following way.

Line graph of multiple lines in ggplot2

Lines width and style

The styling of the lines can be changed making use of the arguments of , like for changing the style of the line or to change its width.

Line chart customization in ggplot2

Color customization

The default color palette can be changed passing a vector of colors to the argument of the function.

Change the color of a line chart by group in ggplot2

Highlight some lines

Note that using the previous method you can also highlight some lines of the chart, using the same color for all lines but some.

Highlight line in ggplot2

Legend customization

Change the title of the legend of a line chart in ggplot2

Title

You can change the default title for the legend of the line chart with as shown below.

Change the key legend labels of a ggplot2 line chart

Legend labels

The labels of the legend can be modified making use of the argument of .

Remove the legend of a line graph in ggplot2

Remove the legend

You can also get rid of the legend making use of .

Sours: https://r-charts.com/evolution/line-graph-multiple-lines-ggplot2/
Scatter plot and Line plot in R (using ggplot2)

[This article was first published on Quantargo Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Use the aesthetic to draw line graphs and customize its styling using the parameter. Specify which coordinates to use for each line with the parameter.

  • Create your first line graph using
  • Define how different lines are connected using the parameter
  • Change the line color of a line graph using the parameter
ggplot(___) + geom_line( mapping = aes(x = ___, y = ___, group = ___, color = ___) )

Introduction to line graphs

Line graphs are used to visualize the trajectory of one numeric variable against another. Unlike scatter plots the x- and y-coordinates are not visualized through points but are instead connected through lines. Line graphs are most typically used if one variable changes continuously against another numeric variable which is the case for most time series charts (e.g. prices, customers, CO2 concentration, temperature over time), continuous functions (e.g. sine ) or other near-continuous relationships (real-world supply/demand curves).

Quiz: Line Graphs

Which of the following statements about line graphs are correct?
  • Line graphs are typically used to plot the relationship between categorical and numeric variables.
  • Line graphs are typically used to plot variables of type .
  • For line graphs it is not necessary that the relationship between two variables shows continuity.
  • Line graphs can be used to plot time series.
Start Quiz

Creating a simple line graph

ggplot(___) + geom_line( mapping = aes(x = ___, y = ___, group = ___, color = ___) )

Japan is among the countries with the highest life expectancy. Using the dataset we determine how the life expectancy in Japan has developed over time. We need to:

  1. Specify the dataset within
  2. Define the plot layer
  3. Map the to the x-axis and the life expectancy to the y-axis with the function

Note that the ggplot2 library needs to be loaded first with .

library(ggplot2) ggplot(gapminder_japan) + geom_line( mapping = aes(x = year, y = lifeExp) )

Exercise: Plot life expectancy of Brazil

Create your first line graph showing the life expectancy of people from Brazil over time.

  1. Use the function and specify the dataset as input
  2. Add a layer to the plot
  3. Map the to the x-axis and the life expectancy to the y-axis with the function
Start Exercise

Adding more lines

ggplot(___) + geom_line( mapping = aes(x = ___, y = ___, group = ___, color = ___) )

So far we only focused on single lines, but what if we have multiple countries in the dataset and want to somehow differentiate them?

Line graphs are often extended and used for the comparison of two or more lines. Multiple line graphs show the absolute differences between observations but also how the specific trajectories relate to each other. For example, let’s answer the question: How has life expectancy changed in the countries Austria and Hungary over time?

We first filter the dataset for both countries of interest. Then, we set the variable as the argument for the aesthetic mapping. The group argument tells ggplot which observations belong together and should be connected through lines. By specifying the variable ggplot creates a separate line for each country. To make the lines easier to distinguish we also map to the so that each country line has a different color.

gapminder_comparison <- filter(gapminder, country %in% c("Austria", "Hungary")) ggplot(data = gapminder_comparison) + geom_line(mapping = aes(x = year, y = lifeExp, group = country, color = country) )

Note that ggplot also separates the lines correctly if only the mapping is specified (the parameter is implicitly set).

Exercise: Compare life expectancy

Create a line graph to compare the life expectancy in the countries Japan, Brazil and India.

  1. Use the data set in your function which contains only data for the countries , and .
  2. Create a line graph with the function
  3. Map the to the x-axis and the life expectancy to the y-axis with the function
  4. Map the and the parameter to the variable.
Start Exercise

Exercise: Compare populations

Compare the population growth over the last decades in the countries Austria, Hungary and Serbia.

  1. Use the data set in your function which contains only data for the countries in question.
  2. Create a line graph with
  3. Map the to the x-axis and the population to the y-axis with
  4. Map the and the parameter to the variable.
Start Exercise

Quiz: Malformed Plot

gapminder_comparison <- filter(gapminder, country %in% c("Brazil", "China", "India")) ggplot(data = gapminder_comparison) + geom_line(mapping = aes(x = year, y = pop))What has gone wrong in this plot?
  • The population numbers are scaled differently in the plotted countries
  • The aesthetic should be used to map the population variable.
  • The aesthetic should be used to map the population variable.
  • The aesthetic should be used to map the variable.
  • The aesthetic should be used to map the variable.
Start Quiz

Create a line graph with ggplot is an excerpt from the course Introduction to R, which is available for free at quantargo.com

VIEW FULL COURSE

Related

Sours: https://www.r-bloggers.com//09/create-a-line-graph-with-ggplot/

You will also like:

How to Make Stunning Line Charts in R: A Complete Guide with ggplot2

Line Charts with R

Are your visualizations an eyesore? The s are over, pal. Terrible-looking visualizations are no longer acceptable, no matter how useful they might otherwise be. Luckily, there&#;s a lot you can do to quickly and easily enhance the aesthetics of your visualizations. Today you&#;ll learn how to make impressive line charts with R and the package.

Want to learn how to make stunning bar charts with R? Here&#;s our complete guide.

This article demonstrates how to make an aesthetically-pleasing line chart for any occasion. After reading, visualizing time series and similar data should become second nature. Today you&#;ll learn how to:

Make your first line chart

R has a package you can download. It contains data on life expectancy, population, and GDP between and It&#;s a time-series dataset, which is excellent for line-based visualizations.

Here&#;s how to load it (and other libraries):

Calling the function outputs the first six rows of the dataset. Here&#;s how they look:

Image 1 - Head of Gapminder dataset

Image 1 &#; Head of Gapminder dataset

R&#;s widely used package for data visualization is . It&#;s based on the layering principle. The first layer represents the data, and after that comes a visualization layer (or layers). These two are mandatory for any chart type, and line charts are no exception. You&#;ll learn how to add additional layers later.

Your first chart will show the population over time for the United States. Columns  and are placed on X-axis and Y-axis, respectively:

Here&#;s the corresponding visualization:

Image 2 - Population growth over time in the United States

Image 2 &#; Population growth over time in the United States

The visualization is informative but as ugly as they come. The following sections will show you how to tweak the visuals.

Change Color, Line Type, and Add Markers

Keeping the default styling is the worst thing you can do. With the layer, you can change the following properties:

  • &#; line color
  • &#; line width
  • &#; maybe you want dashed lines?

Here&#;s how to make a thicker dashed blue line:

Image 3 - Changing line style, width, and color

Image 3 &#; Changing line style, width, and color

Better, but not quite there yet. Most line charts combine lines and points to make the result more appealing. Here&#;s how to add points (markers) to yours:

Image 4 - Line chart with markers

Image 4 &#; Line chart with markers

Now the charts are getting somewhere &#; but there&#;s still a lot to do.

Titles, Subtitles, and Captions

You can&#;t have a complete chart without at least a title. A good subtitle can come in handy for extra information, and a caption is a good place to cite your sources. The most convenient way to add these is through a layer. It takes in values for , , and . 

Here&#;s how to add all three, without styles:

IMAGE 5; Image 5 - Title, subtitle, and caption with default styles

Image 5 &#; Title, subtitle, and caption with default styles

But there&#;s more to this story. You can customize all three in the same way &#; by putting styles to the layer. Here&#;s how to center title and caption, left align and italicize the caption, and make the title blue:

Image 6 - Styling title, subtitle, and caption

Image 6 &#; Styling title, subtitle, and caption

That&#;s all great, but what about the axis labels? Let&#;s see how to tweak them next.

Edit Axis Labels

Just take a look at the Y-axis for the previous year vs. population charts. The ticks look horrible. Scientific notation doesn&#;t help make things easier to read. The following snippet puts &#;M&#; next to the number &#; indicates &#;Millions&#;:

Image 7 - Changing axis ticks

Image 7 &#; Changing axis ticks

But what if you want a bit more space on top and bottom? You can specify where the axis starts and ends. Here&#;s how:

Image 8 - Changing limits of the axis

Image 8 &#; Changing limits of the axis

The layer takes in values for and &#; these determine the text shown on the X and Y axes, respectively. You can tweak the styles for axis labels the same way you did with the title, subtitle, and caption. The snippet below shows how:

Image 9 - Changing X and Y axis labels

Image 9 &#; Changing X and Y axis labels

And that&#;s it for styling axes! Let&#;s see how to show multiple lines on the same chart next.

Draw Multiple Lines on the Same Chart

Showing multiple lines on a single chart can be useful. We&#;ll use it to compare average life expectancy between major North American countries &#; the United States, Canada, and Mexico.

To display multiple lines, you can use the attribute in the data aesthetics layer. Here&#;s an example:

Image 10 - Average life expectancy among major North American countries

Image 10 &#; Average life expectancy among major North American countries

In case you&#;re wondering how to add markers to multiple lines &#; the procedure is identical as it was for a single one. Take a look at the code snippet and image below:

Image 11 - Adding markers to multiple lines

Image 11 &#; Adding markers to multiple lines

There&#;s a legend right next to the plot because of multiple lines on a single chart. You wouldn&#;t know which line represents what without it. Still, it&#;s position on the right might be irritating for some use cases. Here&#;s how to put it on the top:

Image 12 - Changing the legend position

Image 12 &#; Changing the legend position

You&#;ve learned a lot until now, but there&#;s still one important topic to cover &#; labels.

Adding Labels

If there aren&#;t too many data points on a line chart, it can be useful to add labels showing the exact values. Be careful with them &#; they can make your visualization messy fast. 

Here&#;s how to plot average life expectancy in the United States and show text on top of the line:

Image 13 - Adding text

Image 13 &#; Adding text

A couple of problems, though. The labels are a bit small, and they are positioned right on top of the markers. The code snippet below makes the text larger and pushes them a bit higher:

Image 14 - Styling text

Image 14 &#; Styling text

Showing text might not be the cleanest solution every time. Maybe you want text wrapped inside a box to give your visualization a touch more style. You can do that by replacing with . That&#;s the only change you need to make:

Image 15 - Replacing text with labels

Image 15 &#; Replacing text with labels

And that&#;s all you really need to know about labels and line charts for today. Let&#;s wrap things up. 

Conclusion

Today you&#;ve learned how to make line charts and how to make them aesthetically pleasing. You&#;ve learned how to change colors, line width and type, titles, subtitles, captions, axis labels, and much more. You are now ready to include line charts in your reports and dashboards. You can expect more basic R tutorials weekly (usually on Sundays) and more advanced tutorials throughout the week. Fill out the subscribe form below so you never miss an update.

Are you completely new to R but have some programming experience? Check out our detailed R guide for programmers.

Learn More:

Appsilon is hiring globally! We are primarily seeking an Engineering Manager who can lead a team of ambitious software engineers. See our Careers page for all new openings, including openings for a Project Manager and Community Manager.



Join a team of motivated and talented explorers

Senior R Shiny Developer
(Global/Remote)

Join a team of motivated and talented explorers

Senior R Shiny Developer
(Global/Remote)
Sours: https://appsilon.com/ggplot2-line-charts/


2863 2864 2865 2866 2867