我有以下脚本,它假设创建一个包含两个列图例的图.
#!/usr/bin/Rscript
library(ggplot2)
library(plyr)
library(reshape2)
library(scales)
file <- "http://dpaste.com/1354089/plain/";
dat <-read.table(file,header=TRUE);
datm <- melt(dat)
# Plot them
ggplot(datm,aes(x = variable,y = value,fill = Term)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent_format())+
theme(legend.direction ="vertical",legend.position = "bottom")+
guides(color=guide_legend(ncol=2))+ # this doesn't seem to work
ggsave(file="~/Desktop/test.pdf",width=11,height=11)
Run Code Online (Sandbox Code Playgroud)
但它创造了这个数字
我该怎么做才能正确?
我有以下清单
bar = ['a','b','c','x','y','z']
Run Code Online (Sandbox Code Playgroud)
我想要做的就是分配1,第4和第5的值bar
到v1,v2,v3
,有没有更紧凑的方式比这做的事:
v1, v2, v3 = [bar[0], bar[3], bar[4]]
Run Code Online (Sandbox Code Playgroud)
因为在Perl中你可以这样做:
my($v1, $v2, $v3) = @bar[0,3,4];
Run Code Online (Sandbox Code Playgroud) 鉴于此数据集:
Name Height Weight
1 Mary 65 110
2 John 70 200
3 Jane 64 115
Run Code Online (Sandbox Code Playgroud)
我想总结每个限定词列(高度和重量)
199 425
Run Code Online (Sandbox Code Playgroud)
问题是限定符可能不仅仅是2(即不仅仅是高度和重量).
我可以做这个.
# Create the dataframe people
Name <- c("Mary", "John", "Jane")
Height <- c(65,70,64)
Weight <- c(110,200,115)
people <- data.frame(Name, Height, Weight)
res <- c(sum(people$Height),sum(people$Weight))
Run Code Online (Sandbox Code Playgroud)
但是当资格赛增加时,它会变得太长.什么是紧凑的方式来做到这一点?
我有以下数据框:
In [11]: import pandas as pd
In [12]: mydict = {'foo':[0, 0.3], 'bar':[1,0.55], 'qux': [0.3,4.1]}
In [13]: df = pd.DataFrame.from_dict(mydict, orient='index')
In [14]: df
Out[14]:
0 1
qux 0.3 4.10
foo 0.0 0.30
bar 1.0 0.55
Run Code Online (Sandbox Code Playgroud)
我想要做的是用0替换所有小于1的值.产生:
0 1
qux 0 4.10
foo 0 0
bar 1.0 0
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有以下Shiny-app,按照以下方式组织
-- base_app
|-- server.R
|-- ui.R
`-- www
Run Code Online (Sandbox Code Playgroud)
例如,server.R
包含以下行:
infile <- "foo.txt"
# do something with infile
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种自动生成app目录和文件内容的方法.例如,如果我们定义2个参数:
应用名称 mycool_app
输入文件: bar.txt
之后它将根据定义的模板生成此内容base_app
.
-- mycool_app
|-- server.R
|-- ui.R
`-- www
Run Code Online (Sandbox Code Playgroud)
而新的server.R
将包含infile <- "bar.txt"
.
我看着 胡须.但它不提供内部文件编码的模板,只能通过字符串.我正在考虑Python的Jinja2的功能.
R中最好的方法是什么?
更新:
另一个例子ui.R
:
# Example of ui.R
# Choices differs from analysis to analysis
# Hand coded
# ---------------------------
choices_list <- list(
"A. Rumef.XXX vs Salt" = "Group_A.Rumef.XXX_vs_Salt.iv",
"B. …
Run Code Online (Sandbox Code Playgroud) 鉴于此数据:
abTcells 1456.74119
Macrophages 5656.38478
Monocytes 4415.69078
StemCells 1752.11026
Bcells 1869.37056
gdTCells 1511.35291
NKCells 1412.61504
DendriticCells 3326.87741
StromalCells 2008.20603
Neutrophils 12867.50224
Run Code Online (Sandbox Code Playgroud)
这个情节:
使用以下代码生成:
library(ggplot2)
df <- read.table("http://dpaste.com/1697602/plain/");
ggplot(df,aes(x=factor(1),y=V2,fill=V1))+
geom_bar(width=1,stat="identity")+coord_polar(theta="y")
Run Code Online (Sandbox Code Playgroud)
如何删除以下内容:
我有以下清单,
mylist = ['0.976850566018849',
'1.01711066941038',
'0.95545901267938',
'1.13665822176679',
'1.21770587184811',
'1.12567451365206',
'1.18041077035567',
'1.13799827821001',
'1.1624485106005',
'1.37823533969271',
'1.39598077584722',
'1.23844320976322',
'1.57397155911713',
'1.40605782943842',
'1.36037525085048',
'1.185',
'1.22795283469963',
'1.17192311574904',
'1.04121940463022',
'1.0133517787145',
'0.986161470813006',
'1.09820439504488',
'1.06640283661947',
'1.05764772395448',
'1.02678616758973',
'1.01876057166248',
'1.09019498604372',
'1.1665479238629',
'1.07170094763279',
'1.1326945725342',
'1.18199297460235',
'1.20353001964446',
'1.00973941850665',
'1.0662943967844',
'1.04876624296406',
'1.12447065457189',
'0.954629674212134',
'1.02961694279098']
Run Code Online (Sandbox Code Playgroud)
我想要做的是计算该列表中> = 1.3的值.返回5,这是:
'1.57397155911713'
'1.40605782943842'
'1.36037525085048'
'1.39598077584722'
'1.37823533969271'
Run Code Online (Sandbox Code Playgroud)
在Python中有一种紧凑的方式吗?
我有以下Pandas 数据框:
print(df)
head1 head2 head3
bar 32 3 100
bix 22 NaN NaN
foo 11 1 NaN
qux NaN 10 NaN
xoo NaN 2 20
Run Code Online (Sandbox Code Playgroud)
我想要做的是将行名称bar, bix, ...
转换为列,以便最终我有这样的事情:
newhead head1 head2 head3
0 bar 32 3 100
1 bix 22 NaN NaN
2 foo 11 1 NaN
3 qux NaN 10 NaN
4 xoo NaN 2 20
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有以下代码:
library(gplots)
library(RColorBrewer);
setwd("~/Desktop")
mydata <- mtcars
hclustfunc <- function(x) hclust(x, method="complete")
distfunc <- function(x) dist(x,method="euclidean")
d <- distfunc(mydata)
fit <- hclustfunc(d)
clusters <- cutree(fit, h=100)
nofclust.height <- length(unique(as.vector(clusters)));
# Colorings
hmcols <- rev(redgreen(2750))
selcol <- colorRampPalette(brewer.pal(12,"Set3"))
selcol2 <- colorRampPalette(brewer.pal(9,"Set1"))
clustcol.height = selcol2(nofclust.height);
heatmap.2(as.matrix(mydata),
trace='none',
dendrogram='both',
key=F,
Colv=T,
scale='row',
hclust=hclustfunc, distfun=distfunc, col=hmcols,
symbreak=T,
margins=c(7,10), keysize=0.1,
lwid=c(5,0.5,3), lhei=c(0.05,0.5),
lmat=rbind(c(5,0,4),c(3,1,2)),
labRow=rownames(mydata),
#ColSideColors=clustcol.height[clusters], # This line doesn't work
RowSideColors=clustcol.height[clusters])
Run Code Online (Sandbox Code Playgroud)
其中产生如下图:
我想要做的是在行和列上执行聚类,并在树形图旁边显示聚类条(RowSideColors和ColSideColors).我怎样才能做到这一点?
目前我只是成功RowSideColors
而不是那个ColSideColors
.
我有以下数据:
> dat
ID Gene Value1 Value2
1 NM_013468 Ankrd1 Inf Inf
2 NM_023785 Ppbp Inf Inf
3 NM_178666 Themis NaN Inf
4 NM_001161790 Mefv Inf Inf
5 NM_001161791 Mefv Inf Inf
6 NM_019453 Mefv Inf Inf
7 NM_008337 Ifng Inf Inf
8 NM_022430 Ms4a8a Inf Inf
9 PBANKA_090410 Rab6 NaN Inf
10 NM_011328 Sct Inf Inf
11 NM_198411 Inf2 1.152414 1.445595
12 NM_177363 Tarm1 NaN Inf
13 NM_001136068 Klrc1 NaN Inf
14 NM_019418 Tnfsf14 Inf Inf
15 NM_010652 Klrc1 …
Run Code Online (Sandbox Code Playgroud)