R语言 如何在ggplot2图中设置数据因子排序

rggaifut  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(168)

我现在有一组4*16的数据,我想用ggplot2画一个像图1那样的漂亮的刻面折线图,但是我的代码是这样的,通过一些信息我知道我认为我的数据排序有问题,但是我不知道怎么调整,请帮帮我.

data<-data.frame(A=c(0.05,0.10,0.05,0.10,0.05,0.10,0.05,0.10,0.05,0.10,0.05,0.10,0.05,0.10,0.05,0.10),
               B=c(1,1,3,3,1,1,3,3,1,1,3,3,1,1,3,3),
               C=c(1,1,1,1,5,5,5,5,6,6,6,6,10,10,10,10),
               D=c(1.3221625,1.3006163,1.3091457,1.2738161,1.2470091,
                  1.1765646,1.1838100,1.0545490,1.2307647,1.1513869,
                  1.1569168,1.0097403,1.1758619,1.068641,1.0578440,
                  0.8668918))
 ggplot(data,aes(x=factor(C),y=D, colour=B))+
 geom_point(shape=1)+
 facet_wrap(~A,ncol = 2, nrow = 2)+geom_line()
bf1o4zei

bf1o4zei1#

你几乎是:

# helper function to create facet strip label (see documentation for `ggplot2::as_labeller()` for more details and examples.

appender <- function(string, prefix = "shrinkage:") paste0(prefix, string)

library(ggplot2)
  ggplot(data, aes(x = C, y = D, colour = factor(B)))+
  geom_point()+
  geom_line() +
  facet_wrap(~ A, labeller = as_labeller(appender),  ncol = 2)+
    labs(x = NULL, 
         y = NULL, 
         colour = NULL)+
    theme(legend.position = "top")

创建于2022年11月27日,使用reprex v2.0.2

相关问题