基于r中的图检索x和y值 [英] retrieve x and y value based on graph in r
问题描述
我是r的新手,我想请大家帮忙.我有x(值)和prob(概率),如下所示:
I'm new in r and I would ask you all some help. I have x (value) and prob (it's probability) as follow:
- x<-c(0.00,1.08,2.08,3.08,4.08,4.64,4.68)
- 概率<-c(0.000,0.600,0.370,0.010,0.006,0.006,0.006)
我的目标是根据这些值构造一个估计分布图.到目前为止,我使用 qplot(x,prob,geom = c("point","smooth"),span = 0.55)
进行制作,如下所示 https://i.stack.imgur.com/aVgNk.png
My aim is to contruct an estimate distribution graph based on those values. So far, I use qplot(x,prob,geom=c("point", "smooth"),span=0.55)
to make it and it's shown here
https://i.stack.imgur.com/aVgNk.png
我的问题是:
- 是否有其他方法可以构建像这样的不错的发行版不使用qplot?
- 我需要检索所有x值(即0.5、1、1.2等)及其对应的概率值.我可以那样做吗?
我已经搜索了一段时间,但是没有运气.
I've been searching for a while, but with no luck.
谢谢大家
推荐答案
如果要为给定的 x
值预测 prob
的值,则为一种方法.请注意,我在这里使用的是黄土
预测函数(因为我相信这是 ggplot
的 smooth
geom的默认设置),可能不适合您.
If you're looking to predict the values of prob
for given values of x
, this is one way to do it. Note I'm using a loess
prediction function here (because I believe it's the default for ggplot
's smooth
geom, which you've used), which may or may not be appropriate for you.
x <- c(0.00, 1.08, 2.08, 3.08, 4.08, 4.64, 4.68)
prob <- c(0.000, 0.600, 0.370, 0.010, 0.006, 0.006, 0.006)
首先创建一个只有一列的数据框,然后将大量数据点放到该列中,只是作一堆预测.
First make a data frame with one column, I'll put a whole lot of data points into that column, just to make a bunch of predictions.
df <- data.frame( datapoints = seq.int( 0, max(x), 0.1 ) )
然后创建一个预测列.我正在使用 predict
函数,将 loess
平滑函数传递给它.将为 loess
函数提供输入数据,并要求 predict
使用 loess
中的函数来预测 df的值$ datapoints
Then create a prediction column. I'm using the predict
function, passing a loess
smoothed function to it. The loess
function is given your input data, and predict
is asked to use the function from loess
to predict for the values of df$datapoints
df$predicted <- predict( loess( prob ~ x, span = 0.55 ), df$datapoints )
这是输出的样子.
> head( df )
datapoints predicted
1 0.0 0.01971800
2 0.1 0.09229939
3 0.2 0.15914675
4 0.3 0.22037484
5 0.4 0.27609841
6 0.5 0.32643223
在绘图方面, ggplot2
是一个不错的选择,因此在这里我不认为有理由回避 qplot
.如果希望从 ggplot2
中获得更多的灵活性,则可以更明确地编写功能(如@Jan Sila在另一个答案中提到的那样).这是 ggplot2
的更常见(更灵活)语法的一种方式:
On the plotting side of things, ggplot2
is a good way to go, so I don't see a reason to shy away from qplot
here. If you want more flexibility in what you get from ggplot2
, you can code the functions more explicitly (as @Jan Sila has mentioned in another answer). Here's a way with ggplot2
's more common (and more flexible) syntax:
plot <- ggplot( data = df,
mapping = aes( x = datapoints,
y = predicted ) ) +
geom_point() +
geom_smooth( span = 0.55 )
plot
这篇关于基于r中的图检索x和y值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!