我有以下data.frame调用df.我的问题与y轴上的顺序有关.我希望y轴上的名称根据变量排序depth.
如果我这样做:
ggplot(df,aes(x=factor(name),y=depth)) + geom_bar(stat='identity') + coord_flip() + labs(y='depth',x='species')
Run Code Online (Sandbox Code Playgroud)
我得到graph1下面没有订购.因此我按照这里的说明,name按照深度订购了我的因子水平:
df2=df[order(df$depth),]
df2$name=factor(df2$name,levels=df2$name)
ggplot(df2,aes(x=factor(name),y=depth)) + geom_bar(stat='identity') + coord_flip() + labs(y='depth',x='species')
Run Code Online (Sandbox Code Playgroud)
我得到了Graph2以下.但对我来说,下一步是根据Mut变量对条形图进行不同的着色.
ggplot(df2,aes(x=factor(name),y=depth)) + geom_bar(stat='identity',data=subset(df2,df2$Mut==2),fill='red') + geom_bar(stat='identity',data=subset(df2,df2$Mut==1),fill='blue') + coord_flip() + labs(y='depth',x='species')
Run Code Online (Sandbox Code Playgroud)
我得到Graph3的不再订购!
如何根据graph2中显示的顺序生成graph3
name depth Mut x
25 A_rubrocinctus_GA070 8 2 -0.033318659
9 A_omanensis_GA051 10 2 -0.020387101
4 A_latifasciatus_GA083 12 1 -0.005645811
27 A_frenatus_GA068 12 1 -0.024190876
13 A_percula_GA017 15 1 0.034591721
14 A_percula_GA039 15 2 …Run Code Online (Sandbox Code Playgroud) 我正在使用@jbaums 在这里trees找到的包,并在这篇文章中解释过.
我的数据如下:
树由树干组成
Trunk
[1] 13.60415
Run Code Online (Sandbox Code Playgroud)
和分支机构
Tree
TreeBranchLength TreeBranchID
1 10.004269 1
2 7.994269 2
3 9.028834 11
4 10.817401 12
5 8.551311 111
6 10.599798 112
7 11.073243 121
8 13.367392 122
9 9.625431 1111
10 10.793569 1112
11 9.896499 11121
12 8.687741 11122
13 7.791180 1211
14 12.506105 1212
15 6.768478 1221
16 10.441796 1222
17 10.751892 1121
18 9.458651 1122
19 10.768509 11221
20 10.150673 11222 …Run Code Online (Sandbox Code Playgroud) 我试图在下面的情节背景上有2个"阴影".这些阴影应分别代表橙色和蓝色点的密度.是否有意义?
以下是要改进的ggplot:

这是df我用来创建这个图的代码和数据(矩阵):
PC1 PC2 aa
A_akallopisos 0.043272525 0.0151023307 2
A_akindynos -0.020707141 -0.0158198405 1
A_allardi -0.020277664 -0.0221016281 2
A_barberi -0.023165596 0.0389906701 2
A_bicinctus -0.025354572 -0.0059122384 2
A_chrysogaster 0.012608835 -0.0339330213 2
A_chrysopterus -0.022402365 -0.0092476009 1
A_clarkii -0.014474658 -0.0127024469 1
A_ephippium -0.016859412 0.0320034231 2
A_frenatus -0.024190876 0.0238499714 2
A_latezonatus -0.010718845 -0.0289904165 1
A_latifasciatus -0.005645811 -0.0183202248 2
A_mccullochi -0.031664307 -0.0096059126 2
A_melanopus -0.026915545 0.0308399009 2
A_nigripes 0.023420045 0.0293801537 2
A_ocellaris 0.052042539 0.0126144250 2
A_omanensis -0.020387101 0.0010944998 2
A_pacificus 0.042406273 -0.0260308092 2 …Run Code Online (Sandbox Code Playgroud) > matrix(c(c(0, 3.75882e-06, 3.71645e-05, 2.16088e-06, 1.357e-06, 1.19274e-06, NaN, 1.14748e-06, 9.3314e-07), c(3.75882e-06, 0, 3.94165e-05, 3.58464e-06, 3.60392e-06, 3.43881e-06, NaN, 3.39315e-06, 3.17616e-06), c(3.71645e-05, 3.94165e-05, 0, 3.78173e-05, 3.70121e-05, 3.68449e-05, NaN, 3.6798e-05, 3.65591e-05), c(2.16088e-06, 3.58464e-06, 3.78173e-05, 0, 2.00581e-06, 1.84085e-06, NaN, 1.79527e-06, 1.57976e-06), c(1.357e-06, 3.60392e-06, 3.70121e-05, 2.00581e-06, 0, 1.03709e-06, NaN, 9.91615e-07, 7.77135e-07), c(1.19274e-06, 3.43881e-06, 3.68449e-05, 1.84085e-06, 1.03709e-06, 0, NaN, 8.27333e-07, 6.12979e-07), c(NaN, NaN, NaN, NaN, NaN, NaN, 0, NaN, NaN), c(1.14748e-06, 3.39315e-06, 3.6798e-05, 1.79527e-06, 9.91615e-07, 8.27333e-07, NaN, 0, 5.67856e-07), c(9.3314e-07, 3.17616e-06, 3.65591e-05, 1.57976e-06, 7.77135e-07, 6.12979e-07, NaN, …Run Code Online (Sandbox Code Playgroud) 我有一个以下类型的data.frame
set.seed(12)
d = data.frame(a=sample(5,x=1:9),
b=sample(5,x=1:9),
c=sample(5,x=1:9),
d=sample(5,x=1:9),
e=sample(5,x=1:9),
f=sample(5,x=1:9))
d
# a b c d e f
# 1 1 1 4 4 2 3
# 2 7 2 7 9 7 5
# 3 8 5 3 8 1 2
# 4 2 9 8 7 5 9
# 5 9 6 2 1 9 4
Run Code Online (Sandbox Code Playgroud)
我想取前两列,将整数转换为字符并将同一行的两个元素粘贴在一起.然后重复每个连续的一对列的过程.
这是一个可以正确完成工作的脚本:
bar = function (twocols) {sapply(1:nrow(twocols), FUN=function(x) {paste(twocols[x,], collapse="")} )}
count = 0
out = matrix(0, ncol=ncol(d)/2, nrow=nrow(d))
for (i in …Run Code Online (Sandbox Code Playgroud)