首页 > 解决方案 > 带有两个变量的点图

问题描述

df <- data.frame(a=c(227, 222, 218, 216, 218, 217, 225, 229, 228, 221)
                 ,b=c(219, 214, 218, 203, 215, 211, 209, 204, 201, 205))
library(ggplot2)
ggplot(df,aes(a, b))+geom_dotplot()

我想用不同的标记来表示 a 和 b 组。

标签: rvariablespolymer

解决方案


我们需要重塑为“长”格式

library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
     pivot_longer(cols = everything()) %>%
     ggplot(aes(x =  value, color = name, fill = name)) +
      geom_dotplot()

推荐阅读