Warwick Data Science Society
gather()
spread()
separate()
(or extract()
)unite()
NA
s'File > New File > R Script
or using Ctrl-Shift-N
Ctrl-Enter
Ctrl-Shift-S
(S for Source)write_csv()
function from readr
append = TRUE
to instead append the contentsggplot2
function called ggsave
path
argumentggsave
to understand thesedevice
, scale
, width
, height
, and dpi
ggplot2
's potentialgeom_bar()
geometrydiamonds
dataframeggplot(data = diamonds) +
geom_bar(mappping = aes(x = cut))
cut
, but on the y-axis we have a variable, count
, that didn't appear anywhere in the original datasetgeom_smooth()
) fit a model to your data and then plot the model predictionsgeom_bar()
states that the default value for the stat
parameter is “count”stat_count()
is documented on the same page as geom_bar()
count
and prop
stat_identity()
instead of stat_count()
y
aesthetic and no statistical transformation takes placesalaries <- tribble(
~name, ~salary,
"Andy", 7,
"Beth", 8,
"Charlie", 6,
"Devon", 7,
"Erica", 10
)
ggplot(data = salaries) +
geom_bar(mapping = aes(x = name, y = salary), stat = "identity")
geom_col()
geom_bar()
but has a default stat of stat_identity
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = -1))
..var_name..
ggplot2
behave, we can introduce some more geometries:
geom_area()
, geom_density()
, geom_freqpoly()
, geom_histogram()
, geom_bar()
geom_label()
, geom_text()
, geom_rug()
geom_col()
, geom_boxplot()
, geom_violin()
geom_count()
geom_bin2d()
, geom_density2d()
, geom_hex()
geom_point()
and geom_line()
had their colours controlled entirely by the col
(or color
/colour
) aesthetic bar charts have two aesthetics for colourcol
is used for the outline colour of each bar and fill
is used for the interior colourggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, colour = cut))
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = cut))
x
?ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity))
position
parameter for geom_bar
is “stack”alpha
set to a low value or with fill = NA
and an outline colourposition = "identity"
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, colour = clarity), fill = NA, position = "identity")
geom_jitter()
which is the same as geom_point()
but has a default position argument of “jitter”coord_flip()
as a new layerggplot(mpg, aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
coord_fixed(ratio = ...)
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_hex() +
coord_fixed(ratio = 1)
coord_polar()
to treat your x and y values as r and theta in polar coordinatesggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
coord_polar()
ggplot(mpg, aes(x = -1, fill = class)) +
geom_bar(position = 'fill') +
coord_polar("y") +
labs(x = "")
theme_grey()
- which ironically is one of the ugliest?theme_grey
)theme_classic()
, theme_bw()
, and theme_minimal()
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) +
geom_violin(alpha = 0.5, show.legend = FALSE) +
coord_flip() +
theme_minimal()
theme_get()
theme_grey()
theme_set(theme_minimal())
theme_set()
returns the old theme when calledold_theme <- theme_set(theme_minimal())
so that the original theme can be restored if neededtheme()
layer?theme
element_blank()
(remove the element), element_rect()
, element_line()
, element_text()
theme(axis.text.x = element_text(colour = 'red'))
as a new layerggplot2
in the several session involving itggplot(data = <DATA>) +
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>),
stat = <STAT>,
position = <POSITION>
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION> +
<THEME_FUNCTION>
gganimate
- turn static ggplot2
plots into animations using only a few lines of codeggthemes
- more themes, more geometries, more scalesggrepel
- repel overlapping text labels away from each othergeofacet
- create facets that map to real-life geography