ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (2024)

  • Bereid de gegevens voor
  • Basis spreidingsdiagrammen
  • Label punten in de spreidingsplot
    • Voeg regressielijnen toe
    • Verander het uiterlijk van punten en lijnen
  • Spreidingsplots met meerdere groepen
    • Wijzig de puntkleur/vorm/grootte automatisch
    • Voeg regressielijnen toe
    • Wijzig de puntkleur/vorm/grootte handmatig
  • Voeg marginale tapijten toe aan een spreidingsplot
  • Spreidingsplots met de 2D-dichtheidsschatting
  • Spreidingsplots met ellipsen
  • Scatter plots met rechthoekige bakken
  • Spreidingsplot met marginale dichtheidsverdelingsplot
  • Aangepaste spreidingsdiagrammen
  • Info's

In dit artikel wordt beschreven hoe u eenspreidingsplotgebruik makend vanR-softwareEnggplot2pakket. De functiegeom_point()is gebruikt.

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (1)


mtcarsin de onderstaande voorbeelden worden datasets gebruikt.

# Converteer cyl-kolom van een numerieke naar een factorvariabelemtcars$cyl <- as.factor(mtcars$cyl)head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4## Datsun 71 0 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17. 02 0 0 3 2## Dapper 18,1 6 225 105 2,76 3,460 20,22 1 0 3 1

Eenvoudige spreidingsplots worden gemaakt met behulp van de onderstaande R-code. De kleur, de grootte en de vorm van punten kunnen worden gewijzigd met behulp van de functiegeom_point()als volgt :

geom_point(grootte, kleur, vorm)
bibliotheek(ggplot2)# Basic scatter plotggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()# Wijzig de puntgrootte en vormggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(size=2, shape=23)

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (3)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (4)

Merk op dat de grootte van de punten kan worden geregeld door de waarden van een continue variabele, zoals in het onderstaande voorbeeld.

# Wijzig de puntgrootteggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(aes(size=qsec))

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (5)

Lees meer over puntvormen:ggplot2 puntvormen

De functiegeom_text()kan worden gebruikt :

ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + geom_text(label=rijnamen(mtcars))

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (6)

Lees meer over tekstannotaties:ggplot2 - voeg teksten toe aan een plot

Voeg regressielijnen toe

De onderstaande functies kunnen worden gebruikt om regressielijnen toe te voegen aan een spreidingsplot:

  • geom_smooth()Enstat_smooth()
  • geom_abline()

geom_abline()is al beschreven op deze link:ggplot2 voeg rechte lijnen toe aan een plot.

Alleen de functiegeom_smooth()wordt in dit gedeelte behandeld.

Een vereenvoudigd formaat is:

geom_smooth(methode="auto", se=TRUE, fullrange=FALSE, level=0.95)

  • methode: te gebruiken afvlakkingsmethode. Mogelijke waarden zijn lm, glm, gam, löss, rlm.
    • methode = "löss": Dit is de standaardwaarde voor een klein aantal waarnemingen. Het berekent een soepele lokale regressie. U kunt er meer over lezenlössmet behulp van de R-code?löss.
    • methode =“lm”: Het past eenlineair model. Merk op dat het ook mogelijk is om de formule aan te geven alsformule = y ~ poly(x, 3)om een ​​polynoom van graad 3 te specificeren.
  • met: logische waarde. Indien WAAR, wordt het betrouwbaarheidsinterval rond glad weergegeven.
  • volledig bereik: logische waarde. Indien WAAR, omvat de aanpassing het volledige bereik van de plot
  • niveau: niveau van te gebruiken betrouwbaarheidsinterval. De standaardwaarde is 0,95
# Voeg de regressielijn toeggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()+ geom_smooth(method=lm)# Verwijder het betrouwbaarheidsintervalggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()+ geom_smooth(method=lm, se=FALSE)# Löss-methodeggplot(mtcars, aes (x=wt, y=mpg)) + geom_point()+ geom_smooth()

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (7)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (8)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (9)

Verander het uiterlijk van punten en lijnen

In dit gedeelte wordt beschreven hoe u kunt wijzigen:

  • de kleur en de vorm van punten
  • het lijntype en de kleur van de regressielijn
  • de vulkleur van het betrouwbaarheidsinterval
# Verander de puntkleuren en -vormen# Verander het lijntype en colorggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(shape=18, color="blue")+ geom_smooth(method=lm, se=FALSE, linetype="dashed", color="darkred")# Verander de betrouwbaarheidsinterval vulling colorggplot(mtcars, aes(x=wt, y=mpg)) + geom_point( shape=18, color="blue")+ geom_smooth(method=lm, linetype="dashed", color="darkred", fill="blue")

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (10)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (11)

Merk op dat er standaard een transparante kleur wordt gebruikt voor de betrouwbaarheidsband. Dit kan worden gewijzigd door het argument te gebruikenalfa:geom_smooth(fill=“blauw”, alpha=1)

Lees meer over puntvormen:ggplot2 puntvormen

Lees meer over lijntypen:ggplot2 lijntypen

In dit gedeelte wordt beschreven hoe u puntkleuren en -vormen automatisch en handmatig kunt wijzigen.

Wijzig de puntkleur/vorm/grootte automatisch

In de onderstaande R-code worden puntvormen, kleuren en afmetingen bepaald door de niveaus van de factorvariabelecil:

# Verander puntvormen door de niveaus van cylggplot(mtcars, aes(x=wt, y=mpg, shape=cyl)) + geom_point()# Verander puntvormen en kleurenggplot(mtcars, aes(x=wt, y=mpg, shape=cyl, color=cyl)) + geom_point()# Verander puntvormen, kleuren en matenggplot(mtcars, aes(x=wt, y=mp g, vorm=cyl, kleur=cyl, maat=cyl)) + geom_point()

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (12)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (13)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (14)

Voeg regressielijnen toe

Regressie lijnenkan als volgt worden toegevoegd:

# Voeg regressielijnen toeggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm)# Verwijder betrouwbaarheidsintervallen# Verleng de regressielijnenggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(meth od=lm, se=ONWAAR, volledig bereik=WAAR)

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (15)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (16)

Merk op dat u ook het lijntype van de regressielijnen kunt wijzigen door de esthetiek te gebruikenlijntype = cil.

De vulkleur van betrouwbaarheidsbanden kan als volgt worden gewijzigd:

ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm, aes(fill=cyl))

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (17)

Wijzig de puntkleur/vorm/grootte handmatig

Onderstaande functies worden gebruikt:

  • scale_shape_manual()voor puntvormen
  • scale_color_manual()voor puntkleuren
  • scale_size_manual()voor puntmaten
# Verander puntvormen en kleuren handmatigggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+ scale_shape_manual(values=c(3, 16, 17))+ scale_color_manual(values=c('#999999',' #E69F00', '#56B4E9'))+ theme(legend.position="top") # Wijzig de puntgroottes handmatigggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl))+ geom_point(aes(size=cyl)) + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+ scale_shape_manual(values=c (3, 16, 17))+ scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+ scale_size_manual(values=c(2,3,4))+ thema(legend.position="top")

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (18)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (19)

Het is ook mogelijk omhandmatig punt- en lijnkleuren wijzigenmet behulp van de functies:

  • schaal_kleur_brewer(): om kleurenpaletten van te gebruikenRColorBrewerpakket
  • schaal_kleur_grijs(): om grijze kleurenpaletten te gebruiken
p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+ theme_classic()# Gebruik het kleurenpalet van de brouwersp+scale_color_brewer(palette="Dark2")# Gebruik grijsschaalp + scale_color_grey()

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (20)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (21)

Lees hier meer over ggplot2-kleuren:ggplot2 kleuren

De functiegeom_kleed()kan worden gebruikt :

geom_rug(zijkanten = "bl")

kanten: een tekenreeks die bepaalt aan welke kant van de plot de tapijten verschijnen. Toegestane waarde is een tekenreeks die "trbl" bevat voor boven, rechts, onder en links.

# Voeg marginale tapijten toeggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + geom_rug()# Verander kleurenggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) + geom_point() + geom_rug()# Voeg marginale tapijten toe met getrouwe dataggplot(faithful, aes(x=uitbarstingen, y=wachten)) + geom_punt() + geom_rug()

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (22)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (23)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (24)

De functiesgeom_density_2d()ofstat_density_2d()kan worden gebruikt :

# Spreidingsplot met de schattingen van de 2D-dichtheidsp <- ggplot(trouw, aes(x=uitbarstingen, y=wachten)) + geom_point()sp + geom_density_2d()# Verloopkleurenp + stat_density_2d(aes(fill = ..level..), geom="polygon")# Wijzig de verloopkleurenp + stat_density_2d(aes(fill = ..level ..), geom="polygoon")+ scale_fill_gradient(low="blue", high="red")

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (25)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (26)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (27)

Lees hier meer over ggplot2-kleuren:ggplot2 kleuren

De functiestat_ellipse()kan als volgt worden gebruikt:

# Eén ellips rond alle puntenggplot(faithful, aes(wachten, uitbarstingen))+ geom_point()+ stat_ellipse()# Ellips by groupp <- ggplot(faithful, aes(waiting, uitbarstingen, kleur = uitbarstingen > 3))+ geom_point()p + stat_ellipse()# Wijzig het type ellipsen: mogelijke waarden zijn "t", "norm ", "euclid"p + stat_ellipse(type = "norm")

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (28)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (29)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (30)

Het aantal waarnemingen wordt in elke bak geteld en weergegeven met behulp van een van de onderstaande functies:

  • geom_bin2d()voor het toevoegen van een heatmap van 2D bin counts
  • start_bin_2d()voor het tellen van het aantal waarnemingen in rechthoekige bakken
  • stat_summary_2d()functie toepassen voor 2D rechthoekige bakken

De vereenvoudigde formaten van deze functies zijn:

plot + geom_bin2d(...)plot+stat_bin_2d(geom=NULL, bins=30)plot + stat_summary_2d(geom = NULL, bins = 30, fun = mean)
  • geom: geometrisch object om de gegevens weer te geven
  • bakken: Aantal bakken in zowel verticale als horizontale richting. De standaardwaarde is 30
  • plezier: functie voor samenvatting

De datasetsdiamantenvan ggplot2 pakket wordt gebruikt:

hoofd (diamanten)
## karaat geslepen kleur helderheid diepte tabel prijs x y z## 1 0,23 Ideaal E SI2 61,5 55 326 3,95 3,98 2,43## 2 0,21 Premium E SI1 59,8 61 326 3,89 3,84 2,31## 3 0,23 Goed E VS1 56,9 65 327 4. 05 4,07 2,31## 4 0,29 Premium I VS2 62,4 58 334 4,20 4,23 2,63## 5 0,31 Goed J SI2 63,3 58 335 4,34 4,35 2,75## 6 0,24 Zeer goed J VVS2 62,8 57 336 3,9 4 3,96 2,48
# Plotp <- ggplot(diamanten, aes(karaat, prijs))p + geom_bin2d()

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (31)

Wijzig het aantal bakken:

# Verander het aantal binsp + geom_bin2d(bins=10)

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (32)

Of specificeer de breedte van bakken:

# Of specificeer de breedte van binsp + geom_bin2d(binwidth=c(1, 1000))

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (33)

Stap 1/3. Maak enkele gegevens aan:

set.seed(1234)x <- c(rnorm(500, gemiddelde = -1), rnorm(500, gemiddelde = 1,5))y <- c(rnorm(500, gemiddelde = 1), rnorm(500, gemiddelde = 1,7))groep <- as.factor(rep(c(1,2), elk=500))df <- data.frame(x, y, groep)head(df)
## x y groep## 1 -2.20706575 -0.2053334 1## 2 -0.72257076 1.3014667 1## 3 0.08444118 -0.5391452 1## 4 -3.34569770 1.6353707 1## 5 -0.5 7087531 1,7029518 1## 6 -0,49394411 -0,9058829 1

Stap 2/3. Maak de percelen:

# scatterplot van x- en y-variabelen# color by groupsscatterPlot <- ggplot(df,aes(x, y, color=group)) + geom_point() + scale_color_manual(values ​​= c('#999999','#E69F00')) + theme(legend.position=c(0,1), legend.justification=c(0,1))scatterPlot# Marginale dichtheidsplot van x (bovenste paneel)xdensity <- ggplot(df, aes(x, fill=group)) + geom_density(alpha=.5) + scale_fill_manual(values ​​= c('#999999','#E69F00')) + theme(legend.position = "none")xdensity# Marginale dichtheidsplot van y (rechterpaneel)ydensiteit <- ggplot(df, aes(y , fill=group)) + geom_density(alpha=.5) + scale_fill_manual(values ​​= c('#999999','#E69F00')) + theme(legend.position = "none")ydensity

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (34)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (35)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (36)

Maak een lege plaatshouderplot:

blankPlot <- ggplot()+geom_blank(aes(1,1))+ theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), as.titel.x = element_blank(), as.titel.y = element_blank(), as.tekst.x = element _blank(), as.tekst.y = element_blank(), as.tekens = element_blank() )

Stap 3/3. Zet de percelen bij elkaar:

Om meerdere percelen op dezelfde pagina te plaatsen, is het pakketgridExtrakan worden gebruikt. Installeer het pakket als volgt:

install.packages("gridExtra")

Schik ggplot2 met aangepaste hoogte en breedte voor elke rij en kolom:

library("gridExtra")grid.arrange(xdensity, blankPlot, scatterPlot, ydensity, ncol=2, nrow=2, widths=c(4, 1.4), heights=c(1.4, 4))

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (37)

Lees meer over het rangschikken van meerdere ggplots op één pagina:ggplot2 - Gemakkelijke manier om meerdere grafieken op dezelfde pagina te mixen

# Basic scatter plotggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()+ geom_smooth(method=lm, color="black")+ labs(title="Miles per gallon \n volgens het gewicht", x="Weight (lb/1000)", y = "Miles/(US) gallon")+ theme_classic() # Wijzig kleur/vorm per groep# Verwijder betrouwbaarheidsbandensp <- ggplot( mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point()+ geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+ labs(title="Miles per gallon \n volgens het gewicht", x="Weight (lb/1000)", y = "Miles/(US) gallon")p + theme_classic()

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (38)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (39)

Wijzig kleuren handmatig:

# Continue kleurenp + scale_color_brewer(palette="Paired") + theme_classic()# Discrete kleurenp + scale_color_brewer(palette="Dark2") + theme_minimal()# Verloopkleurenp + scale_color_brewer(palette="Accent") + theme_minimal()

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (40)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (41)ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (42)

Lees hier meer over ggplot2-kleuren:ggplot2 kleuren

Deze analyse is uitgevoerd met behulp vanR-software(versie 3.2.4) enggplot2(versie 2.1.0)

ggplot2 scatterplots : Snelstartgids - R-software en datavisualisatie - Easy Guides - Wiki (2024)

FAQs

How to plot data in R using ggplot2? ›

7 Plotting with ggplot2
  1. Start by preparing a dataset so that it is in the right format.
  2. Create a plot object using the function ggplot() .
  3. Define so-called “aesthetic mappings”, i.e. we determine which variables should be displayed on the X and Y axes and which variables are used to group the data.

Which method is used to create a scatter plot using ggplot2? ›

To plot scatterplot we will use we will be using geom_point() function. Following is brief information about ggplot function, geom_point(). Parameter : size : Size of Points.

How do I make a scatterplot from a Dataframe in R? ›

To create scatterplot using data frame columns, we need to convert the data frame columns into a variable and the value for each column will be read in a new column against each column name. This can be done with the help of melt function in reshape2 package.

How to make a scatter plot using ggplot2 in R? ›

can be summarized as follows:
  1. Load the package ggplot2 using. library(ggplot2) .
  2. Specify the dataset to be plotted using. ggplot() .
  3. Use the. + ...
  4. Add a geometric layer to define the shapes to be plotted. In case of scatter plots, use. ...
  5. Map variables from the dataset to plotting properties through the. mapping.
Jul 22, 2020

Why use R for data visualization? ›

R has the following advantages over other tools for data visualization: R offers a broad collection of visualization libraries along with extensive online guidance on their usage. R also offers data visualization in the form of 3D models and multipanel charts.

Why use ggplot2? ›

Plotting with ggplot2. ggplot2 is a plotting package that provides helpful commands to create complex plots from data in a data frame. It provides a more programmatic interface for specifying what variables to plot, how they are displayed, and general visual properties.

What is scatter plot in GGPlot2? ›

Data Visualization using GGPlot2. A Scatter plot (also known as X-Y plot or Point graph) is used to display the relationship between two continuous variables x and y. By displaying a variable in each axis, it is possible to determine if an association or a correlation exists between the two variables.

Is scatter plot a data visualization method? ›

A scatter plot is a data visualization that displays the values of two different variables as points. The data for each point is represented by its horizontal (x) and vertical (y) position on the visualization.

How are scatter plots used to analyze data? ›

Use a scatter plot to determine whether or not two variables have a relationship or correlation. Are you trying to see if your two variables might mean something when put together? Plotting a scattergram with your data points can help you to determine whether there's a potential relationship between them.

Which two methods can be used to create scatter plots in R? ›

Each point on the scatterplot defines the values of the two variables. One variable is selected for the vertical axis and other for the horizontal axis. In R, there are two ways of creating scatterplot, i.e., using plot() function and using the ggplot2 package's functions.

How do you plot a scatter plot for a data set? ›

To create a simple scatter plot, we use the sns. scatterplot command and specify the values for: the horizontal x-axis ( x=insurance_data['bmi'] ), and. the vertical y-axis ( y=insurance_data['charges'] ).

How do I construct a scatter plot? ›

Scatter Diagram Procedure

Collect pairs of data where a relationship is suspected. Draw a graph with the independent variable on the horizontal axis and the dependent variable on the vertical axis. For each pair of data, put a dot or a symbol where the x-axis value intersects the y-axis value.

How do I create a plot in R with a dataset? ›

Plot data in R-Studio
  1. Load the data into R and type the following: data<-read.csv("http://joeystanley.com/downloads/menu.csv")
  2. Type the following into the script: plot(data$Calories,data$Fat). ...
  3. Then, press Run.
  4. A plot will come up on the bottom right-hand corner on your screen.
Jun 27, 2022

How to plot a set of data in R? ›

Plot data in R-Studio
  1. Load the data into R and type the following: data<-read.csv("http://joeystanley.com/downloads/menu.csv")
  2. Type the following into the script: plot(data$Calories,data$Fat). ...
  3. Then, press Run.
  4. A plot will come up on the bottom right-hand corner on your screen.
Jun 27, 2022

How to plot a line chart in R using ggplot? ›

Creating a simple line graph
  1. Specify the dataset within. ggplot() ggplot()
  2. Define the. geom_line() plot layer.
  3. Map the. year. to the x-axis and the life expectancy. lifeExp. to the y-axis with the. aes() function.
Sep 5, 2020

How to plot a subset of data in ggplot2? ›

To construct a subset of the data depending on a condition, use the subset() function. Age%in% “18” in this instance filters the data to only include rows with the value “18” for the Age variable. Using the + operator, the geom_point() function is added to the plot.

Top Articles
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5455

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.