Position: stack
b1 <- ggplot(data=diamonds, aes(x=cut, fill=color))
Position: dodge
ggplot(gapminderAsia, aes(x=year, y=lifeExp, col=country))+
geom_line()
p2 <- ggplot(gapminderAsia, aes(x=year, y=lifeExp, col=country))+
geom_line() + theme(legend.position="bottom")
p2
ggplotly(p2)
set.seed(2020)
x <- runif(10000, -1, 1)
y <- runif(10000, -1, 1)
fx <- x^2 + y^2
coly <- ifelse(fx <= 1, 1, 0)
coly <- as.factor(coly)
circle.points <- data.frame(x=x, y=y, coly=coly)
head(circle.points)
## x y coly
## 1 0.29380568 -0.7572887 1
## 2 -0.21154848 0.2479973 1
## 3 0.23700363 0.4911537 1
## 4 -0.04621773 0.3838934 1
## 5 -0.72780563 0.8263328 0
## 6 -0.86523123 -0.3060675 1
ggplot(circle.points, aes(x=x, y=y, col=coly)) +
geom_point() +
scale_colour_manual(values = c("#e7298a", "#1b9e77")) +
theme(legend.position = "none")
ggplot(circle.points, aes(x=x, y=y, col=coly)) + geom_point() +
scale_colour_manual(values = c("#e7298a", "#1b9e77")) +
coord_equal() +
ggtitle("Figure 2: Aspect ratio is fixed.") +
theme(legend.position = "none")
# Generate sample data
set.seed(25052020)
# normal
s1 <- rnorm(100)
s2 <- rbeta(100, 2, 1)
s3 <- rexp(100)
# mixture
s4 <- rnorm(100, rep(c(-1, 1), each = 50) * sqrt(0.9), sqrt(0.1))
four <- data.frame(
dist = factor(rep(c("s1", "s2", "s3", "s4"),
each = 100),
c("s1", "s2", "s3", "s4")),
vals = c(s1, s2, s3, s4)
)
ggplot(four, aes(x = vals)) +
geom_histogram(binwidth = .1, fill = "orange", colour = "black") +
coord_cartesian(xlim = c(-3, 6)) +
facet_wrap(~ dist)
ggplot(four, aes(x = vals)) +
geom_histogram(binwidth = 2, fill = "orange", colour = "black") +
coord_cartesian(xlim = c(-3, 6)) +
facet_wrap(~ dist)
Add a rug
ggplot(four, aes(x = vals)) +
geom_histogram(binwidth = .2, fill = "orange", colour = "black") +
geom_rug() +
coord_cartesian(xlim = c(-3,6)) +
facet_wrap(~ dist)
Medium to Large N
ggplot(four, aes(y = vals, x = dist)) +
geom_boxplot() +
scale_x_discrete(name="") +
scale_y_continuous(name="")
“Notches are used to compare groups; if the notches of two boxes do not overlap, this is strong evidence that the medians differ.” (Chambers et al., 1983, p. 62)
ggplot(four, aes(y = vals, x = dist)) +
geom_boxplot(notch = T) +
scale_x_discrete(name = "") +
scale_y_continuous(name = "") +
coord_cartesian(ylim = c(-4,4))
Stripchart
Small to Medium
ggplot(four, aes(x = dist, y = vals)) +
geom_jitter(position = position_jitter(height = 0, width = .1),
fill = "forestgreen",
colour = "forestgreen",
alpha = .5) +
stat_summary(fun.y = median,
geom = "crossbar",
width = 0.5) +
scale_x_discrete(name = "") +
scale_y_continuous(name = "") +
coord_cartesian(ylim = c(-4, 4))