Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Line Plots and Trend Lines | section
Hands-On Data Visualization with ggplot2 in R
Seksjon 1. Kapittel 6
single

single

bookLine Plots and Trend Lines

Sveip for å vise menyen

Line plots are essential for visualizing time series data, where the order of observations is important and typically mapped to the x-axis. You use line plots to show how a variable changes over time, such as tracking sales by month or monitoring temperature by day. In these plots, points are connected in the order of the x-variable, making it easy to see patterns, trends, and fluctuations.

123456789101112
library(ggplot2) # Example time series data: monthly sales sales_data <- data.frame( month = 1:12, sales = c(120, 135, 150, 160, 170, 180, 175, 165, 155, 145, 140, 130) ) # Basic line plot ggplot(sales_data, aes(x = month, y = sales)) + geom_line(color = "blue", linewidth = 1) + labs(title = "Monthly Sales Over a Year", x = "Month", y = "Sales")
copy
1234567891011
library(ggplot2) sales_data <- data.frame( month = 1:12, sales = c(120, 135, 150, 160, 170, 180, 175, 165, 155, 145, 140, 130) ) # Add a trend line using geom_smooth() ggplot(sales_data, aes(x = month, y = sales)) + geom_line(color = "blue", linewidth = 1) + geom_smooth(method = "loess", se = FALSE, color = "red", linetype = "dashed") + labs(title = "Monthly Sales with Trend Line", x = "Month", y = "Sales")
copy

When you interpret line plots, look for patterns such as upward or downward trends, cycles, or sudden changes. Adding a trend line with geom_smooth() helps you see the underlying direction of the data, filtering out short-term fluctuations. The method argument in geom_smooth() lets you choose the smoothing technique, such as "loess" for flexible local regression or "lm" for a straight linear fit. Understanding the shape and direction of the trend line can guide your analysis and help you make informed decisions based on time series data.

Note
Note

The parameters in geom_smooth control the appearance and calculation of the trend line:

  • method = "loess": chooses the local regression smoothing technique, which fits a smooth curve that adapts to local patterns in the data;
  • se = FALSE: hides the shaded confidence interval around the trend line, making the plot less cluttered;
  • color = "red": sets the trend line color to red for clear visual distinction;
  • linetype = "dashed": draws the trend line as a dashed line instead of a solid one, helping it stand out from the main data line.
Oppgave

Sveip for å begynne å kode

Use the given temperature_data data frame to create a line plot of daily average temperatures over 10 days.

  • Use ggplot2 to create the plot.
  • Map day to the x-axis and temperature to the y-axis.
  • Add a blue line for the temperature values using geom_line().
  • Add a trend line with geom_smooth() using:
    • method = "loess";
    • se = FALSE;
    • darkgreen color;
    • type of line dotdash.
  • Assign your plot to a variable named temperature_plot.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 6
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt