繁体   English   中英

连接ggplot中的点

[英]connect points in ggplot

我附上了一个示例 plot 代码。 x 轴是离散的(chr 类型),y 轴是数字。

  1. 如何连接点以查看计数随天数的变化,即连接相同颜色的点
  2. 我希望 x 轴处于预定义的顺序(不是字母顺序),即 - “Sun”、“Mon”、“Tue”
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_pointgeom_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM