

The four plots generated by the code are (in order p1, p2, then p3 and p4): 8 ), # And put a box around it legend.background = element_rect ( color = "black" )) + # Retitle the legend that pops up to explain the discrete (factor) difference in colors # (note if we just want a name change we could do guides(color = guide_legend(title = 'Random Factor')) instead) scale_color_manual ( name = "Random Factor", # And specify the colors for the factor levels (1 and 2) by hand if we like values = c ( "1" = "red", "2" = "blue" )) p4 # Put them all together with cowplot for LOST upload plot_grid ( p1, p2, p3, p4, nrow = 2 ) p2 <- ggplot () + # choose a color of preference geom_line ( aes ( x = date, y = uempmed ), color = "navyblue", data = eco_df ) + # add chart title and change axes labels labs ( title = "Median Duration of Unemployment", x = "Date", y = "" ) + # Add a ggplot theme theme_light () # center the chart title theme ( plot.title = element_text ( hjust = 0.5 )) + p2 # plotting multiple charts (of different line types and sizes) p3 <- ggplot () + geom_line ( aes ( x = date, y = uempmed ), color = "navyblue", size = 1.5, data = eco_df ) + geom_line ( aes ( x = date, y = psavert ), color = "red2", linetype = "dotted", size = 0.8, data = eco_df ) + labs ( title = "Unemployment Duration (Blue) and Savings Rate (Red)", x = "Date", y = "" ) + theme_light () + theme ( plot.title = element_text ( hjust = 0.5 )) p3 # Plotting a different line type for each group # There isn't a natural factor in this data so let's just duplicate the data and make one up eco_df $ fac <- factor ( 1, levels = c ( 1, 2 )) eco_df2 <- eco_df eco_df2 $ fac <- 2 eco_df2 $ uempmed <- eco_df2 $ uempmed - 2 + rnorm ( nrow ( eco_df2 )) eco_df <- rbind ( eco_df, eco_df2 ) p4 <- ggplot () + # This time, color goes inside aes geom_line ( aes ( x = date, y = uempmed, color = fac ), data = eco_df ) + # add chart title and change axes labels labs ( title = "Median Duration of Unemployment", x = "Date", y = "" ) + # Add a ggplot theme theme_light () + # center the chart title theme ( plot.title = element_text ( hjust = 0.5 ), # Move the legend onto some blank space on the diagram legend.position = c (. # If necessary # install.packages(c('ggplot2','cowplot')) # load packages library ( ggplot2 ) # Cowplot is just to join together the four graphs at the end library ( cowplot ) # load data (the Economics dataset comes with ggplot2) eco_df <- economics # basic plot p1 <- ggplot () + geom_line ( aes ( x = date, y = uempmed ), data = eco_df ) p1 # Change line color and chart labels # Note here that color is *outside* of the aes() argument, and so this will color the line # If color were instead *inside* aes() and set to a factor variable, ggplot would create # a different line for each value of the factor variable, colored differently. This site uses Just the Docs, a documentation theme for Jekyll. Import a Delimited Data File (CSV, TSV).Graphing a By-Group or Over-Time Summary Statistic.Marginal Effects Plots for Interactions with Continuous Variables.


Marginal effects plots for interactions with categorical variables.Line Graph with Labels at the Beginning or End of Lines.Marginal Effects in Nonlinear Regression.Density Discontinuity Tests for Regression Discontinuity.Random/Mixed Effects in Linear Regression.McFadden's Choice Model (Alternative-Specific Conditional Logit).Determine the Observation Level of a Data Set.Creating a Variable with Group Calculations.
