使用facet_wrap标签绘制散点图

时间:2014-11-25 17:08:00

标签: r ggplot2 facet-wrap geom-text

我一直在尝试使用facet_wrap来描述我的变量与#34; Total_Sale"之间的关系。和"保证金"期间"日期"。此散点图的每个点都是一个" Products"。当我在下面的代码中尝试一个" Date"标签是正确的,但是当我尝试将其扩展为更多"日期"标签迷路了。在下面找到我的数据。

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") 

以下是具有正确输出的代码我想复制给其他人"日期":

 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")

但是,当我尝试扩展到更多&#34;日期&#34;它迷路了。

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 个答案:

答案 0 :(得分:0)

我想我明白这是什么问题。他们为geom_text分配标签的方式是错误的。您能否使用以下内容并告诉我它是否有效?

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")

由于