2
votes

I know that this question has been answered a few times here and here. The key point seems to be to include the argument label in aes. But, for me ggplot does not accept label as an argument in aes. I tried using the generic function labels in aes as below, but that didn't work to create labels for points, though I am able to generate a graph:

  launch_curve <- ggplot(data = saltsnck_2002_plot_t,aes(x=weeks,y=markets, labels(c(1,2,3,4,5,6,7,8,9,10,11,12))))+
    geom_line()+geom_point()+
    scale_x_continuous(breaks = seq(0,12,by=1))+
    scale_y_continuous(limits=c(0,50), breaks = seq(0,50,by=5))+
    xlab("Weeks since launch")+ ylab("No. of markets")+
    ggtitle(paste0(marker1,marker2))+
    theme(plot.title = element_text(size = 10))
  print(launch_curve)

Does anyone know a way around this? I am using R version 3.4.3.

Edited to include sample data: The data that I use to plot is in the dataframe saltsnck_2002_plot_t. (12 rows by 94 cols). A sample is given below:

>saltsnck_2002_plot_t

       11410008398 11600028960 11819570760 11819570761 12325461033 12325461035 12325461037
Week1            3           2           2           1           2           2           1
Week2            6          16          10           1           3           2           2
Week3           11          41          13          10           3           3           2
Week4           15          46          14          14           3           4           4
Week5           15          48          15          14           3           4           4
Week6           27          48          15          15           3           4           4
Week7           31          50          15          15           3           4           5
Week8           33          50          16          16           5           5           6
Week9           34          50          18          16           5           5           6
Week10          34          50          21          19           5           5           6
Week11          34          50          23          21           5           5           6
Week12          34          50          24          23           5           5           6

I am actually plotting graphs in a loop by moving through the columns of the dataframe. This dataframe is the result of a transpose operation, hence the weird row and column names. The graph for the first column looks like the one below. And a correction from my earlier post, I need to capture as data labels the values in the column and not c(1,2,3,4,5,6,7,8,9,10,11,12).

enter image description here

1
Are you sure labels(c(1,2,3,4,5,6,7,8,9,10,11,12)) is a correct syntax. Also dput for reproducible data will be helpful.A. Suliman
The key point is to use geom_text, as in the answers to which you linked. Please include some data from saltsnck_2002_plot_t to make your problem reproducible.neilfws

1 Answers

3
votes

Use geom_text

library(ggplot2)  
ggplot(data = df,aes(x=weeks_num,y=markets))+
 geom_line() + geom_point() + geom_text(aes(label = weeks), hjust = 0.5,  vjust = -1) +
 scale_y_continuous(limits=c(0,50), breaks = seq(0,50,by=5)) +
 scale_x_continuous(breaks = seq(1,12,by=1),labels = weeks_num)+
 xlab("Weeks since launch")+ ylab("No. of markets")+
 ggtitle(paste0(markets))+
 theme(plot.title = element_text(size = 10))

enter image description here

Data

df <- structure(list(weeks_num = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12), weeks = structure(1:12, .Label = c("week1", "week2", "week3", 
"week4", "week5", "week6", "week7", "week8", "week9", "week10", 
"week11", "week12"), class = c("ordered", "factor")), markets = c(3, 
 6, 11, 15, 27, 31, 33, 34, 34, 34, 34, 34)), .Names = c("weeks_num", 
"weeks", "markets"), row.names = c(NA, -12L), class = "data.frame")