0

I am have an issue with ggplot. Spike going down and up, between 9 and 12 is not in the data.

 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") 

Plotting function:

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) 

Thanks in advance.

2
  • 1
    Maybe ggplot(df, aes(one_min_spec, sens, label = cut_point)) + geom_path() + geom_point() + ggrepel::geom_text_repel() Commented Jun 26, 2017 at 0:32
  • thanks @alistaire. this worked. Do you know what might be an issue? you can post your comment as an answer. Commented Jun 26, 2017 at 10:41

2 Answers 2

1

If you want your points to be connected by observation (row) order instead of x-value, you need to use geom_path instead of geom_line. Tidying a bit and substituting ggrepel::geom_text_repel for geom_text to avoid overlapping labels,


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 

Sign up to request clarification or add additional context in comments.

Comments

1

You can just reorder the dataset row-wise

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.