scatter plot with facet_wrap labels

487 views Asked by At

I have been trying to use facet_wrap to describe the relationship between my variables "Total_Sale" and "Margin" during the time "Date". Each point of this scatter plot will be one "Products". When I try the code below for one "Date" the labels are correct, however when I try to expand it for more "Date" the labels get lost. Find below my data.

dput(data)

structure(list(Products = c("K-OTHRINE_SC__30_ML", "K-OTHRINE_SC__30_ML", 
"TERRAMICINA_LA_50ML", "RODILON_R_PELLETS_25GR", "DECTOMAX_INJ._50ML", 
"RODILON_R_PELLETS_25GR", "OCITOPEC_INJ._50ML"), Date = structure(c(1L, 
2L, 1L, 1L, 1L, 2L, 1L), .Label = c("2014-01-01", "2014-02-01", 
"2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01", "2014-07-01", 
"2014-08-01", "2014-09-01", "2014-10-01"), class = "factor"), 
    Units_Sold = c(113710, 87061, 28551, 23843, 19761, 16469, 
    16060), Total_Sale = c(532764.61, 406119.99, 231751.87, 20082.12, 
    217633.92, 13830.56, 72002.93), Margin = c(0.205133153739965, 
    0.209605483356955, 0.0698413978494625, 0.362371134020619, 
    0.123838046272494, 0.348102139406487, 0.27636316872428), 
    Ratio = c(0.179930281169745, 0.137761940101303, 0.045177991888817, 
    0.0377282358097812, 0.0312690377820361, 0.0260599050266865, 
    0.0254127193350286)), .Names = c("Products", "Date", "Units_Sold", 
"Total_Sale", "Margin", "Ratio"), row.names = c(3L, 4L, 16L, 
19L, 27L, 35L, 37L), class = "data.frame") 

and here is the code with the correct output I would like to replicate to others "Date":

 p <- qplot(Total_Sale,Margin ,data = dataauxiliarymult[dataauxiliarymult$Date=="2014-01-01",],xmax=1.5*max(Total_Sale),label=str_sub( dataauxiliarymult[dataauxiliarymult$Date=="2014-01-01"  ,]$Products,1,11),colour=Units_Sold,size = Ratio) + 
      facet_wrap(~Date) +
      geom_point()+
      geom_text(angle =0,family = "mono",size=3,hjust=0, vjust=0, colour="grey50")

However, when I try to expand to more "Date" it get lost.

p <- qplot(Total_Sale,Margin ,data = dataauxiliarymult[dataauxiliarymult$Date=="2014-01-01" | dataauxiliarymult$Date=="2014-02-01" ,],xmax=1.5*max(Total_Sale),label=str_sub( dataauxiliarymult[dataauxiliarymult$Date=="2014-01-01"| dataauxiliarymult$Date=="2014-02-01" ,]$Products,1,11),colour=Units_Sold,size = Ratio) + 
  facet_wrap(~Date, ncol=3) +
  geom_point()+
  geom_text(angle =0,family = "mono",size=3,hjust=0, vjust=0, colour="grey50")
1

There are 1 answers

0
Nikos On

I think I understand what is the issue. They way you assign the label for the geom_text is wrong. Can you use the following and let me know if it is working?

dataauxiliarymult$Date=as.Date(dataauxiliarymult$Date)

ggplot(dataauxiliarymult,aes(Total_Sale,Margin,label=str_sub( Products,1,11),colour=Units_Sold,size = Ratio))+
geom_point()+
facet_wrap(~Date, ncol=3)+
geom_text(angle =0,family = "mono",size=3,hjust=0, vjust=0, colour="grey50")

Thanks