watchtower watchtower - 3 months ago 9
R Question

Properly aligning directlabels with ggplot2

I am absolutely new to

directlabel
+
ggplot2
package. So, I'd appreciate any help. I want to properly align my labels for the linegraph--I've spent hours trying to learn
directlabel
, and I think with SO's help I can make this work.

Here's my data:

structure(list(Rio.Olympics.Sports.Participating.Team = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("American Gymnastics",
"American Swimmers", "Boxing", "European Gymnastics", "Running"
), class = "factor"), Calendar.Quarter = structure(c(16071, 16161,
16252, 16344, 16436, 16526, 16617, 16709, 16801, 16892, 16983,
17075, 16071, 16161, 16252, 16344, 16436, 16526, 16617, 16709,
16801, 16892, 16983, 17075, 16071, 16161, 16252, 16344, 16436,
16526, 16617, 16709, 16801, 16892, 16983, 17075, 16071, 16161,
16252, 16344, 16436, 16526, 16617, 16709, 16801, 16892, 16983,
17075, 16071, 16161, 16252, 16344, 16436, 16526, 16617, 16709,
16801, 16892, 16983, 17075), class = "Date"), Randomized.Viewers = c(49,
45, 51, 55, 47, 48, 54, 57, 53, 50, 52, 58, 32, 29, 33, 40, 34,
36, 31, 39, 37, 30, 35, 41, 5, 1, 25, 46, 38, 4, 56, 27, 21,
43, 42, 44, 2, 59, 3, 10, 60, 7, 14, 24, 13, 16, 17, 28, 15,
6, 19, 23, 11, 12, 20, 22, 9, 8, 18, 26)), .Names = c("Rio.Olympics.Sports.Participating.Team",
"Calendar.Quarter", "Randomized.Viewers"), row.names = c(NA,
-60L), class = "data.frame")


Now, I wrote the following code to plot a simple linegraph:

d<-ggplot(data = p, aes(x=Calendar.Quarter,y=Randomized.Viewers)) +
geom_line(aes(color = Rio.Olympics.Sports.Participating.Team)) +
scale_color_manual(name = "Sports Type",values = c("blue", "gray51","gray51","gray51","gray51"),guide = FALSE)
direct.label(d, list('last.qp', cex=.75,dl.trans(x=x-3)))


I used the following reference guide for
maxvar.qp
http://directlabels.r-forge.r-project.org/docs/

The output of this graph is:enter image description here

As we can see all the points have moved to the left. While the top-3 lines seems to be okay, but "Running" and "Boxing" have moved a lot.

I have two questions:

a) Is there anyway I can place labels on top of linegraphs and toward the end of the linegraph, especially for "Running" and "Boxing"? I read this thread Labelling points with ggplot2 and directlabels but I wasn't quite sure what's going on---I am a beginner with R have started using R only for about 2-3 weeks. My hypothesis is that the poster is using for loops, but I have too many rows in my actual data, and I want to stay away from for loop. I am looking for vectorized operation, if possible.

b) From this thread How to use custom names for labels with ggplot2 + directlabels, it seems that
DirectLabels
package takes values from Column names of the table. Is there anyway I can modify the label names?
ggplot2
provides an API for this. However, I am not sure whether
directlabels
has such API as well. If not, I would use
colnames
to overwrite the column names before calling
directlabel
function. I'd appreciate any thoughts on this.

PS: I am asking two questions because I strongly believe they are related to the same data and problem. In the past, I have been criticized for creating multiple questions on the same dataset. So, I'm using my best judgment here.

Answer

a) Is there anyway I can place labels on top of linegraphs and toward the end of the linegraph [...]?

For example,

direct.label(d, list('last.qp', cex=.75, hjust = 1, vjust = 0))

gives you

enter image description here

b) [...] Is there anyway I can modify the label names?

For example

d + geom_dl(
  aes(label = letters[1:5][p[,1]]), 
  method = list('last.qp', cex=.75, hjust = 1, vjust = 0)
)

gives you

enter image description here

The mapping is

cbind(levels(p[,1]), letters[1:5])
#      [,1]                  [,2]
# [1,] "American Gymnastics" "a" 
# [2,] "American Swimmers"   "b" 
# [3,] "Boxing"              "c" 
# [4,] "European Gymnastics" "d" 
# [5,] "Running"             "e" 

You can adjust it as you wish.