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 transmissiongroup= to separate groups for things like labeling
without making them visibly differentlibrary(tidyverse)
mtcars <- mtcars %>%
mutate(Transmission = factor(am, labels = c('Automatic','Manual')),
CarName = row.names(mtcars))
ggplot(mtcars, aes(x = mpg, y = hp, color = Transmission)) +
geom_point()See 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_textdata <- tibble(category = c('Apple','Banana','Carrot'),
quality = c(6,4,3))
ggplot(data, aes(x = category,y=quality)) + geom_col()data <- tibble(category = c('Apple','Banana','Carrot','Apple','Banana','Carrot'),
person = c('Me','Me','Me','You','You','You'),
quality = c(6,4,3,1,6,3))
ggplot(data, aes(x = person, y = quality, fill = category)) + geom_col(position = 'dodge')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# Or make it straight, and separate by Transmission, and no bands
ggplot(mtcars, aes(x = mpg, y = hp, color = Transmission)) +
geom_point() + geom_smooth(method = 'lm', se = FALSE)data(iris)data(economics_long)economics_long <- economics_long %>%
filter(variable %in% c('psavert','uempmed')) keeps just the
two variables we wantdata(economics)