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?
- How do I select the first row in an R data frame that meets certain criteria?
- Poker hand range chart visualization in R
- Critical t values in R
- Error in
: object of type ‘closure’ is not subsettable - What does %>% function mean in R?
- How to join (merge) data frames (inner, outer, left, right)
- rbind error: “names do not match previous names”
- rbind error: “names do not match previous names”
- R Error in x$ed : $ operator is invalid for atomic vectors
- Plotting with ggplot2: “Error: Discrete value supplied to continuous scale” on categorical y-axis
- ggplot2 error : Discrete value supplied to continuous scale
- What does na.rm=TRUE actually means?
- Remove legend ggplot 2.2
- How do I replace NA values with zeros in an R dataframe?
- R on MacOS Error: vector memory exhausted (limit reached?)
- Why use as.factor() instead of just factor()
- Update R using RStudio
- mean() warning: argument is not numeric or logical: returning NA
- ‘x’ and ‘y’ lengths differ ERROR when plotting
- Why do I get “number of items to replace is not a multiple of replacement length”
- Drop data frame columns by name
- How can two strings be concatenated?
- Remove a row from a data table in R
- “Error: Continuous value supplied to discrete scale” in default data set example mtcars and ggplot2
- Why am I getting “algorithm did not converge” and “fitted prob numerically 0 or 1” warnings with glm?
- How does one reorder columns in a data frame?
- Arcsine transformation of percentage data
- R loop that adds following numbers to each element in list
- Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) 0 non-na cases
- Can I calculate z-score with R?
- Using Caret Package but Getting Error in library(e1071)
- Function to clear the console in R and RStudio
- plot.new has not been called yet
- R: need finite ‘ylim’ values in function
- How to save a data frame as CSV to a user selected location using tcltk
- Convert a list to a data frame
- Why I get this error writing data to a file
- Remove NA values from a vector
- character string is not in a standard unambiguous format
- Remove rows with all or some NAs (missing values) in data.frame
- duplicate ‘row.names’ are not allowed error
- How to count the number of observations in R like Stata command count
- Error: unexpected ‘}’ in “}” in R [duplicate]
- Error : length of ‘dimnames’ [2] not equal to array extent
- Mean of a column in a data frame, given the column’s name
- Center Plot title in ggplot2
- What is the difference between rm() and rm(list=ls())?
- Subscript out of bounds – general definition and solution?
- Understanding `scale` in R
- Error with apply function
- file.path function in R
- Reshaping data.frame from wide to long format
- Remove rows with all or some NAs (missing values) in data.frame
- Converting data frame column from character to numeric
- How to set limits for axes in ggplot2 R plots?
- Rotating and spacing axis labels in ggplot2
- duplicate ‘row.names’ are not allowed error
- How to sum data.frame column values?
- Error: invalid subscript type ‘list’ in R
- Is there a dictionary functionality in R
- “non-numeric argument to binary operator” error in R
- Position-dodge warning with ggplot boxplot?
- ggplot2 manually specifying colour with geom_line
- ggplot geom_text font size control
- How to update a package in R?
- Error: Invalid input: date_trans works with objects of class Date only
- Matrix expression causes error “requires numeric/complex matrix/vector arguments”?
- NAs are not allowed in subscripted assignments
- Unused arguments in R
- How to convert a data frame column to numeric type?
- Boxplot dplyr: Error: non-numeric argument to binary operator
- Error: ggplot2 doesn’t know how to deal with data of class matrix?
- lmer error: grouping factor must be < number of observations
- Aggregate multiple columns at once
- installing data.table results in “there is no package”
- What is integer overflow in R and how can it happen?
- Creating box plot on exercise
- changing title in multiplot ggplot2 using grid.arrange
- How to split data into training/testing sets using sample function
- How to sort a data frame by alphabetic order of a character variable in R?
- Error in na.fail.default(as.ts(x)) : missing values in object in time series forecasting
- Error in contrib.url(repos, “source”) in R trying to use CRAN without setting a mirror Calls: install.packages -> contrib.url Execution halted
- Geometric Mean: is there a built-in?
- Error in xj[i] : invalid subscript type ‘list’
- Error in eval(predvars, data, env) : invalid ‘envir’ argument of type ‘character’
- What does Continuous x aesthetic — did you forget aes(group=…) mean?
- IF “OR” multiple conditions
- How can I make a list of lists in R?
- Merge data frames based on rownames in R
- The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
- Subset and ggplot2
- How do I print a hexadecimal number with leading 0 to have width 2 using sprintf?
- How to deal with “data of class uneval” error from ggplot2?