y轴标题显得太靠近轴文本.
ggplot(mpg, aes(cty, hwy)) + geom_point()
Run Code Online (Sandbox Code Playgroud)

我试过改变许多参数的值,theme()但似乎没有任何帮助.
请考虑以下几行.
p <- ggplot(mpg, aes(x=factor(cyl), y=..count..))
p + geom_histogram()
p + stat_summary(fun.y=identity, geom='bar')
Run Code Online (Sandbox Code Playgroud)
理论上,最后两个应该产生相同的情节.在实践中,stat_summary失败并抱怨所需的美学缺失.
为什么我不能用..count..的stat_summary?我在文档中找不到有关如何使用这些变量的信息.
我编写了一个返回颜色名称向量的函数:
custom.colors <- function(n) {
palette <- c("dodgerblue1", "skyblue4", "chocolate1", "seagreen4",
"bisque3", "red4", "purple4", "mediumpurple3",
"maroon", "dodgerblue4", "skyblue2", "darkcyan",
"darkslategray3", "lightgreen", "bisque",
"palevioletred1", "black", "gray79", "lightsalmon4",
"darkgoldenrod1")
if (n > length(palette))
warning('palette has duplicated colours')
rep(palette, length.out=n)
}
Run Code Online (Sandbox Code Playgroud)
我希望ggplot默认使用上面的函数来生成调色板.也许只适用于离散尺度.scale_manual()每次使用都太过拖累了.可能吗?
字符串模块有一个Template类,它允许您使用映射对象在字符串中进行替换,例如:
>>> string.Template('var is $var').substitute({'var': 1})
'var is 1'
Run Code Online (Sandbox Code Playgroud)
例如,如果尝试替换映射中缺少的元素,则替换方法可能引发KeyError异常
>>> string.Template('var is $var and foo is $foo').substitute({'var': 1})
KeyError: 'foo'
Run Code Online (Sandbox Code Playgroud)
或者可能引发ValueError,如果模板字符串无效,例如它包含一个$后跟空格的字符:
>>> string.Template('$ var is $var').substitute({'var': 1})
ValueError: Invalid placeholder in string: line 1, col 1
Run Code Online (Sandbox Code Playgroud)
给定模板字符串和映射,我想确定模板中的所有占位符是否都将被替换.为此,我会尝试进行替换并捕获任何KeyError异常:
def check_substitution(template, mapping):
try:
string.Template(template).substitute(mapping)
except KeyError:
return False
except ValueError:
pass
return True
Run Code Online (Sandbox Code Playgroud)
但是这不起作用,因为如果模板无效并且引发了ValueError,则不会捕获后续的KeyErrors:
>>> check_substitution('var is $var and foo is $foo', {'var': 1})
False
>>> check_substitution('$ var is $var and foo is $foo', {'var': 1})
True …Run Code Online (Sandbox Code Playgroud) 考虑使用"私有"方法的类,例如:
class Foo(object):
def __init__(self):
self.__method()
def __method(self):
print('42')
Run Code Online (Sandbox Code Playgroud)
当我尝试子类化Foo和覆盖方法时__method,可以看到它Foo.__method仍然被调用,而不是MoreFoo.__method.
class MoreFoo(Foo):
def __method(self):
print('41')
>>> MoreFoo()
42
<__main__.MoreFoo object at 0x7fb726197d90>
Run Code Online (Sandbox Code Playgroud)
覆盖这种方法的方法是什么?
我需要一个接受任意数量的参数的函数,并将它们作为表达式存储在变量中而不进行评估.我设法做到了,match.call但似乎有点"kludgy".
foo <- function(...) {
expr <- match.call()
expr[[1]] <- expression
expr <- eval(expr)
# do some stuff with expr
return(expr)
}
> bla
Error: object 'bla' not found
> foo(x=bla, y=2)
expression(x = bla, y = 2)
Run Code Online (Sandbox Code Playgroud)
为了澄清,我问如何编写一个行为类似的函数expression().我不能expression()直接使用太长时间无法解释的原因.
假设我更喜欢默认geom_point使用circle(pch=1)而不是solid dots(pch=16).您可以通过传递shape参数来更改标记的形状geom_point,例如
ggplot(diamonds, aes(depth, carat, colour=cut)) + geom_point(shape=1)
ggplot(diamonds, aes(depth, carat, colour=cut)) + geom_point(shape=16)
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何更改默认行为.
这是一个比较泊松分布的λ参数的ML估计的测试.
with(data.frame(x=rpois(2000, 1.5), i=LETTERS[1:20]),
cbind(cf=tapply(x, i, mean),
iter=optim(rep(1, length(levels(i))), function(par)
-sum(x * log(par[i]) - par[i]), method='BFGS')$par))
Run Code Online (Sandbox Code Playgroud)
第一列显示从封闭形式的解决方案(供参考)获得的ML估计器,而第二列显示通过使用BFGS方法最大化对数似然函数而获得的ML估计器.结果:
cf iter
A 1.38 1.380054
B 1.61 1.609101
C 1.49 1.490903
D 1.47 1.468520
E 1.57 1.569831
F 1.63 1.630244
G 1.33 1.330469
H 1.63 1.630244
I 1.27 1.270003
J 1.64 1.641064
K 1.58 1.579308
L 1.54 1.540839
M 1.49 1.490903
N 1.50 1.501168
O 1.69 1.689926
P 1.52 1.520876
Q 1.48 1.479891
R 1.64 1.641064
S 1.46 1.459310
T 1.57 1.569831
Run Code Online (Sandbox Code Playgroud)
可以看出,使用迭代优化方法获得的估计量可能与正确值有很大差异.这是预期的还是有另一种(多维)优化技术可以产生更好的近似值?
如何在Emacs中获取当前缓冲区数量(不包括内部缓冲区)?
我有这个,但看起来很复杂
;; number of buffers excluding internal buffers
(apply '+ (mapcar
(lambda (b)
(if (or (buffer-file-name b)
(not (string-equal (substring (buffer-name b) 0 1) " ")))
1 0))
(buffer-list)))
Run Code Online (Sandbox Code Playgroud)
它的作用是计算访问文件或名称不以空格开头的缓冲区.
我只想在帧标题中添加缓冲区数量.
我正在尝试编写一个let允许我执行以下操作的函数:
let(a=2, b=3, a+b)
>>> 5
Run Code Online (Sandbox Code Playgroud)
目前我一直坚持
let <- function(..., expr) {
with(list(...), quote(expr))
}
Run Code Online (Sandbox Code Playgroud)
这根本不起作用.任何帮助赞赏.