我发现当coord_flip()使用ggplot2 添加到某些图时,图例中值的顺序不再与图中值的顺序对齐.
例如:
dTbl = data.frame(x=c(1,2,3,4,5,6,7,8),
y=c('a','a','b','b','a','a','b','b'),
z=c('q','q','q','q','r','r','r','r'))
print(ggplot(dTbl, aes(x=factor(y),y=x, fill=z)) +
geom_bar(position=position_dodge(), stat='identity') +
coord_flip() +
theme(legend.position='top', legend.direction='vertical'))
Run Code Online (Sandbox Code Playgroud)

我希望在不改变图中'q'和'r'的顺序的情况下反转图例中的'q'和'r'.
scale.x.reverse() 看起来很有希望,但似乎并没有在因素范围内起作用(就像这个条形图的情况一样).
我有一个很长的数据集我想扩大,我很好奇是否有一种方法可以使用R中的reshape2或tidyr包一步完成所有这些操作.
数据框df如下所示:
id type transactions amount
20 income 20 100
20 expense 25 95
30 income 50 300
30 expense 45 250
Run Code Online (Sandbox Code Playgroud)
我想谈谈这个问题:
id income_transactions expense_transactions income_amount expense_amount
20 20 25 100 95
30 50 45 300 250
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过reshape2来获得部分路径,例如:
dcast(df, id ~ type, value.var="transactions")
Run Code Online (Sandbox Code Playgroud)
但有没有办法一次性重塑整个df,同时解决"交易"和"金额"变量?理想情况下,新的更合适的列名称?
我有一个数据框,其中包含来自蝾螈内脏的各种节肢动物的长度和宽度.因为一些胆量有数千种特定的猎物,我只测量了每种猎物类型的一个子集.我现在想用这个猎物的平均长度和宽度替换每个未测量的个体.我想保留数据帧,只需添加推算列(length2,width2).主要原因是每行还有一些列,其中包含收集蝾螈的日期和位置的数据.我可以通过随机选择测量的个体填写NA,但为了论证,我们假设我只想用平均值替换每个NA.
例如,假设我有一个类似于以下内容的数据框:
id taxa length width
101 collembola 2.1 0.9
102 mite 0.9 0.7
103 mite 1.1 0.8
104 collembola NA NA
105 collembola 1.5 0.5
106 mite NA NA
Run Code Online (Sandbox Code Playgroud)
实际上,我有更多的列和大约25种不同的类群,总共约有30,000个猎物.似乎plyr包可能是理想的,但我无法弄清楚如何做到这一点.我不是很精通R或编程精通,但我正在努力学习.
并不是说我知道我在做什么,但如果有帮助,我会尝试创建一个小数据集.
exampleDF <- data.frame(id = seq(1:100), taxa = c(rep("collembola", 50), rep("mite", 25),
rep("ant", 25)), length = c(rnorm(40, 1, 0.5), rep("NA", 10), rnorm(20, 0.8, 0.1), rep("NA",
5), rnorm(20, 2.5, 0.5), rep("NA", 5)), width = c(rnorm(40, 0.5, 0.25), rep("NA", 10),
rnorm(20, 0.3, 0.01), rep("NA", 5), rnorm(20, 1, 0.1), rep("NA", 5)))
Run Code Online (Sandbox Code Playgroud)
以下是我尝试过的一些事情(没有用过): …
我有一个如下所示的当前DataFrame:
DATETIME MEAS_AVG TARG_MIN TARG_AVG TARG_MAX DESPORT_NOTE
1 2012/04/10 14:03:37 0.2888 0.22 0.25 0.27 GOOD_PT
2 2012/03/30 07:48:17 0.2544 0.22 0.25 0.27 GOOD_PT
3 2012/03/24 19:23:08 0.2333 0.22 0.25 0.27 GOOD_PT
4 2012/03/25 16:10:17 0.2111 0.22 0.25 0.27 GOOD_PT
5 2012/04/10 00:58:29 0.2222 0.22 0.25 0.27 GOOD_PT
6 2012/04/14 18:32:52 0.2888 0.22 0.25 0.27 GOOD_PT
7 2012/04/21 14:47:47 0.2777 0.22 0.25 0.27 GOOD_PT
Run Code Online (Sandbox Code Playgroud)
调用数据框df3,我正在寻找替换日期的特定列df3$DATETIME.
我已经在我的代码中使用了这个函数来剥离日期时间:
date <- strptime(df3$DATETIME, "%Y/%m/%d %H:%M:%S")
Run Code Online (Sandbox Code Playgroud)
我想用简单的月份名称替换所有日期时间信息.这是替换功能后的样子:
DATETIME MEAS_AVG TARG_MIN TARG_AVG TARG_MAX …Run Code Online (Sandbox Code Playgroud) 我有一个数据帧d:
> head(d,20)
groupchange Symscore3
1 4 1
2 4 2
3 4 1
4 4 2
5 5 0
6 5 0
7 5 0
8 4 0
9 2 2
10 5 0
11 5 0
12 5 1
13 5 0
14 4 1
15 5 1
16 1 0
17 4 0
18 1 1
19 5 0
20 4 0
Run Code Online (Sandbox Code Playgroud)
我正在密谋:
ggplot(d, aes(groupchange, y=..count../sum(..count..), fill=Symscore3)) +
geom_bar(position = "dodge")
Run Code Online (Sandbox Code Playgroud)
通过这种方式,每个条形表示其在整个数据上的百分比.
相反,我希望每个条形代表相对百分比; 即获得的柱的总和groupchange …
考虑以下多边形图:
ggplot(df, aes(x=year,y=afw)) +
geom_polygon() +
scale_x_continuous("", expand=c(0,0), breaks=seq(1910,2010,10)) +
theme_bw()
Run Code Online (Sandbox Code Playgroud)

但是,我想用两种不同的颜色填充它.例如,红色表示上方黑色区域0,蓝色表示下方黑色区域0.不幸的是,使用fill=col不填充正确的区域.
我尝试了以下代码(我添加了geom_line以便说明填充边框应该在哪里):
ggplot(df, aes(x=year,y=afw)) +
geom_line() +
geom_polygon(aes(fill=col), alpha=0.5) +
scale_x_continuous("", expand=c(0,0), breaks=seq(1910,2010,10)) +
theme_bw()
Run Code Online (Sandbox Code Playgroud)
这使:

正如你所看到的,它的填充量超出了应有的水平.我怎么解决这个问题?
数据:
df <- structure(list(year = c(1901, 1901, 1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919, 1920, 1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929, 1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, …Run Code Online (Sandbox Code Playgroud) R中用于将数字四舍五入为2小数点的公共代码是:
> a = 14.1234
> round(a, digits=2)
> a
> 14.12
Run Code Online (Sandbox Code Playgroud)
但是,如果数字的前两个十进制数字为零,则R在显示中禁止为零:
> a = 14.0034
> round(a, digits=2)
> a
> 14
Run Code Online (Sandbox Code Playgroud)
即使它们是零,我们怎样才能使R显示第一个十进制数字?我特别需要这个情节.我在这里搜索了一些人建议使用options(digits=2),但这使得R有一种奇怪的行为.
我对闰年有些怀疑,我怎么能确定使用这样的公式
add.years= function(x,y){
if(!isTRUE(all.equal(y,round(y)))) stop("Argument \"y\" must be an integer.\n")
x <- as.POSIXlt(x)
x$year <- x$year+y
as.Date(x)
}
Run Code Online (Sandbox Code Playgroud)
它会考虑闰年,例如在观察数据集中添加100年?我该怎么控制呢?
我有一个时间序列数据集,有50年的观察:
date obs
1995-01-01 1.0
1995-01-02 2.0
1995-01-03 2.5
...
2045-12-30 0.2
2045-12-31 0.1
Run Code Online (Sandbox Code Playgroud)
数据集+ 100年
date obs
2095-01-01 1.0
2095-01-02 2.0
2095-01-03 2.5
...
2145-12-30 0.2
2145-12-31 0.1
Run Code Online (Sandbox Code Playgroud)
经过基本检查后,我注意到原始数据集和数据集后100年的行数相同.我不确定闰年之前的第29届Februray之前是否会出现3月1日非闰年的价值等等.
我可以检查闰年从使用chron的库函数leap.year,但是我想知道是否有这样做,以确保一个更简单的方法,与2月29日的天通该行以后的100年不存在将被删除,并且2月29日的新日期将添加NA值.
我想重塑一个宽格式数据集,该数据集具有多个测试,这些测试在3个时间点进行测量:
ID Test Year Fall Spring Winter
1 1 2008 15 16 19
1 1 2009 12 13 27
1 2 2008 22 22 24
1 2 2009 10 14 20
2 1 2008 12 13 25
2 1 2009 16 14 21
2 2 2008 13 11 29
2 2 2009 23 20 26
3 1 2008 11 12 22
3 1 2009 13 11 27
3 2 2008 17 12 23
3 2 2009 14 9 31
Run Code Online (Sandbox Code Playgroud)
进入一个按列分隔测试的数据集,但将测量时间转换为长格式,对于每个新列,如下所示: …
我的数据集中的一个变量包含Google搜索结果页的网址.我想从这些网址中提取搜索关键字.
示例数据集:
keyw <- structure(list(user = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("p1", "p2"), class = "factor"),
url = structure(c(3L, 5L, 4L, 1L, 2L, 6L), .Label = c("https://www.google.nl/search?q=five+fingers&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=kERoVbmMO6fp7AaGioCYAw", "https://www.google.nl/search?q=five+fingers&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=kERoVbmMO6fp7AaGioCYAw#safe=off&q=five+short+fingers+", "https://www.google.nl/search?q=high+five&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=bENoVZSqL4ON7Qb5wIDIDg", "https://www.google.nl/search?q=high+five&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=bENoVZSqL4ON7Qb5wIDIDg#safe=off&q=high+five+with+a+chair", "https://www.google.nl/search?q=high+five&ie=utf-8&oe=utf-8&gws_rd=cr,ssl&ei=bENoVZSqL4ON7Qb5wIDIDg#safe=off&q=high+five+with+handshake", "https://www.youtube.com/watch?v=6HOallAdtDI"), class = "factor")),
.Names = c("user", "url"), class = "data.frame", row.names = c(NA, -6L))
Run Code Online (Sandbox Code Playgroud)
到目前为止,我能够从URL中提取搜索关键字部分:
keyw$words <- sapply(str_extract_all(keyw$url, 'q=([^&#]*)'),paste, collapse=",")
Run Code Online (Sandbox Code Playgroud)
但是,这仍然没有给我想要的结果.上面的代码给出了以下结果:
> keyw$words
[1] "q=high+five"
[2] "q=high+five,q=high+five+with+handshake"
[3] "q=high+five,q=high+five+with+a+chair"
[4] "q=five+fingers"
[5] "q=five+fingers,q=five+short+fingers+"
[6] ""
Run Code Online (Sandbox Code Playgroud)
此输出有三个问题:
q=high+five,我需要high,five.NA. …