在向绘图添加注释文本时,我注意到geom_text()产生了难看的锯齿状文本,同时annotate()产生了流畅,漂亮的文本.有谁知道为什么会发生这种情况,如果有办法解决它?我知道我可以在annotate()这里使用,但有些情况可能geom_text()会更好,我想找到一个解决办法.此外,geom_text()不能打算给出看起来不好的文字,所以要么我做错了,要么我遇到了某种微妙的副作用.
这里有一些假数据和生成图表的代码,以及显示结果的图像.
library(ggplot2)
age = structure(list(age = c(41L, 40L, 43L, 44L, 40L, 42L, 44L, 45L,
44L, 41L, 43L, 40L, 43L, 43L, 40L, 42L, 43L, 44L, 43L, 41L)),
.Names = "age", row.names = c(NA, -20L), class = "data.frame")
ggplot(age, aes(age)) +
geom_histogram() +
scale_x_continuous(breaks=seq(40,45,1)) +
stat_bin(binwidth=1, color="black", fill="blue") +
geom_text(aes(41, 5.2,
label=paste("Average = ", round(mean(age),1))), size=12) +
annotate("text", x=41, y=4.5,
label=paste("Average = ", round(mean(age$age),1)), size=12)
Run Code Online (Sandbox Code Playgroud)

ggplot2挑战的latticist需要帮助:在直方图中请求变量per-facet中断的语法是什么?
library(ggplot2)
d = data.frame(x=c(rnorm(100,10,0.1),rnorm(100,20,0.1)),par=rep(letters[1:2],each=100))
# Note: breaks have different length by par
breaks = list(a=seq(9,11,by=0.1),b=seq(19,21,by=0.2))
ggplot(d, aes(x=x) ) +
geom_histogram() + ### Here the ~breaks should be added
facet_wrap(~ par, scales="free")
Run Code Online (Sandbox Code Playgroud)
根据特殊要求,并说明为什么我不是一个伟大的ggplot粉丝,lattice版本
library(lattice)
d = data.frame(x=c(rnorm(100,10,0.1),rnorm(100,20,0.1)),par=rep(letters[1:2],each=100))
# Note: breaks have different length by par
myBreaks = list(a=seq(8,12,by=0.1),b=seq(18,22,by=0.2))
histogram(~x|par,data=d,
panel = function(x,breaks,...){
# I don't know of a generic way to get the
# grouping variable with histogram, so
# this is not …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个项目,涉及创建与Hadley的ggplot2 0.9.0页面中关于stat_density2d()的示例非常相似的图.
library(ggplot2)
dsmall <- diamonds[sample(nrow(diamonds), 1000), ]
d <- ggplot(dsmall, aes(carat, price)) + xlim(1,3)
d + stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE)
last_plot() + scale_fill_gradient(limits=c(1e-5,8e-4))
Run Code Online (Sandbox Code Playgroud)

现在,我正在努力的是一种基本上为所有不在填充范围内的瓷砖关闭alpha(alpha = 0)的方法.因此,在图像中看到的每个灰色瓷砖都应该设置为0.这样可以使图像更好,特别是当覆盖在地图上时.
如果有人有任何建议,我将不胜感激.
我尝试使用aspect = 1进行自由缩放,但每个面板中的x/y范围相同.在下面的示例中,这意味着b中的x缩放应为(-0.04,0.04).
编辑:添加格子版
library(ggplot2)
d = data.frame(x=rnorm(100),group=c("A","B"))
d$y = d$x+rnorm(100,0,0.5)
d[d$group=="B","x"]=d[d$group=="B","x"]/100
d[d$group=="B","y"]=d[d$group=="B","y"]/60
qplot(x,y,data=d,asp=1) + facet_wrap(~group,scale="free")
require(lattice)
xyplot(y~x|group, data=d,aspect=1,scales=list(relation="free"),
prepanel=function(x,y){
lims = c(min(x,y), max(x,y))
list(xlim=lims,ylim=lims)
} )
Run Code Online (Sandbox Code Playgroud)

我记得在R-帮助评论在2001年说drop = TRUE的[.data.frame是R中历史上最严重的设计决策.
dplyr纠正了这一点并且没有隐含地下降.当试图旧代码转换为dplyr风格,这引起了当一些讨厌的错误d[, 1]或d[1]假设的载体.
我目前的解决方法使用unlist如下所示来获得1列向量.有更好的想法吗?
library(dplyr)
d2 = data.frame(x = 1:5, y = (1:5) ^ 2)
str(d2[,1]) # implicit drop = TRUE
# int [1:5] 1 2 3 4 5
str(d2[,1, drop = FALSE])
# data.frame': 5 obs. of 1 variable:
# $ x: int 1 2 3 4 5
# With dplyr functions
d1 = data_frame(x = 1:5, y = x ^ 2)
str(d1[,1])
# …Run Code Online (Sandbox Code Playgroud) 我在RStudio中使用roxygen2来生成包文档.如何强制@description部分中的以下内容显示为逐字固定字体?
SettingID Value
RedItem Patient_10574
GreenItem Record_433
Run Code Online (Sandbox Code Playgroud)
我的解决方法是将它放入@examples\dontrun {},但这不是我想要的.
@Joshua Ulrich在这里找到了一个(非)解决方案
我目前正在切换到pander我的大部分knitr-markdown格式,因为它提供了如此强大的pandoc支持.我不满意的一件事是默认的中心对齐.营销人员可能会喜欢它,但对于技术报告来说这是一个恐怖.
使用的最佳选择Hmisc是默认使用左对齐文本和日期,右对齐所有类型的数字.
是否有一种简单的方法可以全局设置pander?
library(pander)
pander(data.frame(
name = letters[1:3],
size = 1:3,
we.have.dates = Sys.Date() - 1:3
))
Run Code Online (Sandbox Code Playgroud) 为了与grav CMS一起使用,我想从.rmd创建md文件.下面的设置会使公式中的反斜杠加倍,但我需要保持公式不变.有什么建议使用什么pandoc选项?
---
output:
md_document
---
# This is an rmd file
$\alpha = \frac{4}{3}$
Run Code Online (Sandbox Code Playgroud)
这是20天后http://rmarkdown.rstudio.com/r_notebooks.html上的一个问题的重新发布
避免"重复"关闭请求:我知道如何读取Excel命名范围; 示例在下面的代码中给出.这是关于Excel中的"真实"表.
Excel2007及更高版本具有表的有用概念:您可以将范围转换为表,并在排序和重新排列时避免麻烦.在Excel范围中创建表时,它将获得默认名称(Tabelle1在TableName以下示例中为德语版本),但您还可以简单地命名表的范围(TableAsRangeName); 如Excel范围名称编辑器中的图标所示,这两个似乎被区别对待.
我无法从R中读取这些表(严格意义上).唯一已知的解决方法是使用CSV中间体,或将表转换为正常的命名范围,当您在单元格中使用列名时,这会产生令人讨厌的不可逆副作用引用; 这些转换为A1表示法.
以下示例显示了该问题.您的里程数可能因32/64位ODBC驱动程序和32/64位Java的不同组合而有所不同
# Read Excel Tables (not simply named ranges)
# Test Computer: 64 Bit Windows 7, R 32 bit
# My ODBC drivers are 32 bit
library(RODBC)
# Test file has three ranges
# NonTable Simple named range
# TableName Name of table
# TableAsRangeName Named Range covering the above table
sampleFile = "ExcelTables.xlsx"
if (!file.exists(sampleFile)){
download.file("http://www.menne-biomed.de/uni/ExcelTables.xlsx",sampleFile)
# Or do it manually, if this fails
}
# ODBC …Run Code Online (Sandbox Code Playgroud) 一直以来,knitr的一大特色就是智能四舍五入,让你少走很多sprintf/round/paste0弯路。
在一位客户抱怨我错误地给出小数之后,我注意到忘记可能以科学计数法打印的数字的 $$ 是非常危险的。但同一位客户抱怨说,我使用科学记数法的格式看起来很难看,因为类似乳胶的数学与主要字体不匹配。

按照@yihui 对已关闭问题的评论(https://github.com/yihui/knitr/issues/864),$$是必需的。
有人对此有智能解决方案吗?目前,我又回到过去用 sprintf 格式化所有内容。
---
output:
html_document:
theme: cosmo
---
I use the cosmo theme because it shows the typographic problem more clearly.
```{r}
options(digits=2)
mean = c(2.31310934, 1.23456e-7)
std = c(0.31310934, 6.54321e-7)
```
digits is `r getOption("digits")`
The mean is `r mean[1]` with a standard deviation of `r std[1]`.
This looks good
The mean is `r mean[2]` with a standard deviation of `r std[2]`.
Note that the aboves looks like …Run Code Online (Sandbox Code Playgroud)