小编rno*_*erg的帖子

将文本添加到ggplot2中的刻面图上,日期位于X轴上

我是ggplot2的新手并且它很精彩,但我有一件事情有困难.

我画了一些跨越一年的时间序列.X轴来自类的变量Date.我已将该情节分面,因此我在具有独立y轴的列中有7个时间序列.该图的重点是比较每个面与顶面的相关性.

我想做的最后一件事是在每个方面的右上角添加文本(每个方面与第一个方面之间的估计的皮尔逊相关性).

这被证明是非常困难的,因为geom_text()每个文本位需要x和y坐标.当X轴为日期且每个面的Y轴不同时,如何指定坐标?这是一些示例数据和我到目前为止的代码,所以你可以重现我到目前为止:

library(ggplot2)

date <- rep(as.Date(1:365,origin='2011-1-1'),7)
location <- factor(rep(1:7,365))
product <- rep(letters[1:7], each=365)
value <- c(sample(1:10, size=365, replace=T),sample(1:3, size=365, replace=T),
           sample(10:100, size=365, replace=T), sample(1:50, size=365, replace=T),
           sample(1:20, size=365, replace=T),sample(50:100, size=365, replace=T),
           sample(1:100, size=365, replace=T))
dat<-data.frame(date,location,product,value)

qplot(date, value, data=dat, geom="line", color=location, group=location, 
      main='Time Series Comparison', xlab='Month (2011)',ylab='Value') + 
        facet_grid(product ~ ., scale = "free_y")
Run Code Online (Sandbox Code Playgroud)

text r time-series ggplot2

11
推荐指数
1
解决办法
3458
查看次数

如何将wordcloud放入grob中?

我创建了一个简单的wordcloud:

require(wordcloud)    
words <- c('affectionate', 'ambitious', 'anxious', 'articulate', 'artistic', 'caring', 'contented', 'creative', 'cynical', 'daring', 'dependable', 'easygoing', 'energetic', 'funny', 'generous', 'genuine', 'goodlistener', 'goodtalker', 'happy', 'hardworking', 'humerous', 'impulsive', 'intelligent', 'kind', 'loyal', 'modest', 'optimistic', 'outgoing', 'outrageous', 'passionate', 'perceptive', 'physicallyfit', 'quiet', 'rational', 'respectful', 'romantic', 'shy', 'spiritual', 'spontaneous', 'sweet', 'thoughtful', 'warm')
freqs <- c(134, 53, 0, 5, 0, 247, 0, 78, 0, 0, 134, 178, 79, 344, 63, 65, 257, 0, 109, 113, 0, 0, 107, 51, 199, 24, 67, 232, 0, 109, 24, 28, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 word-cloud gridextra r-grid

2
推荐指数
1
解决办法
1874
查看次数

使用R优化最小订单数量

我是优化的新手,我需要在一个简单的场景中实现它:

有一家汽车制造商可以生产5种型号的汽车/货车.与可以生产的每种型号相关联的是需要的劳动时间和所需的数量的钢,以及通过销售这样的汽车/厢式车获得的利润.制造商目前拥有固定数量的钢材和人工,应该以优化总利润的方式使用.

这是我挂断的部分 - 每辆车也有最低订货量.在生产/销售该模型变得经济可行之前,公司必须制造一定数量的每种型号.optim()如果不是最终条件,这将很容易发送,因为`lower = ...'参数可以给出一个具有最小订单数量的向量,但是它不会将0视为一个选项.有人可以帮我解决这个问题,考虑到最低订单,但仍然允许订单为0?以下是我组织相关信息/约束的方法:

Dorian <- data.frame(Model = c('SmCar', 'MdCar', 'LgCar', 'MdVan', 'LgVan'),
                   SteelReq = c(1.5,3,5,6,8), LabReq=c(30,25,40,45,55),
                   MinProd = c(1000,1000,1000,200,200),
                   Profit = c(2000,2500,3000,5500,7000))

Materials <- data.frame(Steel=6500,Labor=65000)

NetProfit<-function(x) {
  x[1]->SmCar
  x[2]->MdCar
  x[3]->LgCar
  x[4]->MdVan
  x[5]->LgVan
  np<-sum(Dorian$Profit*c(SmCar,MdCar,LgCar,MdVan,LgVan))
  np
}
LowerVec <- Dorian$MinProd #Or 0, how would I add this option?
UpperVec <- apply(rbind(Materials$Labor/Dorian$LabReq, 
                   Materials$Steel/Dorian$SteelReq),2,min)
# Attempt at using optim()
optim(c(0,0,0,0,0),NetProfit,lower=LowerVec, upper=UpperVec)
Run Code Online (Sandbox Code Playgroud)

最后,我想用已知分布的随机变量替换参数,例如Profit和LabReq(需要人工),并将其包装到一个函数中,该函数将Steel和Labor作为输入以及随机变量的参数.我想多次模拟然后找到给出Profit和Labor Required的特定参数的平均解决方案,所以理想情况下这个优化也会很快,以便我可以执行模拟.在此先感谢您的帮助!

r mathematical-optimization

1
推荐指数
1
解决办法
666
查看次数