You only have to add group = 1
into the ggplot or geom_line aes().
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 group=1. When more variables are used and multiple lines are drawn, the grouping for lines is usually done by variable.
Reference: Cookbook for R, Chapter: Graphs Bar_and_line_graphs_(ggplot2), Line graphs.
Try this:
plot5 <- ggplot(df, aes(year, pollution, group = 1)) + geom_point() + geom_line() + labs(x = "Year", y = "Particulate matter emissions (tons)", title = "Motor vehicle emissions in Baltimore")
Related Posts:
- Emulate ggplot2 default color palette
- ggplot wrong color assignment
- ggplot2 line chart gives “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”
- Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : Polygon edge not found
- Editing legend (text) labels in ggplot
- Increase number of axis ticks
- ggplot2, facet_grid, free scales?
- R Hex to RGB converter
- Error in file(file, “rt”) : cannot open the connection [duplicate]
- t-stat for feature selection
- Could not find function “%<>%” with dplyr loaded
- Interpreting “condition has length > 1” warning from `if` function
- Error in if/while (condition) {: missing Value where TRUE/FALSE needed
- How to change legend title in ggplot
- R on MacOS Error: vector memory exhausted (limit reached?)
- Counting the number of elements with the values of x in a vector
- What does “Error: object ‘
‘ not found” mean? - Non-numeric Argument to Binary Operator Error in R
- mean() warning: argument is not numeric or logical: returning NA
- How to find the statistical mode?
- Error in colMeans(x, na.rm = TRUE) : ‘x’ must be numeric in KNN classification
- Error in file(file, “rt”) : invalid ‘description’ argument in complete.cases program
- Logistic Regression on factor: Error in eval(family$initialize) : y values must be 0 <= y <= 1
- rmarkdown error “attempt to use zero-length variable name”
- Error in lm.fit(x,y,offset = offset, singular.ok,…) 0 non-NA cases with boxcox formula
- Extract year from date
- Basic – T-Test -> Grouping Factor Must have Exactly 2 Levels
- R – longer object length is not a multiple of shorter object length
- incorrect number of dimensions and incorrect number of subscripts in array
- Rotating and spacing axis labels in ggplot2
- How to debug “contrasts can be applied only to factors with 2 or more levels” error?
- How does one reorder columns in a data frame?
- Arcsine transformation of percentage data
- R t-test Grouping factor must have exactly 2 levels error
- R loop that adds following numbers to each element in list
- R: Using equation with natural logarithm in nls
- Can I calculate z-score with R?
- What does c do in R?
- Understanding the order() function
- Using Caret Package but Getting Error in library(e1071)
- Reading PSV (pipe-separated) file or string
- Function to clear the console in R and RStudio
- Manually colouring plots with `scale_fill_manual` in ggplot2 not working
- Calculate correlation for more than two variables?
- Convert a list to a data frame
- Function to clear the console in R and RStudio
- Sum rows in data.frame or matrix
- Invalid factor level, NA generated warning
- R: “Unary operator error” from multiline ggplot2 command
- not a Stata version 5-12 .dta file
- Can’t draw Histogram, ‘x’ must be numeric
- No non-missing arguments warning when using min or max in reshape2
- What is the difference between rm() and rm(list=ls())?
- Having trouble setting working directory
- Add empty columns to a dataframe with specified names from a vector
- Convert date-time string to class Date
- Linear model function lm() error: NA/NaN/Inf in foreign function call (arg 1)
- Generate a set of random unique integers from an interval
- Singularity in backsolve at level 0, block 1 in LME model
- “installation of package ‘FILE_PATH’ had non-zero exit status” in R
- Convert categorical variables to numeric in R
- How do I get RSS from a linear model output
- Warning message: In `…` : invalid factor level, NA generated
- Plot two graphs in same plot in R
- Mean of a column in a data frame, given the column’s name
- Replace all particular values in a data frame
- Still getting error in dev.off() : cannot shut down device 1 (the null device)
- How to get summary statistics by group
- Run R script from command line
- How to avoid “Error in stripchart.default(x1, …) : invalid plotting method” error?
- ‘height’ must be a vector or a matrix. barplot error
- What causes an R script to get Killed?
- dplyr mutate with conditional values
- How do I install an R package from source?
- Getting invalid model formula in ExtractVars when using rpart function in R
- Error: ggplot2 doesn’t know how to deal with data of class matrix?
- R Error (from NA’s to 0): duplicate subscripts for column in Data Frame
- incorrect number of subscripts on matrix in R
- Error in rbind(deparse.level, …) : numbers of columns of arguments do not match R
- Correct syntax for mutate_if
- Plotting data in R; error: default method not implemented for type ‘list’
- Error:attempt to apply non-function
- R apply function with multiple parameters
- Changing fonts in ggplot2
- Error in
: target of assignment expands to non-language object - run a for loop in parallel in R
- Error in na.fail.default: missing values in object – but no missing values
- Error: Invalid number of ‘breaks’ in R
- How to eliminate “NA/NaN/Inf in foreign function call (arg 7)” running predict with randomForest
- Batch convert columns to numeric type
- Merge data frames based on rownames in R
- Error in x[[i]] : recursive indexing failed at level 2
- Merge error : negative length vectors are not allowed
- What do these R glm error messages mean: “Error: no valid set of coefficients has been found: please supply starting values”
- How to avoid “operations are possible only for numeric, logical or complex types” when computing top 3 elements in each group
- why nrow(dataframe) and length(dataframe) in r give different results?
- Meaning of objects being masked by the global environment
- Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt
- cbind warnings : row names were found from a short variable and have been discarded
- How to calculate combination and permutation in R?