dapengde 找了几个软和的柿子捏,麻烦你先更新到书里吧。
#1 图1.13
chippy <- function(x) sin(cos(x) * exp(-x / 2))
x1 <- seq(-8, 7, by = 0.01)
df1 <- data.frame(x1, y1 <- chippy(x1))
p1 <- ggplot(df1, aes(x1, y1)) + geom_line()
x2 <- seq(-20, 20, by = 0.01)
y2 <- sin(x2)/x2
df2 <- data.frame(x2, y2)
p2 <- ggplot(df2, aes(x2, y2)) + geom_line()
ggarrange(p1, p2, ncol = 1)
#2 图1.14
tm <- rownames(VADeaths)
rownames(VADeaths) <- NULL
vd <- data.frame(cbind(tm, VADeaths))
vd <- vd %>% gather(key = "people", value = "rate", 2:5)
vd$rate <- as.numeric(vd$rate)
vd$tm <- factor(vd$tm,levels = rev(levels(vd$tm)) )
ggplot(vd, aes(people, rate, color = people)) + geom_point() +
facet_grid(tm~.) + coord_flip()
#3 图1.25
ggplot(faithful, aes(eruptions)) + geom_line(stat = "density") + geom_rug() + xlim(c(1, 6))
#4 图1.39
f <- function(mu1, mu2)
c(rnorm(300, mu1, 0.5), rnorm(200, mu2, 0.5))
x1 <- f(0, 2)
x2 <- f(2, 3.5)
x3 <- f(0.5, 2)
x <- data.frame(A = x1, B = x2, C = x3)
x <- x %>% gather(key = "var", value = "num")
ggplot(x, aes(var, num)) + geom_violin(fill = "bisque") + geom_boxplot(width = .1) +
theme(axis.title.x=element_blank(), axis.title.y=element_blank()) +
scale_y_continuous(breaks = c(-1, 0, 1, 2, 3, 4, 5)) + coord_flip()
GitHub 我得从拼音学起。