library(ggplot2)
or library(tidyverse)
geom_
s): How should the data be
drawn on the graph? Lines? Points? Bars?We can focus, at least to start, on:
data.frame
or
tibble
) as you’d normally work with it in R.x=
and y=
axes, but also
other axes on which data is differentiated, like color=
,
size=
, linetype=
, fill=
,
etc.aes(x=mpg,y=hp,color=transmission)
puts the
mpg
variable on the x-axis, hp
on the y-axis,
and colors things differently by transmission
group=
to separate groups for things like labeling
without making them visibly differentSee help()
for a given geometry to see what it accepts.
But common are:
x
, y
: position on the x and y axiscolor
, fill
: color is for coloring things
generally. For shapes, color
is the outline and
fill
is the insidelabel
: text labelssize
: guess what this doesalpha
: Transparencylinetype
, linewidth
: for lines or
outlines, the type/thickness of line (solid, dashed…)xmin
/xmax
/xend
(and same for
y
): For line segments or shaded ranges, where do they
start/end?geom_point()
drew points.geom_point
, geom_line
,
geom_col
, geom_text
What was that position = 'dodge'
?
x
aesthetic that you want to be side-by-sidegeom_col()
or geom_bar()
you can set
position = 'dodge'
to make the different-fill bars go side
by sideposition = position_dodge(.9)
with other
geometries to make them line up. For example…geom_density
/geom_histogram
/geom_dotplot
,
geom_bar
, geom_smooth
data(iris)
data(economics_long)
economics_long <- economics_long %>%
filter(variable %in% c('psavert','uempmed'))
keeps just the
two variables we wantdata(economics)