如何将标题和图例对齐在一行ggplot2 2.2.0?
library(ggplot2)
library(dplyr)
library(tidyr)
dfr <- data.frame(x=factor(1:20),y1=runif(n=20)) %>%
mutate(y2=1-y1) %>%
gather(variable,value,-x)
ggplot(dfr,aes(x=x,y=value,fill=variable))+
geom_bar(stat="identity")+
labs(title="Badass title")+
theme(legend.position="top",
legend.justification="right")
Run Code Online (Sandbox Code Playgroud)
更改lineheight和/或vjust作为标题属性似乎没有做任何事情.
ggplot(dfr,aes(x=x,y=value,fill=variable))+
geom_bar(stat="identity")+
labs(title="Badass title")+
theme(legend.position="top",
legend.justification="right",
plot.title = element_text(lineheight=-5,vjust=0))
Run Code Online (Sandbox Code Playgroud) 如何删除绘图区域周围的所有空间?
样本数据
dfr <- data.frame(x=factor(1:5),y=c(3,5,4,2,4))
ggplot(dfr,aes(x,y))+
geom_bar(stat="identity")
Run Code Online (Sandbox Code Playgroud)
我尝试如下,但左侧和底部仍然有空间。添加了Bg边框颜色以可视化边缘。
ggplot(dfr,aes(x,y))+
geom_bar(stat="identity")+
labs(x="",y="")+
theme(axis.ticks=element_blank(),
axis.text=element_blank(),
plot.background=element_rect(colour="steelblue"),
plot.margin=grid::unit(c(0,0,0,0),"cm"))
Run Code Online (Sandbox Code Playgroud)
我想在 Shinyapps.io 上的闪亮应用程序(在绘图上)中使用自定义字体。我在./www/目录中有我的 Roboto-Regular.ttf 。这是我的 app.R 文件的上半部分:
dir.create('~/.fonts')
system("chmod +x ./www/Roboto-Regular.ttf")
system("cp ./www/Roboto-Regular.ttf ~/.fonts/")
system('fc-cache -f -v ~/.fonts/')
system('fc-match Roboto')
library(ggplot2)
library(shiny)
library(shinythemes)
library(extrafont)
font_import(pattern="Roboto",prompt=FALSE)
loadfonts()
print(fonts())
Run Code Online (Sandbox Code Playgroud)
部署应用程序后,我最终得到一个如下所示的错误:
Registering fonts with R
Scanning ttf files in /usr/share/fonts/, ~/.fonts/ ...
Extracting .afm files from .ttf files...
/home/shiny/.fonts/Roboto-Regular.ttfWarning in gzfile(dest, "w") :
cannot open compressed file '/opt/R/3.5.1/lib/R/library/extrafontdb/metrics/Roboto-Regular.afm.gz', probable reason 'Permission denied'
Error in value[[3L]](cond) : cannot open the connection
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution …Run Code Online (Sandbox Code Playgroud) 在为学生准备教程时,我需要一种隐藏可折叠面板中内容的方法,可通过单击按钮将其隐藏。我已经使用下面的代码来工作了。RMarkdown文件如下所示:
---
title: Collapsible Panel
output:
html_document:
theme: flatly
highlight: tango
---
<p>
<a class="btn btn-primary" data-toggle="collapse" href="#collapseExample1" role="button" aria-expanded="false" aria-controls="collapseExample1">
Click For Answer
</a>
</p>
<div class="collapse" id="collapseExample1">
<div class="card card-body">
```{r}
hist(1:10)
```
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
渲染时看起来像这样:
这可行!我还可以通过控制块选项来控制是否必须显示代码和/或结果。
但是,这不是最佳选择,因为所有原始html的代码都很凌乱且难看。多次复制粘贴并不理想。collapseExample1每次使用此代码块时,使用的ID 必须唯一。
有什么方法可以将此块打包成可重用的单元,例如函数或其他东西?我想这样的R功能,在那里我可以在代码传递给被评估(或不需要评估做的代码),块选项(eval,echo,results,等)和面板的状态(开/关)。
collapsible_panel(code=NULL,echo=TRUE,results="show",state="closed")
我现在有很多不清楚的问题。我可以在R块中运行R块吗?也许我需要使用子Rmd文件?我需要编写一些自定义JavaScript吗?
如何关闭 Rmd HTML 文档中的标题?
---
title: ""
output: html_document
---
rmarkdown::render("index.Rmd")
Run Code Online (Sandbox Code Playgroud)
[警告] 此文档格式需要一个非空元素。请在元数据中指定“title”或“pagetitle”。回退到'index.utf8'
寻找一种快速简单的修复方法,而无需修改底层模板。
目的是
我有 1 个工作要做,但不确定这是最好的方法。这是一个工作示例。
library(magick)
path <- "https://cdn.pixabay.com/photo/2016/08/17/21/12/people-1601516_960_720.jpg"
img <- magick::image_read(path)
img
Run Code Online (Sandbox Code Playgroud)
原始图像
ii <- magick::image_info(img)
ii_min <- min(ii$width,ii$height)
img1 <- magick::image_crop(img, geometry=paste0(ii_min,"x",ii_min,"+0+0"),repage=TRUE)
img1
Run Code Online (Sandbox Code Playgroud)
裁剪方形纵横比
我不确定如何让最后一部分(2)在 R 中工作。尽管我已经设法在 unix 中使用 image-magick 让它工作。
convert -size 500x500 xc:white -fill cropped.jpeg -draw "circle 250,250 250,1" circ.jpg
Run Code Online (Sandbox Code Playgroud)
圆形框架
我正在寻找R中2的解决方案。
我试图在多列上订购数据帧.列名称通过变量传递.
df <- data.frame(var1=c("b","a","b","a"),var2=c("l","l","k","k"),var3=c("t","w","x","t"))
var1 var2 var3
1 b l t
2 a l w
3 b k x
4 a k t
Run Code Online (Sandbox Code Playgroud)
使用变量对一列进行排序
sortvar <- "var1"
df[order(df[,sortvar]),]
var1 var2 var3
2 a l w
4 a k t
1 b l t
3 b k x
Run Code Online (Sandbox Code Playgroud)
现在,如果我想按两列排序,则上述解决方案不起作用.
sortvar <- c("var1","var2")
df[order(df[,sortvar]),] #does not work
Run Code Online (Sandbox Code Playgroud)
我可以手动订购列名.
df[with(df, order(var1,var2)),]
var1 var2 var3
4 a k t
2 a l w
3 b k x
1 b l t
Run Code Online (Sandbox Code Playgroud)
但是,如何使用变量在多列上订购数据框?我知道plyr和dplyr arrange函数,但我想在这里使用base …
如何调整(增加或减少)轴文本(数字)和绘图区域(灰色区域)之间的空间?
dfr <- data.frame(x=1:5,y=1:5)
ggplot(dfr,aes(x,y))+
geom_point()+
theme(axis.title=element_blank(),
axis.ticks=element_blank())
Run Code Online (Sandbox Code Playgroud)
在尝试加载新库时,我在R中收到此错误.
Installation failed: unable to load shared object 'C:/R/R-3.4.0/library/curl/libs/x64/curl.dll':
`maximal number of DLLs reached...
Run Code Online (Sandbox Code Playgroud)
这与它无关curl,它可以是任何库.我sessionInfo看起来如下:
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252
[2] LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] parallel stats graphics grDevices utils datasets
[7] methods base
other attached packages:
[1] M3Drop_1.2.0 numDeriv_2016.8-1 bindrcpp_0.2
[4] Seurat_2.0.1 Matrix_1.2-11 cowplot_0.8.0
[7] scater_1.4.0 …Run Code Online (Sandbox Code Playgroud) 我有一个S3桶.我在这个帐户中有几个IAM用户.我想设置一个存储桶策略,多个用户可以访问此存储桶.
为了访问单个用户,我的存储桶策略如下所示:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::8474632:user/personA"
},
"Action": [
"s3:getObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:getObjectAcl",
"s3:GetObjectVersion"
],
"Resource": "arn:aws:s3:::thisbucket/*"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我试图改变这一行:
"AWS": "arn:aws:iam::8474632:user/personA"
至
"AWS": "arn:aws:iam::8474632:user/*"
允许访问所有用户,但这不起作用.
我可以逐个列出所有/一些用户:
"Principal": {
"AWS": ["arn:aws:iam::111122223333:user/PersonA",
"arn:aws:iam::111122223333:user/PersonB"]
},
Run Code Online (Sandbox Code Playgroud)
是否有更好的方法允许桶访问一组用户或所有用户?
library(ggplot2)
library(gridExtra)
df1 <- data.frame(x=c("A1","A2","A3","A4"),something=c(10,18,24,32))
df2 <- data.frame(x=c("C1","C2","C3","C4"),somethingelse=c(10543,182334,242334,32255))
p1 <- ggplot(df1,aes(x,something))+
geom_bar(stat="identity")
p2 <- ggplot(df2,aes(x,somethingelse))+
geom_bar(stat="identity")
png("test.png",height=8,width=6,res=120,units="cm")
gridExtra::grid.arrange(p1,p2,heights=grid::unit(c(4,4),"cm"))
dev.off()
Run Code Online (Sandbox Code Playgroud)
当我像上面那样手动组合两个或多个图时,如何将 y 轴宽度固定为相同,以便所有图 (A1-C1,A2-C2,..) 上的条形对齐?有没有办法计算最大 y 标签宽度并将该宽度应用于所有图的 y 轴?不,在这种特殊情况下,facet 不是我想要的。
r ×9
ggplot2 ×4
r-markdown ×2
amazon-iam ×1
amazon-s3 ×1
bioconductor ×1
imagemagick ×1
rmagick ×1
shiny ×1
shinyapps ×1