R - 具有facet_wrap标签的散点图



我一直试图用facet_wrap来描述我的变量"Total_Sale"和"边距"之间的关系。此散点图的每个点将是一个"产品"。当我尝试为一个"日期"使用以下代码时,标签是正确的,但是当我尝试将其扩展为更多"日期"时,标签会丢失。在下面找到我的数据。

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

这是我想复制到其他人"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")

但是,当我尝试扩展到更多的"日期"时,它会丢失。

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

我明白问题是什么。他们为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")

谢谢

相关内容

  • 没有找到相关文章

最新更新