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

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

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

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

bf1o4zei1#

你几乎是:

  1. # helper function to create facet strip label (see documentation for `ggplot2::as_labeller()` for more details and examples.
  2. appender <- function(string, prefix = "shrinkage:") paste0(prefix, string)
  3. library(ggplot2)
  4. ggplot(data, aes(x = C, y = D, colour = factor(B)))+
  5. geom_point()+
  6. geom_line() +
  7. facet_wrap(~ A, labeller = as_labeller(appender), ncol = 2)+
  8. labs(x = NULL,
  9. y = NULL,
  10. colour = NULL)+
  11. theme(legend.position = "top")

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

展开查看全部

相关问题