第 7 章 Color
= lubridate::ymd(c("2020-02-01", "2020-04-01",
date "2020-06-01", "2020-09-01"))
= list()
data $Britain <-
datadata.frame(
date = date,
y = c(0, 50, 80, 100)
)$Spain <-
datadata.frame(
date = date,
y = c(0, 32, 53, 103)
)$Italy <-
datadata.frame(
date = date,
y = c(0, 50, 60, 99)
)
<- purrr::map_dfr(
dataAll names(data),
~{
cbind(data[[.x]], data.frame(country=.x))
} )
- dataAll is a grouped data
7.1 Group aesthetics
When you have a grouped data frame and intend to draw by groups, you need to set the aesthetics group
equal to the group variable in your data frame.
Without group aes:
ggplot(data = dataAll) +
geom_line(
aes(
x = date,
y = y
) )
With group aes:
ggplot(data = dataAll) +
geom_line(
aes(
x = date,
y = y,
group = country
) )
Using aesthetics other than x, y can function as group aesthetic as well:
ggplot(data = dataAll) +
geom_line(
aes(
x = date,
y = y,
linetype = country
) )
ggplot(data = dataAll) +
geom_line(
aes(
x = date,
y = y,
color = country
) )