[英]connect points in ggplot
我附上了一个示例 plot 代码。 x 轴是离散的(chr 类型),y 轴是数字。
days <- c("Sun", "Mon", "Tue", "Sun", "Mon", "Tue")
count <- c(1, 2, 3, 2.5, 3, 4.5)
variant_type <- c("A", "A", "A", "B", "B", "B")
tbl <- tibble(days, count, variant_type)
ggplot(data = tbl,
aes(x = days,
y = count,
color = variant_type)) +
geom_line() +
geom_point() +
theme_bw() +
theme(legend.position = "right",
aspect.ratio = 1)
为了实现这一点,您需要通过将 group 映射到variant_type
ggplot()
美学中的 variant_type 变量来明确定义分组结构,它将应用于所有层。 然后你只需要 map 到geom_point
和geom_line
中的variant_type
变量的colour
美学。
library(ggplot2)
library(tibble)
days <- c("Sun", "Mon", "Tue", "Sun", "Mon", "Tue")
count <- c(1, 2, 3, 2.5, 3, 4.5)
variant_type <- c("A", "A", "A", "B", "B", "B")
tbl <- tibble(days, count, variant_type)
ggplot(data = tbl,
aes(x = days,
y = count,
group = variant_type)) +
geom_point(aes(colour = variant_type)) +
geom_line(aes(colour = variant_type)) +
theme_bw() +
theme(legend.position = "right",
aspect.ratio = 1)
由reprex package (v0.3.0) 于 2021 年 3 月 8 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.