-1

I want to plot points as horizontal lines in ggplot with labels at the end of each line. Points for reference are:

Year          a           b          c           d           e          f      g      h
2014 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333
2015 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333
2016 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333
2017 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333
2018 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333

I need to plot a,b,c,d,e,f,g and h on y axis and YEAR on x axis as lines having separate colors and labels as a,b,c, d and so on. Please help.

Looper
  • 295
  • 2
  • 3
  • 10

2 Answers2

1

you need geom_hline and annotate

  ggplot(mtcars, aes(cyl, wt)) + 
    geom_point(alpha = 0.4) +
    geom_hline(yintercept = a) + 
    geom_hline(yintercept = b) + 
    geom_hline(yintercept = c) + 
    annotate(geom="text", label="a", x=max(mtcars$cyl)+1, y=a, vjust=-1) + 
    annotate(geom="text", label="b", x=max(mtcars$cyl)+1, y=b, vjust=-1) + 
    annotate(geom="text", label="c", x=max(mtcars$cyl)+1, y=c, vjust=-1) 
tom
  • 725
  • 4
  • 17
1

Replaced previous answer

I think this is what you want:

df <- read.csv(text = "Year a b c d e f g h
 2014 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333
 2015 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333
 2016 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333
 2017 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333
 2018 0.02932623 0.006530686 0.05212177 0.007424746 0.004063887 0.01078561 0.0101 0.0333", sep=' ',header=T)


 library(ggplot2)
 library(reshape2)

 df_melt <- melt(data=df, id.vars='Year')

 ggplot(data=df_melt, aes(x=Year,y=value, group=variable)) +
  geom_line(aes(color=variable))

I use melt() from reshape to convert your data to long format. ggplot like that format more.
Then I create a ggplot with Year on the x-axis and value on the y-axis. geom_line() then plots lines through the points. The grouping variable is needed for geom_line() to understand that each of the variables a,b,c... are separate line.
Color is added dependent on the variable variable using aes(color=variable) in geom_line().

Note that making this type of grid pattern is easier with geom_hline(), as @tom suggested, but this way it's a bit more flexible and can make non-straight lines.

Community
  • 1
  • 1
P1storius
  • 917
  • 5
  • 12
  • I understand this but here you are using mtcars dataset with defined x and y axis. I don't have a x axis variable. I just need a horizontal line on x-y plot with y intercept equal to the value of the point. – Looper Aug 08 '19 at 07:16
  • Is there no data at all in your x-y plot? or do you have a working example I can use to add in the horizontal lines? – P1storius Aug 08 '19 at 07:23