如何绘制具有多个 y 值的 x?

How can I plot x with mutiple y values?

我想绘制这些。

   subj type stc key    value
1    2    1   1  X1 94.30562
2    2    1   7  X1 94.30104
3    2    2   1  X1 95.26288
4    2    2   7  X1 94.66240
5    3    1   1  X1 89.52976
6    3    1   7  X1 88.52011

我想做:

我试过这种方式,但是看起来很乱:

ggplot(data = df0, aes(x = key, y = value))+ 
  geom_line(group = type, color = type)

可以用geom_smooth(),但是因为你的xaxis是字符,需要用group=强制平滑线:

set.seed(111)
df0 = expand.grid(subj=1:3,key=paste0("X",1:60),type=1:2)
df0$value = rnorm(nrow(df0),rep(1:2,each=120))
df0$type = factor(df0$type)

ggplot(df0,aes(x=key,y=value,fill=type,group=type)) + 
geom_smooth() + theme(axis.text.x = element_text(size=4))

或者您可以考虑将按键更改为数字:

df0$key_num =as.numeric(gsub("X","",as.character(df0$key)))

ggplot(df0,aes(x=key_num,y=value,fill=type)) + 
geom_smooth()