0% found this document useful (0 votes)
16 views

Ggplot

ggplot2 is an R package that provides a programmatic way to create complex plots from data frames. It works by layering graphical elements (geoms) on top of an initial plot object defined by the data and assigned aesthetics. Common geoms include points, lines, and boxes. This layering approach allows for flexibility in customizing plots. The ggplot() function is used to initialize plots and the + operator allows adding geoms and modifying existing ggplot objects.

Uploaded by

whoareyoubro1111
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Ggplot

ggplot2 is an R package that provides a programmatic way to create complex plots from data frames. It works by layering graphical elements (geoms) on top of an initial plot object defined by the data and assigned aesthetics. Common geoms include points, lines, and boxes. This layering approach allows for flexibility in customizing plots. The ggplot() function is used to initialize plots and the + operator allows adding geoms and modifying existing ggplot objects.

Uploaded by

whoareyoubro1111
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Visualization

in R
GGPLOT

1. Install library
install.packages("tidyverse")
2. library(tidyverse)
Section 1 Title

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. Therefore, we only need
minimal changes if the underlying data change or if we
decide to change from a bar plot to a scatterplot. This helps
in creating publication quality plots with minimal amounts of
adjustments and tweaking.
Section 1 Title

ggplot2 refers to the name of the package itself. When using


the package we use the function ggplot() to generate the plots,
and so references to using the function will be referred to
as ggplot() and the package as a whole as ggplot2

Plots work best with data in the ‘long’ format, i.e., a column for
every variable, and a row for every observation. Well-structured
data will save you lots of time when making figures with ggplot2
ggplot graphics are built layer by layer by adding new elements.
Adding layers in this fashion allows for extensive flexibility and
customization of plots.
Section 1 Title

ggplot(data = <DATA>, mapping = aes(<MAPPINGS>)) +


<GEOM_FUNCTION>()
1. ggplot(data = surveys_complete)
Section 1 Title

ggplot(data = surveys_complete, mapping =


aes(x = weight, y = hindfoot_length))

✓ define an aesthetic mapping (using the aesthetic (aes)


function), by selecting the variables to be plotted and specifying
how to present them in the graph, e.g., as x/y positions or
characteristics such as size, shape, color, etc
Section 1 Title

3. ggplot(data = surveys_complete, aes(x =


weight, y = hindfoot_length)) + geom_point()
•add ‘geoms’ – graphical representations of the data in the plot
(points, lines, bars). offers many different geoms; we will use
ggplot2

some common ones today, including:


•Geom_point()-for scatter plots, dot plots, etc.
•Geom_boxplot()- for, well, boxplots!
•Geom_line()-for trend lines, time series, etc.
o add a geom to the plot use + operator. Because we have two
continuous variables
Section 1 Title

The + in the ggplot2 package is particularly useful because it


allows you to modify existing ggplot objects.
This means you can easily set up plot “templates” and
conveniently explore different types of plots, so the above plot can
also be generated with code like this :
# Assign plot to a variable
surveys_plot <- ggplot(data = surveys_complete,
mapping = aes(x = weight, y = hindfoot_length))
# Draw the plot
surveys_plot + geom_point()
Section 1 Title

• You may notice that we sometimes reference ‘ggplot2’ and


sometimes ‘ggplot’. To clarify, ‘ggplot2’ is the name of the most
recent version of the package. However, any time we call the
function itself, it’s just called ‘ggplot’.
•The previous version of the ggplot2 package, called ggplot,
which also contained the ggplot() function is now unsupported
and has been removed from CRAN in order to reduce accidental
installations and further confusion.
Program 1
library(tidyverse)
BOD
ggplot(data=BOD,
mapping=aes(x=Time,
y=demand))+
geom_point(size=5)
Program 2
library(tidyverse)
fd1 <- data.frame(
Q=c(5,7,12,13,25),
s=c(120,150,45,35,44))
ggplot(data = fd1, mapping = aes(x = Q,
y = s))+
geom_point(size=4,color="green")+
geom_line()

You might also like