0

I have a dataframe containing several columns.

Here is an extract from my data frame:

    emp_dayNumber emp_dayName emp_workedDays emp_fullPrice emp_halfFare emp_monthly emp_yearly
1               1         mon           TRUE          23.7       117.20      117.66    1058.84
2               2         tue           TRUE          47.4       129.05      117.66    1058.84
3               3         wed           TRUE          71.1       140.90      117.66    1058.84

I plot the variables emp_fullPrice, emp_halfFare, emp_monthly and emp_yearly using ggplot2. In order to display labels, I searched the web and found recommendations for the library ggrepel. It seems to work, but only for the first geom_line on my plot.

I would like to post a picture but I can't add images since I have low reputation. So instead here's a shitty drawing.

|
|
|                                  / 1209
|      ___________________________/  
|     /                          ____
|    /                 _________/
|   /__________       /
|  /           \_____/_______
| /                 /        \_______ 
|/_________________/_________________ 

As you can see, I managed to get the label for the first value (emp_fullPrice, so 1209) but not for the others.

Here is the code for my plot:

p<- ggplot(emp.data, aes(emp_dayNumber, emp_fullPrice))+
  geom_line(colour=1, size=1.3)+
  geom_line(aes(y=emp_halfFare),colour=2, size=1.3)+
  geom_line(aes(y=emp_monthly),colour=3, size=1.3)+
  geom_line(aes(y=emp_yearly),colour=4, size=1.3)+

  #Label at the end of the line
  geom_text_repel(
    data = subset(emp.data, emp_dayNumber == 154),
    aes(label = emp_fullPrice),
    size = 4,
    nudge_x = 5);

print(p)

From what I understand, it works for the value displayed in ggplot(), but not in the ones I added with geom_lines().

Does anyone have a solution? Thank you kindly.

C. Crt
  • 357
  • 2
  • 17
  • @c-crt have you seen this [post](https://stackoverflow.com/questions/29357612/plot-labels-at-ends-of-lines) if yes and it does not solve the problem, then revise your question to explicitly state what is different in your problem. Moreover, see [here](https://stackoverflow.com/help/mcve) and learn how to create a minimal reproducible example. Thereafter, do edit your post because for now your question is irreproducible. – mnm May 03 '19 at 11:45

1 Answers1

-1

The first step of making this a lot easier for yourself is by changing the shape of your data.

Try the "reshape2" package, made by Hadley Wickham, the maker of ggplot.

If you would apply the "melt" function on your data.frame, you would end up with a data.frame with two columns: one for the values (the numbers in you data.frame) and one for the type off the values (the column names of your data.frame).

As an example:

emp.data <- data.frame("emp_dayNumber" = 1:100,
                       "emp_monthly" = rnorm(100),
                       "emp_yearly" = rnorm(100),
                       "emp_WorkedDays" = sample(c(TRUE,FALSE), 100, replace = TRUE))
library(reshape2)

## Select the colums you want to plot:
select.data <- emp.data[ , 1:3]

## Change the data.frame to a long format, and state that you want to keep "emp_dayNumber" variable
## as a separate column (as you use it for the x-axis)
plot.data <- melt(emp.data, id.vars = "emp_dayNumber")

Your data should now look like this:

  emp_dayNumber    variable      value
1             1 emp_monthly  0.4231487
2             2 emp_monthly -1.0966351
3             3 emp_monthly  0.2761555
4             4 emp_monthly  0.8575178
5             5 emp_monthly -0.8528019
6             6 emp_monthly  0.4341048

Now plot your data, where "emp_dayNumber" should be your x, "value" your y and "variable" your color

ggplot(toplot.data, aes(x = "emp_dayNumber", y = "value", color = "variable")) +
    geom_line()

Try to always apply this on all your plotting functions. This will in the end save you a lot of time. For more explanations on long and wide format see: http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/

Using this, you could now apply the solution stated in the post linked in the commment by "mnm", or using "ggrepel", as you now only use one y variable!

Jurr.Ian
  • 51
  • 4