R Plots Not Showing

broken image


The settings of the plot are usually controlled by the par function (see?par for the many possible arguments), once the arguments are set in par they apply to all subsequent plots. Some arguments in par (for example cex.axis) can also be set in other plot functions like axis or text.When these arguments are set in these other functions they will then apply only to the current plot.

This tutorial uses ggplot2 to create customized plots of time series data. We will learn how to adjust x- and y-axis ticks using the scalespackage, how to add trend lines to a scatter plot and how to customize plotlabels, colors and overall plot appearance using ggthemes.

Not

Learning Objectives

  • The R ggplot2 dot Plot or dot chart consists of a data point drawn on a specified scale. Let me show how to Create an R ggplot dotplot, Format its colors, plot horizontal dot plots with an example. For this R ggplot2 Dot Plot demonstration, we use the airquality data set provided by the R.
  • In the simplest case, we can pass in a vector and we will get a scatter plot of magnitude vs index. But generally, we pass in two vectors and a scatter plot of these points are plotted. For example, the command plot(c(1,2),c(3,5)) would plot the points (1,3) and (2,5). Here is a more concrete example where we plot a sine function form range -pi.

After completing this tutorial, you will be able to:

  • Create basic time series plots using ggplot() in R.
  • Explain the syntax of ggplot() and know how to find out more about thepackage.
  • Plot data using scatter and bar plots.

Things You'll Need To Complete This Tutorial

You will need the most current version of R and, preferably, RStudio loaded onyour computer to complete this tutorial.

Install R Packages

  • lubridate:install.packages('lubridate')
  • ggplot2:install.packages('ggplot2')
  • scales:install.packages('scales')
  • gridExtra:install.packages('gridExtra')
  • ggthemes:install.packages('ggthemes')

More on Packages in R – Adapted from Software Carpentry.

Download Data

NEON Teaching Data Subset: Meteorological Data for Harvard Forest

The data used in this lesson were collected at the National Ecological Observatory Network's Harvard Forest field site. These data are proxy data for what will be available for 30 years on the NEON data portalfor the Harvard Forest and other field sites located across the United States.

Set Working Directory: This lesson assumes that you have set your working directory to the location of the downloaded and unzipped data subsets.

R Script & Challenge Code: NEON data lessons often contain challenges that reinforce learned skills. If available, the code for challenge solutions is found in thedownloadable R script of the entire lesson, available in the footer of each lesson page.

Additional Resources

  • Winston Chang'sCookbook for R site based on his R Graphics Cookbook text.
  • The NEON Data Skills tutorial on Interactive Data Viz Using R, ggplot2 and PLOTLY.
  • Data Carpentry's Data Visualization with ggplot2 lesson.
  • Hadley Wickham's documentation on the ggplot2 package.

Plotting Time Series Data

Plotting our data allows us to quickly see general patterns including outlier points and trends. Plots are also a useful way to communicate the results of our research. ggplot2 is a powerful R package that we use to create customized, professional plots.

Load the Data

Showing

We will use the lubridate, ggplot2, scales and gridExtra packages inthis tutorial.

Our data subset will be the daily meteorology data for 2009-2011 for the NEONHarvard Forest field site(NEON-DS-Met-Time-Series/HARV/FisherTower-Met/Met_HARV_Daily_2009_2011.csv).If this subset is not already loaded, please load it now.

Plot with qplot

We can use the qplot() function in the ggplot2 package to quickly plot avariable such as air temperature (airt) across all three years of our dailyaverage time series data.

The resulting plot displays the pattern of air temperature increasing and decreasing over three years. While qplot() is a quick way to plot data, our ability to customize the output is limited.

Plot with ggplot

The ggplot() function within the ggplot2 package gives us more controlover plot appearance. However, to use ggplot we need to learn a slightly different syntax. Three basic elements are needed for ggplot() to work:

  1. The data_frame: containing the variables that we wish to plot,
  2. aes (aesthetics): which denotes which variables will map to the x-, y-(and other) axes,
  3. geom_XXXX (geometry): which defines the data's graphical representation(e.g. points (geom_point), bars (geom_bar), lines (geom_line), etc).

The syntax begins with the base statement that includes the data_frame(harMetDaily.09.11) and associated x (date) and y (airt) variables to beplotted:

ggplot(harMetDaily.09.11, aes(date, airt))

To successfully plot, the last piece that is needed is the geometry type. In this case, we want to create a scatterplot so we can add + geom_point().

Let's create an air temperature scatterplot.

Customize A Scatterplot

We can customize our plot in many ways. For instance, we can change the size and color of the points using size=, shape pch=, and color= in the geom_pointelement.

geom_point(na.rm=TRUE, color='blue', size=1)

Modify Title & Axis Labels

We can modify plot attributes by adding elements using the + symbol.For example, we can add a title by using + ggtitle='TEXT', and axislabels using + xlab('TEXT') + ylab('TEXT').

Data Tip: Use help(ggplot2) to review the manyelements that can be defined and added to a ggplot2 plot.

Name Plot Objects

We can create a ggplot object by assigning our plot to an object name.When we do this, the plot will not render automatically. To render the plot, weneed to call it in the code.

Double u casino slots games free. Assigning plots to an R object allows us to effectively add on to, and modify the plot later. Let's create a new plot and call it AirTempDaily.

Format Dates in Axis Labels

We can adjust the date display format (e.g. 2009-07 vs. Jul 09) and the number of major and minor ticks for axis date values using scale_x_date. Let'sformat the axis ticks so they read 'month year' (%b %y). To do this, we will use the syntax:

scale_x_date(labels=date_format('%b %y')

Rather than re-coding the entire plot, we can add the scale_x_date elementto the plot object AirTempDaily that we just created.

Data Tip: You can type ?strptime into the R console to find a list of date format conversion specifications (e.g. %b = month).Type scale_x_date for a list of parameters that allow you to format dates on the x-axis.

Data Tip: If you are working with a date & timeclass (e.g. POSIXct), you can use scale_x_datetime instead of scale_x_date.

Adjust Date Ticks

We can adjust the date ticks too. In this instance, having 1 tick per year may be enough. If we have the scales package loaded, we can use breaks=date_breaks('1 year') within the scale_x_date element to create a tick for every year. We can adjust this as needed (e.g. 10 days, 30 days, 1 month).

From R HELP (?date_breaks): width an interval specification, one of 'sec', 'min', 'hour', 'day', 'week', 'month', 'year'. Can be by an integer and a space, or followed by 's'.

Data Tip: We can adjust the tick spacing andformat for x- and y-axes using scale_x_continuous or scale_y_continuous toformat a continue variable. Check out ?scale_x_ (tab complete to view the various x and y scale options)

ggplot - Subset by Time

Sometimes we want to scale the x- or y-axis to a particular time subset without subsetting the entire data_frame. To do this, we can define start and end times. We can then define the limits in the scale_x_date object as follows:

scale_x_date(limits=start.end) +

ggplot() Themes

We can use the theme() element to adjust figure elements.There are some nice pre-defined themes that we can use as a starting place.

Using the theme_bw() we now have a white background rather than grey.

Import New Themes BonusTopic

There are externally developed themes built by the R community that are worthmentioning. Feel free to experiment with the code below to install ggthemes.

More on Themes

Customize ggplot Themes

We can customize theme elements manually too. Let's customize the font size and style.

Challenge: Plot Total Daily Precipitation

Create a plot of total daily precipitation using data in the harMetDaily.09.11data_frame.

  • Format the dates on the x-axis: Month-Year.
  • Create a plot object called PrecipDaily.
  • Be sure to add an appropriate title in addition to x and y axis labels.
  • Increase the font size of the plot text and adjust the number of ticks on thex-axis.

Bar Plots with ggplot

We can use ggplot to create bar plots too. Let's create a bar plot of total daily precipitation next. A bar plot might be a better way to represent a totaldaily value. To create a bar plot, we change the geom element fromgeom_point() to geom_bar().

The default setting for a ggplot bar plot - geom_bar() - is a histogramdesignated by stat='bin'. However, in this case, we want to plot actualprecipitation values. We can use geom_bar(stat='identity') to force ggplot toplot actual values.

Note that some of the bars in the resulting plot appear grey rather than black.This is because R will do it's best to adjust colors of bars that are closelyspaced to improve readability. If we zoom into the plot, all of the bars areblack.

Challenge: Plot with scale_x_data()

Without creating a subsetted dataframe, plot the precipitation data for 2010 only. Customize the plot with:

  • a descriptive title and axis labels,
  • breaks every 4 months, and
  • x-axis labels as only the full month (spelled out).
R Plots Not Showing

HINT: you will need to rebuild the precipitation plot as you will have to specify a new scale_x_data() element.

Bonus: Style your plot with a ggtheme of choice.

Color

We can change the bar fill color by within thegeom_bar(colour='blue') element. We can also specify a separate fill and linecolor using fill= and line=. Colors can be specified by name (e.g., 'blue') or hexadecimal color codes (e.g, #FF9999).

There are many color cheatsheets out there to help with color selection!

Data Tip: For more information on color,including color blind friendly color palettes, checkout the ggplot2 color information from Winston Chang's CookbookforR site based on the RGraphicsCookbook text.

Figures with Lines

We can create line plots too using ggplot. To do this, we use geom_line()instead of bar or point.

R Plots Not Showing Fractions

Note that lines may not be the best way to represent air temperature data givenlines suggest that the connecting points are directly related. It is importantto consider what type of plot best represents the type of data that you arepresenting.

Challenge: Combine Points & Lines

You can combine geometries within one plot. For example, you can have ageom_line() and geom_point element in a plot. Add geom_line(na.rm=TRUE) tothe AirTempDaily, a geom_point plot. What happens?

R Shiny Plot Not Showing

Trend Lines

We can add a trend line, which is a statistical transformation of our data torepresent general patterns, using stat_smooth(). stat_smooth() requires astatistical method as follows:

  • For data with < 1000 observations: the default model is a loess model(a non-parametric regression model)
  • For data with > 1,000 observations: the default model is a GAM (a generaladditive model)
  • A specific model/method can also be specified: for example, a linear regression (method='lm').

For this tutorial, we will use the default trend line model. The gam method will be used with given we have 1,095 measurements.

Data Tip: Remember a trend line is a statisticaltransformation of the data, so prior to adding the line one must understand if a particular statistical transformation is appropriate for the data.

Challenge: A Trend in Precipitation?

Create a bar plot of total daily precipitation. Add a:

  • Trend line for total daily precipitation.
  • Make the bars purple (or your favorite color!).
  • Make the trend line grey (or your other favorite color).
  • Adjust the tick spacing and format the dates to appear as 'Jan 2009'.
  • Render the title in italics.

Challenge: Plot Monthly Air Temperature

Plot the monthly air temperature across 2009-2011 using theharTemp.monthly.09.11 data_frame. Name your plot 'AirTempMonthly'. Be sure tolabel axes and adjust the plot ticks as you see fit.

Display Multiple Figures in Same Panel

R Plots Not Showing The Number

It is often useful to arrange plots in a panel rather than displaying them individually. In base R, we use par(mfrow=()) to accomplish this. Howeverthe grid.arrange() function from the gridExtra package provides a moreefficient approach!

grid.arrange requires 2 things:1. the names of the plots that you wish to render in the panel.2. the number of columns (ncol).

grid.arrange(plotOne, plotTwo, ncol=1)

R Plot Lines Not Showing

Let's plot AirTempMonthly and AirTempDaily on top of each other. To do this,we'll specify one column.

Challenge: Create Panel of Plots

Not

Learning Objectives

  • The R ggplot2 dot Plot or dot chart consists of a data point drawn on a specified scale. Let me show how to Create an R ggplot dotplot, Format its colors, plot horizontal dot plots with an example. For this R ggplot2 Dot Plot demonstration, we use the airquality data set provided by the R.
  • In the simplest case, we can pass in a vector and we will get a scatter plot of magnitude vs index. But generally, we pass in two vectors and a scatter plot of these points are plotted. For example, the command plot(c(1,2),c(3,5)) would plot the points (1,3) and (2,5). Here is a more concrete example where we plot a sine function form range -pi.

After completing this tutorial, you will be able to:

  • Create basic time series plots using ggplot() in R.
  • Explain the syntax of ggplot() and know how to find out more about thepackage.
  • Plot data using scatter and bar plots.

Things You'll Need To Complete This Tutorial

You will need the most current version of R and, preferably, RStudio loaded onyour computer to complete this tutorial.

Install R Packages

  • lubridate:install.packages('lubridate')
  • ggplot2:install.packages('ggplot2')
  • scales:install.packages('scales')
  • gridExtra:install.packages('gridExtra')
  • ggthemes:install.packages('ggthemes')

More on Packages in R – Adapted from Software Carpentry.

Download Data

NEON Teaching Data Subset: Meteorological Data for Harvard Forest

The data used in this lesson were collected at the National Ecological Observatory Network's Harvard Forest field site. These data are proxy data for what will be available for 30 years on the NEON data portalfor the Harvard Forest and other field sites located across the United States.

Set Working Directory: This lesson assumes that you have set your working directory to the location of the downloaded and unzipped data subsets.

R Script & Challenge Code: NEON data lessons often contain challenges that reinforce learned skills. If available, the code for challenge solutions is found in thedownloadable R script of the entire lesson, available in the footer of each lesson page.

Additional Resources

  • Winston Chang'sCookbook for R site based on his R Graphics Cookbook text.
  • The NEON Data Skills tutorial on Interactive Data Viz Using R, ggplot2 and PLOTLY.
  • Data Carpentry's Data Visualization with ggplot2 lesson.
  • Hadley Wickham's documentation on the ggplot2 package.

Plotting Time Series Data

Plotting our data allows us to quickly see general patterns including outlier points and trends. Plots are also a useful way to communicate the results of our research. ggplot2 is a powerful R package that we use to create customized, professional plots.

Load the Data

We will use the lubridate, ggplot2, scales and gridExtra packages inthis tutorial.

Our data subset will be the daily meteorology data for 2009-2011 for the NEONHarvard Forest field site(NEON-DS-Met-Time-Series/HARV/FisherTower-Met/Met_HARV_Daily_2009_2011.csv).If this subset is not already loaded, please load it now.

Plot with qplot

We can use the qplot() function in the ggplot2 package to quickly plot avariable such as air temperature (airt) across all three years of our dailyaverage time series data.

The resulting plot displays the pattern of air temperature increasing and decreasing over three years. While qplot() is a quick way to plot data, our ability to customize the output is limited.

Plot with ggplot

The ggplot() function within the ggplot2 package gives us more controlover plot appearance. However, to use ggplot we need to learn a slightly different syntax. Three basic elements are needed for ggplot() to work:

  1. The data_frame: containing the variables that we wish to plot,
  2. aes (aesthetics): which denotes which variables will map to the x-, y-(and other) axes,
  3. geom_XXXX (geometry): which defines the data's graphical representation(e.g. points (geom_point), bars (geom_bar), lines (geom_line), etc).

The syntax begins with the base statement that includes the data_frame(harMetDaily.09.11) and associated x (date) and y (airt) variables to beplotted:

ggplot(harMetDaily.09.11, aes(date, airt))

To successfully plot, the last piece that is needed is the geometry type. In this case, we want to create a scatterplot so we can add + geom_point().

Let's create an air temperature scatterplot.

Customize A Scatterplot

We can customize our plot in many ways. For instance, we can change the size and color of the points using size=, shape pch=, and color= in the geom_pointelement.

geom_point(na.rm=TRUE, color='blue', size=1)

Modify Title & Axis Labels

We can modify plot attributes by adding elements using the + symbol.For example, we can add a title by using + ggtitle='TEXT', and axislabels using + xlab('TEXT') + ylab('TEXT').

Data Tip: Use help(ggplot2) to review the manyelements that can be defined and added to a ggplot2 plot.

Name Plot Objects

We can create a ggplot object by assigning our plot to an object name.When we do this, the plot will not render automatically. To render the plot, weneed to call it in the code.

Double u casino slots games free. Assigning plots to an R object allows us to effectively add on to, and modify the plot later. Let's create a new plot and call it AirTempDaily.

Format Dates in Axis Labels

We can adjust the date display format (e.g. 2009-07 vs. Jul 09) and the number of major and minor ticks for axis date values using scale_x_date. Let'sformat the axis ticks so they read 'month year' (%b %y). To do this, we will use the syntax:

scale_x_date(labels=date_format('%b %y')

Rather than re-coding the entire plot, we can add the scale_x_date elementto the plot object AirTempDaily that we just created.

Data Tip: You can type ?strptime into the R console to find a list of date format conversion specifications (e.g. %b = month).Type scale_x_date for a list of parameters that allow you to format dates on the x-axis.

Data Tip: If you are working with a date & timeclass (e.g. POSIXct), you can use scale_x_datetime instead of scale_x_date.

Adjust Date Ticks

We can adjust the date ticks too. In this instance, having 1 tick per year may be enough. If we have the scales package loaded, we can use breaks=date_breaks('1 year') within the scale_x_date element to create a tick for every year. We can adjust this as needed (e.g. 10 days, 30 days, 1 month).

From R HELP (?date_breaks): width an interval specification, one of 'sec', 'min', 'hour', 'day', 'week', 'month', 'year'. Can be by an integer and a space, or followed by 's'.

Data Tip: We can adjust the tick spacing andformat for x- and y-axes using scale_x_continuous or scale_y_continuous toformat a continue variable. Check out ?scale_x_ (tab complete to view the various x and y scale options)

ggplot - Subset by Time

Sometimes we want to scale the x- or y-axis to a particular time subset without subsetting the entire data_frame. To do this, we can define start and end times. We can then define the limits in the scale_x_date object as follows:

scale_x_date(limits=start.end) +

ggplot() Themes

We can use the theme() element to adjust figure elements.There are some nice pre-defined themes that we can use as a starting place.

Using the theme_bw() we now have a white background rather than grey.

Import New Themes BonusTopic

There are externally developed themes built by the R community that are worthmentioning. Feel free to experiment with the code below to install ggthemes.

More on Themes

Customize ggplot Themes

We can customize theme elements manually too. Let's customize the font size and style.

Challenge: Plot Total Daily Precipitation

Create a plot of total daily precipitation using data in the harMetDaily.09.11data_frame.

  • Format the dates on the x-axis: Month-Year.
  • Create a plot object called PrecipDaily.
  • Be sure to add an appropriate title in addition to x and y axis labels.
  • Increase the font size of the plot text and adjust the number of ticks on thex-axis.

Bar Plots with ggplot

We can use ggplot to create bar plots too. Let's create a bar plot of total daily precipitation next. A bar plot might be a better way to represent a totaldaily value. To create a bar plot, we change the geom element fromgeom_point() to geom_bar().

The default setting for a ggplot bar plot - geom_bar() - is a histogramdesignated by stat='bin'. However, in this case, we want to plot actualprecipitation values. We can use geom_bar(stat='identity') to force ggplot toplot actual values.

Note that some of the bars in the resulting plot appear grey rather than black.This is because R will do it's best to adjust colors of bars that are closelyspaced to improve readability. If we zoom into the plot, all of the bars areblack.

Challenge: Plot with scale_x_data()

Without creating a subsetted dataframe, plot the precipitation data for 2010 only. Customize the plot with:

  • a descriptive title and axis labels,
  • breaks every 4 months, and
  • x-axis labels as only the full month (spelled out).

HINT: you will need to rebuild the precipitation plot as you will have to specify a new scale_x_data() element.

Bonus: Style your plot with a ggtheme of choice.

Color

We can change the bar fill color by within thegeom_bar(colour='blue') element. We can also specify a separate fill and linecolor using fill= and line=. Colors can be specified by name (e.g., 'blue') or hexadecimal color codes (e.g, #FF9999).

There are many color cheatsheets out there to help with color selection!

Data Tip: For more information on color,including color blind friendly color palettes, checkout the ggplot2 color information from Winston Chang's CookbookforR site based on the RGraphicsCookbook text.

Figures with Lines

We can create line plots too using ggplot. To do this, we use geom_line()instead of bar or point.

R Plots Not Showing Fractions

Note that lines may not be the best way to represent air temperature data givenlines suggest that the connecting points are directly related. It is importantto consider what type of plot best represents the type of data that you arepresenting.

Challenge: Combine Points & Lines

You can combine geometries within one plot. For example, you can have ageom_line() and geom_point element in a plot. Add geom_line(na.rm=TRUE) tothe AirTempDaily, a geom_point plot. What happens?

R Shiny Plot Not Showing

Trend Lines

We can add a trend line, which is a statistical transformation of our data torepresent general patterns, using stat_smooth(). stat_smooth() requires astatistical method as follows:

  • For data with < 1000 observations: the default model is a loess model(a non-parametric regression model)
  • For data with > 1,000 observations: the default model is a GAM (a generaladditive model)
  • A specific model/method can also be specified: for example, a linear regression (method='lm').

For this tutorial, we will use the default trend line model. The gam method will be used with given we have 1,095 measurements.

Data Tip: Remember a trend line is a statisticaltransformation of the data, so prior to adding the line one must understand if a particular statistical transformation is appropriate for the data.

Challenge: A Trend in Precipitation?

Create a bar plot of total daily precipitation. Add a:

  • Trend line for total daily precipitation.
  • Make the bars purple (or your favorite color!).
  • Make the trend line grey (or your other favorite color).
  • Adjust the tick spacing and format the dates to appear as 'Jan 2009'.
  • Render the title in italics.

Challenge: Plot Monthly Air Temperature

Plot the monthly air temperature across 2009-2011 using theharTemp.monthly.09.11 data_frame. Name your plot 'AirTempMonthly'. Be sure tolabel axes and adjust the plot ticks as you see fit.

Display Multiple Figures in Same Panel

R Plots Not Showing The Number

It is often useful to arrange plots in a panel rather than displaying them individually. In base R, we use par(mfrow=()) to accomplish this. Howeverthe grid.arrange() function from the gridExtra package provides a moreefficient approach!

grid.arrange requires 2 things:1. the names of the plots that you wish to render in the panel.2. the number of columns (ncol).

grid.arrange(plotOne, plotTwo, ncol=1)

R Plot Lines Not Showing

Let's plot AirTempMonthly and AirTempDaily on top of each other. To do this,we'll specify one column.

Challenge: Create Panel of Plots

Plot AirTempMonthly and AirTempDaily next to each other rather than stackedon top of each other.

Additional ggplot2 Resources

R Plots Not Showing

In this tutorial, we've covered the basics of ggplot. There are many great resources the cover refining ggplot figures. A few are below:

R Plot Legend Not Showing

  • ggplot2 Cheatsheet from Zev Ross: ggplot2 Cheatsheet
  • ggplot2 documentation index: ggplot2 Documentation

R Plot Title Not Showing

Get Lesson Code





broken image