Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: In ggplot, how to add multiple text labels on the y-axis for each of multiple dates on the x-axis

Tags:

r

ggplot2

I am making a very wide chart that, when output as a PNG file, takes up several thousand pixels in the x-axis; there is about 20 years of daily data. (This may or may not be regarded as good practise, but it is for my own use, not for publication.) Because the chart is so wide, the y-axis disappears from view as you scroll through the chart. Accordingly I want to add labels to the plot at 2-yearly intervals to show the values on the y-axis. The resulting chart looks like the one below, except that in the interests of keeping it compact I have used only 30 days of fake data and put labels roughly every 10th day:

labelling plot with y-axis values in gggplot2

This works more or less as required, but I wonder if there is some better way of approaching it as in this chart (see code below) I have a column for each of the 3 y-axis values of 120, 140 and 160. The real data has many more levels, so I would end up with 15 calls to geom_text to put everything on the plot area.

Q. Is there a simpler way to splat all 20-odd dates, with 15 labels per date, on to the chart at once?

require(ggplot2)

set.seed(12345)
mydf <- data.frame(mydate = seq(as.Date('2012-01-01'), as.Date('2012-01-31'), by = 'day'),
                     price = runif(31, min = 100, max = 200))

mytext <- data.frame(mydate = as.Date(c('2012-01-10', '2012-01-20')),
                col1 = c(120, 120), col2 = c(140,140), col3 = c(160,160))

p <- ggplot(data = mydf) +
    geom_line(aes(x = mydf$mydate, y = mydf$price), colour = 'red', size = 0.8) +
    geom_text(data = mytext, aes(x = mydate, y = col1, label = col1), size = 4) +
    geom_text(data = mytext, aes(x = mydate, y = col2, label = col2), size = 4) +
    geom_text(data = mytext, aes(x = mydate, y = col3, label = col3), size = 4)

print(p)
like image 704
SlowLearner Avatar asked Jul 15 '12 14:07

SlowLearner


People also ask

How do I add labels to the y axis in R?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.

What does %>% do in ggplot?

%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.

How do I change the X and Y axis labels in ggplot?

Changing axis labels To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

How do you prevent X-axis labels from overlapping each other in ggplot?

To avoid overlapping labels in ggplot2, we use guide_axis() within scale_x_discrete().


1 Answers

ggplot2 likes data to be in long format, so melt()ing your text into long format lets you make a single call to geom_text():

require(reshape2)
mytext.m <- melt(mytext, id.vars = "mydate")

Then your plotting command becomes:

ggplot(data = mydf) +
  geom_line(aes(x = mydf$mydate, y = mydf$price), colour = 'red', size = 0.8) +
  geom_text(data = mytext.m, aes(x = mydate, y = value, label = value), size = 4)
like image 102
Chase Avatar answered Sep 26 '22 02:09

Chase