小编Log*_*ter的帖子

ggplot2:如何从geom_density图例中删除斜杠

我试图在ggplot2中绘制一些重叠的密度图.我遇到了一个问题,我无法从图例中删除斜线.我尝试过使用scale_fill_manual()和legend.key以及来自R Cookbook的黑客攻击,但我似乎无法正确使用它.

data(iris)
iris=iris
cols=brewer.pal(3,"Set1")

ggplot(iris) + 
    geom_density(position="identity",aes(x=iris$Sepal.Length,fill=cols[1]),
        colour="black",alpha=.5) +
    geom_density(position="identity",aes(x=iris$Sepal.Width,fill=cols[2]),
        colour="black",alpha=.5)+  
    theme_bw() +
    scale_fill_identity(guide="legend",labels=c("Sepal Width","Sepal Length"))+
    xlab("X axis") +
    theme(panel.background=element_blank(),
        legend.title=element_blank(),
        legend.key = element_rect(),
        legend.background = element_blank(),
        legend.justification=c(1,0), 
        legend.position=c(.75,.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我该怎么做才能解决这个问题?

r legend ggplot2

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

优雅的Json在Spark中展平

我在spark中有以下数据框:

val test = sqlContext.read.json(path = "/path/to/jsonfiles/*")  
test.printSchema
root
 |-- properties: struct (nullable = true)
 |    |-- prop_1: string (nullable = true)
 |    |-- prop_2: string (nullable = true)
 |    |-- prop_3: boolean (nullable = true)
 |    |-- prop_4: long (nullable = true)
...
Run Code Online (Sandbox Code Playgroud)

我想做的是压扁这个数据帧,使其prop_1 ... prop_n存在于顶层.即

test.printSchema
root
|-- prop_1: string (nullable = true)
|-- prop_2: string (nullable = true)
|-- prop_3: boolean (nullable = true)
|-- prop_4: long (nullable = true)
...
Run Code Online (Sandbox Code Playgroud)

有几种类似问题的解决方案.我能找到的最好的就是这里.但是,只有properties …

json scala apache-spark apache-spark-sql

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

熊猫:将分类列拆分为多个列

想象一下以下格式的Pandas数据框:

id  type  v1  v2
1   A     6   9
1   B     4   2
2   A     3   7
2   B     3   6
Run Code Online (Sandbox Code Playgroud)

我想将此数据帧转换为以下格式:

id  A_v1  A_v2  B_v1  B_v2
1   6     9     4     2
2   3     7     3     6
Run Code Online (Sandbox Code Playgroud)

是否有一种优雅的方法?

python indexing pandas

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

Webpack中的同步需求

我正在使用webpack,我正在使用require来提取一些包.我有两个包:package1.js和package2.js.package1.js只是创建一个带有一些属性的对象pkg1.package2是一个javascript文件,包含一个扩展package1的自执行函数.例如

package2.js:

!function () {
    pkg1.prototype.newFunction = function {return "foo"};
}()
Run Code Online (Sandbox Code Playgroud)

我试图通过以下方式将这两者都要求成为一个新脚本:

require('package1')
require('package2')
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我收到一个错误:

Uncaught TypeError: pkg1.newFunction is not a function
Run Code Online (Sandbox Code Playgroud)

我认为这是因为Javascripts异步加载:require(package2)之前执行require('package1').我的证据是,当我执行以下操作时,我没有收到错误:

require('package1')
!function () {
    pkg1.prototype.newFunction = function {return "foo"};
}()
Run Code Online (Sandbox Code Playgroud)

但是,这非常混乱,我想使用require.我该怎么做才能做到这一点?

编辑:具体示例

单张-D3插件开始:

(function() {
    L.HexbinLayer = L.Class.extend({
    ...
 })()
Run Code Online (Sandbox Code Playgroud)

因此,至少根据我的理解,放入一个require(leaflet-d3-plugin)应该导致这个脚本执行和扩展的L内容require('leaflet')

同样,d3-hexbin-v0以:

!function(){d3.hexbin=function(){
    ...
}}()
Run Code Online (Sandbox Code Playgroud)

同样,我读这个的方式是这个脚本只是添加一个.hexbin方法d3.

现在,如果我只是编写html,我会把这些不同的东西放在各种脚本标签中,这只是起作用:

<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.hexbin.v0.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

要么

<script src="http://cdn.leafletjs.com/leaflet-0.7/leaflet.js"></script>
<script src="static/leaflet-d3.min.js" charset="utf-8"></script>
Run Code Online (Sandbox Code Playgroud)

但是因为我正在使用webpack,所以我应该能够在使用npm安装它们之后需要和/或导入这些库,或者我只是将这些脚本中的.js复制到某个目录中,然后 …

javascript webpack

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

在数据帧中按因子分割列

假设我有一个这样的数据框:

v1   v2   v3
a    1    a
a    2    b
a    6    c
b    3    a
b    4    b
b    5    c
Run Code Online (Sandbox Code Playgroud)

其中v1是一个因子,v3是一个字符.我想将一些函数应用于数据框,这样v2在v1中拆分,然后包含在数据框中:

v1   v2   v3   v4   v5
a    1    a    1    NA
a    2    b    2    NA
a    6    c    6    NA
b    3    a    NA   3
b    4    b    NA   4
b    5    c    NA   5
Run Code Online (Sandbox Code Playgroud)

我能够解决的解决方案非常复杂.这样做有一种优雅的方式吗?

(注意:v3的存在是因为任何解决方案都需要能够处理数据帧中应该忽略的其他非数字向量的存在.)

split r plyr dataframe

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

如何在不分配给 val 的情况下使用隐式调用返回的函数

假设我有一个对象,如:

object MyObj {
  def apply(args: List[String])(implicit v: Int): Unit => Int = (_: Unit) => args.length + v
}
Run Code Online (Sandbox Code Playgroud)

如果我想 MyObj.apply,我必须这样做:

implicit val v = 5
val myObj = MyObj(List("a", "b", "c"))
myObj()
Run Code Online (Sandbox Code Playgroud)

但这感觉是多余的。我希望能够做的是:

implicit val v = 5
MyObj(List("a", "b", "c"))()
Run Code Online (Sandbox Code Playgroud)

不幸的是,这似乎不起作用。系统抱怨我遗漏了我隐含的论点,这是有道理的,但令人失望。

有什么方法可以apply直接调用从方法返回的函数,而无需先将其分配给值?

scala implicit

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

“让”的功能替代

我发现自己以这种方式编写了很多 clojure:

(defn my-fun [input]
  (let [result1 (some-complicated-procedure input) 
        result2 (some-other-procedure result1)]
    (do-something-with-results result1 result2)))
Run Code Online (Sandbox Code Playgroud)

let句话似乎非常……势在必行。我不喜欢的。原则上,我可以像这样编写相同的函数:

(defn my-fun [input]
    (do-something-with-results (some-complicated-procedure input) 
                               (some-other-procedure (some-complicated-procedure input)))))
Run Code Online (Sandbox Code Playgroud)

问题在于它涉及重新计算 some-complicated-procedure,这可能非常昂贵。您也可以想象这some-complicated-procedure实际上是一系列嵌套的函数调用,然后我要么必须编写一个全新的函数,要么冒着第一次调用中的更改不会应用于第二次调用的风险:

例如,这有效,但我必须有一个额外的浅层顶级函数,这使得很难进行心理堆栈跟踪:

(defn some-complicated-procedure [input] (lots (of (nested (operations input))))) 
(defn my-fun [input]
    (do-something-with-results (some-complicated-procedure input) 
                               (some-other-procedure (some-complicated-procedure input)))))

Run Code Online (Sandbox Code Playgroud)

例如,这很危险,因为重构很难:

(defn my-fun [input]
    (do-something-with-results (lots (of (nested (operations (mistake input))))) ; oops made a change here that wasn't applied to the other nested calls
                               (some-other-procedure (lots (of (nested (operations …
Run Code Online (Sandbox Code Playgroud)

clojure

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

使用宏和doseq来生成spec

我发现自己写了很多这样的规范:

(s/def ::name string?)
(s/def ::logUri string?)
(s/def ::subnet (s/and string? #(> (count %) 5)))
(s/def ::instanceType string?)
...
(s/def ::key (s/and string? #(> (count %) 5)))
(s/def ::instanceCount string?)
(s/def ::bidPct string?)
Run Code Online (Sandbox Code Playgroud)

即很多s/ands/def。这似乎是一种浪费。所以我决定编写一个宏来为我完成这个任务。就像是:

(defmacro and-spec [validations]
  (doseq [[keyname & funcs] validations]
    `(s/def ~keyname (s/and ~@funcs))))
Run Code Online (Sandbox Code Playgroud)

所以我可以做类似的事情:

(and-spec [[::name1 [string?]]
           [::name2  [string? #(> (count %) 5)]]])
Run Code Online (Sandbox Code Playgroud)

这将为s/def我做所有的事情。不幸的是,上面的宏不起作用,但我不知道为什么。

(s/valid? ::name1 "asdf")
Execution error at emr-cli.utils/eval6038 (form-init17784784591561795514.clj:1).
Unable to resolve spec: :emr-cli.utils/name1
Run Code Online (Sandbox Code Playgroud)

这项工作的较小版本:

(defmacro small-and-spec-works [key-name …
Run Code Online (Sandbox Code Playgroud)

macros clojure clojure.spec

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

spark dataframe udf将索引映射到值

我有一个火花数据框,其中一列由列表的索引组成.我想写一个udf,它允许我创建一个新的列,其中包含与索引相关的值.

例如

假设我有以下数据帧和数组:

val df = spark.createDataFrame(Seq((0, Array(1, 1, 2)), (1, Array(1, 2, 0))))
df.show()
+---+---------+
| _1|       _2|
+---+---------+
|  0|[1, 1, 2]|
|  1|[1, 2, 0]|
+---+---------+
val sArray = Array("a", "b", "c")
Run Code Online (Sandbox Code Playgroud)

我希望能够将指标映射_2到它们的值,sArray从而导致:

+---+---------+---------+
| _1|       _2|       _3|
+---+---------+---------+
|  0|[1, 1, 2]|[b, b, c]|
|  1|[1, 2, 0]|[b, c, a]|
+---+---------+---------+
Run Code Online (Sandbox Code Playgroud)

我一直试图用udf做到这一点:

def indexer (values: Array[String]) = 
  udf((indices: Array[Int]) => indices.map(values(_)))
df.withColumn("_3", indexer(sArray)($"_2"))
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我收到以下错误:

无法执行用户定义的功能

...引起:java.lang.ClassCastException:scala.collection.mutable.WrappedArray $ ofRef无法强制转换为[I

这里出了什么问题?我怎样才能解决这个问题?

indexing scala dataframe apache-spark udf

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

在模式匹配中记录警告而不返回 Any

我有一个可迭代的数组,我试图将其转换为 case 类,并且我正在映射它们以执行此操作。如果数组不可转换为 case 类,我想记录警告并继续映射。但是,当我实施警告时,返回类型更改Iterable[MyCaseClass]Iterable[Any]which 不是我想要的。例如:

case class MyCaseClass(s1: String, s2: String)
object MyCaseClass {
  def apply(sa: Array[String]) = new MyCaseClass(sa(0), sa(1))
}

val arrayIterable: Iterable[Array[String]] = Iterable(Array("a", "b"), Array("a", "b", "c"))

def badReturnType(): Iterable[Any] = { // Iterable[Any] is undesireable
  arrayIterable map {
    case sa: Array[String] if sa.length == 2 => MyCaseClass(sa)
    case _ => println("something bad happened!") // but warnings are good
  }
}

def desiredReturnType(): Iterable[MyCaseClass] = { // Iterable[MyCaseClass] is desireable
  arrayIterable …
Run Code Online (Sandbox Code Playgroud)

scala

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