R以错误的方式绘制数据

时间:2017-06-26 00:10:41

标签: r ggplot2

ggplot存在问题。 Spike下降,在9到12之间不在数据中。

    structure(list(model = structure(c(46L, 46L, 46L, 46L, 46L, 46L, 
46L, 46L, 46L), .Label = c("11111", "11112", "11121", "11122", 
"11131", "11132", "11211", "11212", "11221", "11222", "11231", 
"11232", "12111", "12112", "12121", "12122", "12131", "12132", 
"12211", "12212", "12221", "12222", "12231", "12232", "21111", 
"21112", "21121", "21122", "21131", "21132", "21211", "21212", 
"21221", "21222", "21231", "21232", "22111", "22112", "22121", 
"22122", "22131", "22132", "22211", "22212", "22221", "22222", 
"22231", "22232"), class = "factor"), sens = c(0.8, 0.8, 0.8, 
0.8, 0.8, 0.8, 0.7, 0.7, 0.7), one_min_spec = c(0.448717948717949, 
0.423076923076923, 0.397435897435897, 0.358974358974359, 0.358974358974359, 
0.346153846153846, 0.346153846153846, 0.346153846153846, 0.333333333333333
), cut_point = 6:14), .Names = c("model", "sens", "one_min_spec", 
"cut_point"), row.names = c(279L, 327L, 375L, 423L, 471L, 519L, 
567L, 615L, 663L), class = "data.frame")

绘图功能:

require("ggplot")    
ggplot(df)+
      geom_line(data= df, aes(x = one_min_spec, y = sens), colour = "blue")+
      geom_text(aes(x = one_min_spec, y = sens,label=cut_point),hjust=1, vjust=-1)

提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以按行重新排序数据集

ggplot(df[order(df$sens),]) +
  geom_line(aes(x = one_min_spec, y = sens), colour = "blue")+
  geom_text(aes(x = one_min_spec, y = sens, label = cut_point), hjust = 1, vjust = -1)

答案 1 :(得分:1)

如果您希望通过观察(行)顺序而不是x值来连接您的点,则需要使用geom_path而不是geom_line。稍微整理并用ggrepel::geom_text_repel代替geom_text以避免重叠标签,


library(ggplot2)

df <- structure(list(model = structure(c(46L, 46L, 46L, 46L, 46L, 46L, 
    46L, 46L, 46L), .Label = c("11111", "11112", "11121", "11122", 
    "11131", "11132", "11211", "11212", "11221", "11222", "11231", 
    "11232", "12111", "12112", "12121", "12122", "12131", "12132", 
    "12211", "12212", "12221", "12222", "12231", "12232", "21111", 
    "21112", "21121", "21122", "21131", "21132", "21211", "21212", 
    "21221", "21222", "21231", "21232", "22111", "22112", "22121", 
    "22122", "22131", "22132", "22211", "22212", "22221", "22222", 
    "22231", "22232"), class = "factor"), sens = c(0.8, 0.8, 0.8, 
    0.8, 0.8, 0.8, 0.7, 0.7, 0.7), one_min_spec = c(0.448717948717949, 
    0.423076923076923, 0.397435897435897, 0.358974358974359, 0.358974358974359, 
    0.346153846153846, 0.346153846153846, 0.346153846153846, 0.333333333333333
    ), cut_point = 6:14), .Names = c("model", "sens", "one_min_spec", 
    "cut_point"), row.names = c(279L, 327L, 375L, 423L, 471L, 519L, 
    567L, 615L, 663L), class = "data.frame")

ggplot(df, aes(one_min_spec, sens, label = cut_point)) + 
    geom_path() +    # make line, connecting consecutive observations
    geom_point() +    # for better visibility on straight section
    ggrepel::geom_text_repel()    # drop-in replacement for geom_text that avoids overlaps