Karl Ho
School of Economic, Political and Policy Sciences
University of Texas at Dallas
Murrell, Paul. 2019. R Graphics. CRC Press.
Murrell, Paul. 2019. R Graphics. CRC Press.
Murrell, Paul. 2019. R Graphics. CRC Press.
coord_trans(x="exp", y="exp"))
install.packages("gapminder")
library(gapminder)
gm = gapminder
head(gm)
summary(gm)
table(gm$country)
# Plot one variable
hist(gm$lifeExp)
# Plot two variables with logged version of x
plot(lifeExp ~ gdpPercap, gm, subset = year == 2007, log = "x", pch=20)
"p" for points
"l" for lines
"b" for both
"c" for the lines part alone of "b"
"o" for both ‘overplotted’
"h" for ‘histogram’ like (or ‘high-density’) vertical lines
"s" for stair steps, moves first horizontal, then vertical
"S" for other steps, contrary to "s"
"n" for no plotting.
pch = 0,square
pch = 1,circle
pch = 2,triangle point up
pch = 3,plus
pch = 4,cross
pch = 5,diamond
pch = 6,triangle point down
pch = 7,square cross
pch = 8,star
pch = 9,diamond plus
pch = 10,circle plus
pch = 11,triangles up and down
pch = 12,square plus
pch = 13,circle cross
pch = 14,square and triangle down
pch = 15, filled square
pch = 16, filled circle
pch = 17, filled triangle point-up
pch = 18, filled diamond
pch = 19, solid circle
pch = 20,bullet (smaller circle)
pch = 21, filled circle blue
pch = 22, filled square blue
pch = 23, filled diamond blue
pch = 24, filled triangle point-up blue
pch = 25, filled triangle point down blue
Additional:
*
.
o
O
note: takes longer to plot
& - ampersand
‘ - apostrophe or single quote
* - asterisk
@ - at
{} - braces or curly brackets
[] - brackets
^ - carat
<> - angle brackets or chevron
~ - tilde
| - pipe
# - pound
- - hyphen
Line types can be specified with:
An integer or name:
0 = blank,
1 = solid,
2 = dashed,
3 = dotted,
4 = dotdash,
5 = longdash,
6 = twodash
44
13
1343
73
2262
plot object p cannot be displayed without adding at least one layer at this point, there is nothing to see!
install.packages("ggplot2")
library(ggplot2)
p <- ggplot(data = gm)
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(size=2)
p + geom_point()
# Add some color grouping
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp, color=continent))
p + geom_point()
# Add a regression line, dropped the color grouping
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(pch=16) + geom_smooth(method="lm")
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp,
color = "purple"))
p + geom_point() +
geom_smooth(method = "loess") +
scale_x_log10()
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(color = "purple") +
geom_smooth(method = "loess") + scale_x_log10()
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(color = "purple") +
geom_smooth(method = "loess") + scale_x_log10() +
xlab("GDP per capita") +
ylab("Life Expectancy")
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(color = "purple") +
geom_smooth(method = "loess") + scale_x_log10() +
xlab("GDP per capita") +
ylab("Life Expectancy") +
facet_wrap(~ continent, nrow=2)
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(color = "purple") +
geom_smooth(method = "loess") + scale_x_log10() +
xlab("GDP per capita") +
ylab("Life Expectancy") +
facet_wrap(~ continent, nrow=2) +
theme(text=element_text(size=14, family="Palatino"))
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(color = "purple") +
geom_smooth(method = "loess") + scale_x_log10() +
xlab("GDP per capita") +
ylab("Life Expectancy") +
facet_wrap(~ continent, nrow=2) +
theme(text=element_text(size=14, family="Palatino")) +
theme_bw()
What happen to the font?
p <- ggplot(data = gm,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(color = "purple") +
geom_smooth(method = "loess") + scale_x_log10() +
xlab("GDP per capita") +
ylab("Life Expectancy") +
facet_wrap(~ continent, nrow=2) +
theme_bw() +
theme(text=element_text(size=14, family="Palatino"))
What is different?
Source: Baptiste Auguie. 2017. Laying out multiple plots on a page (https://cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html)