aes()
, it becomes an axis and must be mapped to a variable
nameaes()
(and in the geometry),
it’s a setting applied to the entire geometrycoord_flip
it for bar graphs)axis.text
and the ticks
we mark on the scale
element_text
provides text objectsggplot(mtcars2, aes(x = Transmission,y=count)) +
geom_lollipop(color = 'red', size = 2) + coord_flip() +
labs(x = '', y = '') +
scale_y_continuous(breaks = c(10,20), limits = c(0,20)) +
theme(axis.text.x = element_text(size = 12,family='serif'),
axis.text.y = element_text(size = 16,family='serif', angle = 10))
limits
theme()
help(theme)
element_
functions like element_text()
, element_line()
,
element_rect()
which take aesthetic settings like
size
, color
, etc.axis.line
and
element_line
axis.line
or
even line
) or specifically (axis.ticks.x
)panel
to change what goes behind that
geometry! element_rect
might come up!element_blank()
element_text
(and elsewhere, like annotation or
geom_text
), hjust
and vjust
align
text horizontally and vertically. Set 0/1 for L/R or Top/Bottomlegend.position
is in portion of the frame. .8 = 80% to
the right.ggplot(mtcars, aes(x = mpg, y = hp, color = Transmission)) + geom_point() +
theme(legend.text = element_text(hjust = .5, family = 'serif'),
legend.title = element_text(hjust = 1, family = 'serif', face = 'bold'),
legend.background = element_rect(color = 'black', fill = 'white'),
legend.position = c(.8,.7))
theme()
settings as an object to use
repeatedly as a house styletheme()
customization afterwards.theme_
settings you can tack on. I only use a
few…library(gghighlight); data(gapminder, package = 'gapminder')
ggplot(gapminder, aes(x = year, y = lifeExp, color=country)) + geom_line(size = 1.5) +
labs(x = NULL, y = "Life Expectancy", title = "North America Only") +
scale_x_continuous(limits=c(1950,2015),
breaks = c(1950,1970,1990,2010))+
gghighlight(country %in% c('United States','Canada','Mexico'),
unhighlighted_params = aes(size=.1),
label_params=list(direction='y',nudge_x=10)) +
theme_minimal(base_family='serif')
scale_X_manual()
gapminder %>% mutate(color_name = ifelse(country %in% c('United States','Canada','Mexico'), as.character(country), 'Other')) %>%
ggplot(aes(x = year, y = lifeExp, group = country, color=color_name, size = color_name, alpha = color_name)) + geom_line() +
geom_text(aes(label = ifelse(year == 2007 & color_name != 'Other', color_name,'')),
hjust = 0, size = 13/.pt) +
labs(x = NULL, y = "Life Expectancy", title = "North America Only") +
scale_x_continuous(limits=c(1950,2025),
breaks = c(1950,1970,1990,2010))+
scale_color_manual(values = c('red','forestgreen','gray','blue')) +
scale_size_manual(values = c(1.5,1.5,.1,1.5)) +
scale_alpha_manual(values = c(1,1,.2,1)) +
theme_minimal(base_family='serif') +
guides(color = 'none', size = 'none', alpha = 'none')
geom_text
(see
previous slide)p <- gapminder %>%
group_by(country) %>%
mutate(country_label = ifelse(year == max(year), as.character(country), NA_character_)) %>%
filter(country %in% c('United States','Canada','Mexico')) %>%
ggplot(aes(x = year, y = lifeExp, color=country)) + geom_line(size = 1.5) +
scale_x_continuous(limits = c(1950,2025))+ theme_classic()+guides(color='none')+
geom_text(aes(x = year + 2,label = country_label), hjust = 0)
p
annotate()
function, but this takes some workgeom_mark
functions from ggforce
are good for shape highlights (here)remotes::install_github
("mattcowgill/ggannotate")
, not
install.packages()
ggsave()
will save our result to file (or we can Export
Image in the Plots frame of RStudio)+
to stick together, |
to put “beside”,
-
to move to next column, /
for the next
rowggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point()