功能绘图的简单示例:
p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
p + stat_function(fun = function(x) x^2 + 1*2)
Run Code Online (Sandbox Code Playgroud)
是否可以在ggplot中的绘图代码中添加参数列表?像这样的东西?
fun1 <- function(x) x^2 + a*b
p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
p + stat_function(fun = fun1, par=list(a=1, b=2)) + xlim(-5,5)
Run Code Online (Sandbox Code Playgroud) 我有一个列表输出,看起来像这个片段:
[[1]]
[1] 109
[[2]]
integer(0)
[[3]]
[1] 80
Run Code Online (Sandbox Code Playgroud)
有没有办法将其转换为这种格式?
C1 C2
1 109
2 0 (or NA)
3 80
Run Code Online (Sandbox Code Playgroud)
不知道从哪里开始...如果我取消它,我注意到我失去了整数(0)位置,这对我来说很重要.
我在R中有一个大表,需要计算其中一列中值的出现次数,另外将计数放在它自己的列中.没有聚合.
所以,如果我有:
var
23456
34567
45678
23456
etc
Run Code Online (Sandbox Code Playgroud)
我的输出应该是:
var count
23456 2
34567 1
45678 1
23456 2
etc
Run Code Online (Sandbox Code Playgroud)
我已经真正阅读了所有的线程,但似乎无法在没有聚合的情况下找到这样做的方法.我需要的是相当于excel的count(A1:A4;A1).
I'd like to expand observations from single row-per-id to multiple rows-per-id based on a given time interval:
> dput(df)
structure(list(id = c(123, 456, 789), gender = c(0, 1, 1), yr.start = c(2005,
2010, 2000), yr.last = c(2007, 2012, 2000)), .Names = c("id",
"gender", "yr.start", "yr.last"), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -3L))
> df
# A tibble: 3 x 4
id gender yr.start yr.last
<dbl> <dbl> <dbl> <dbl>
1 123 0 2005 2007
2 456 1 2010 2012 …Run Code Online (Sandbox Code Playgroud) 我需要找到我绘制的两条曲线之间的交点ggplot2。
这是我的代码:
ggplot(Table1, aes(x=10^H4,y=10^H5, colour = "blabla")) +
geom_line(size=1) + coord_cartesian(xlim=c(100,1000000000))+
coord_cartesian(ylim=c(0.000000000001,1)) + xlab("blabla")+
ylab("blabla") + ggtitle("blabla")+
scale_y_log10(breaks=c(0.000000000001, 0.000000001, 0.000001, 0.001)
, labels = trans_format("log10", math_format(10^.x)))+
scale_x_log10(breaks=c(100,1000,10000,100000,1000000,10000000,100000000,1000000000)
, labels = trans_format("log10", math_format(10^.x))) +
geom_line(data=Table1,aes(10^H4,10^H6, colour = "blabla1"))+
scale_color_manual("", values =c("blabla"="blue", "blabla1" = "red")
, labels=c("blabla","blabla1"))
Run Code Online (Sandbox Code Playgroud)
我尝试过使用它locator(),它很有用,但并不像我期望的那样精确:
所需点位为 24600。
我也尝试使用intercept(x,y):
a <- 10^A5
b <- 10^A6
intercept(a,b)
3.689776e-07 1.963360e-07 6.622165e-07
Run Code Online (Sandbox Code Playgroud)
事实并非如此,我认为它可能没有考虑到这是对数尺度的事实。
我的数据:
structure(list(H4 = c(2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8,
2.9, …Run Code Online (Sandbox Code Playgroud) 我有一个Data.Frame:
Height <- c(169,176,173,172,176,158,168,162,178)
Run Code Online (Sandbox Code Playgroud)
另一个参考高度和重量.
heights_f <- c(144.8,147.3,149.9,152.4,154.9,157.5,160,162.6,165.1,167.6,170.2,172.7,175.3,177.8,180.3,182.9,185.4,188,190.5,193,195.6)
weights_f <- c(38.6,40.9,43.1,45.4,47.7,49.9,52.2,54.5,56.8,59,61.3,63.6,65.8,68.1,70.4,72.6,74.9,77.2,79.5,81.7,84)
weightfactor_f <- data.frame(heights_f, weights_f)
Run Code Online (Sandbox Code Playgroud)
我现在需要将第一个data.frame中的高度值与第二个中最适合的高度参考值相匹配,并给出相应的参考权重.
我还没有取得任何成功,因为我无法找到任何关于匹配不完全相同的值的信息.
我有一个id和速度的数据集.
id <- c(1,1,1,1,2,2,2,2,3,3,3)
speed <- c(40,30,50,40,45,50,30,55,50,50,60)
i <- cbind(id, speed)
limit <- 35
Run Code Online (Sandbox Code Playgroud)
比如,如果'速度'越过'限制'将其计为1.只有当速度低于并超过'限制'时,您才会再次计算.
我希望数据像.
id | Speed Viol.
----------
1 | 2
---------
2 | 2
---------
3 | 1
---------
Run Code Online (Sandbox Code Playgroud)
这里是id(count).
id1 (1) 40 (2) 50,40
id2 (1) 45,50 (2) 55
id3 (1) 50,50,60
Run Code Online (Sandbox Code Playgroud)
如何做到不使用if().
我试图从1-10生成一个偶数的随机整数.我在R中编写了这段代码,但是有没有更简单的方法呢?这是代码:
repeat{
value=sample.int(10,1)
if(value%%2==0){
break
}
}
Run Code Online (Sandbox Code Playgroud) 我是runnig rep()函数并将一个对象作为参数传递给length.out参数
# Initialization
pop.seq <- seq(0.05,0.95,by=0.1)
for(pop.rate in pop.seq){
# Population vector
pop.neg.count <- ((1-pop.rate)*100)
pop.vec <- c(rep(1,length.out=pop.rate*100),rep(0,length.out = pop.neg.count))
# Loop 1's in split1
for(split11.count in 1:(pop.rate*100)){
# Loop 0's in split1
for(split10.count in 0:(100-(pop.rate*100)-1)){
# print values
---------
--------
---------
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码每次都会在第7次迭代时停止,而pop.vec只有99个元素,以下将产生64的输出:
length(rep(0,length.out = pop.neg.count))
pop.neg.count也在控制台中显示值65.
我在R中安装ggplot2软件包时遇到问题.根据R-studio,我安装了最新版本; 这是R studio首次加载时的说法:
R version 3.2.2 (2015-08-14) -- "Fire Safety"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
Run Code Online (Sandbox Code Playgroud)
我假设这意味着我正在使用正确的版本.但是,当我尝试使用install.packages("ggplot2")命令安装ggplot2包时,我收到以下错误(必须在pastebin中发布,因为它包含链接):
It appears to be some kind of error with my internet connection (not sure how to fix it, googling didn't seem to give a concrete answer). After I got that error, I went ahead and just tried downloading the .zip file for ggplot2 from the official CRAN website. Then, in R-Studio I went ahead …