简体   繁体   English

在R数据框中缩放数据并将高斯拟合为geom_point

[英]Scaling data in R data frame and fitting gaussian to geom_point

2 questions based on my data.frame 根据我的data.frame 2个问题

structure(list(Collimator = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("n", "y"), class = "factor"), angle = c(0L, 
15L, 30L, 45L, 60L, 75L, 90L, 105L, 120L, 135L, 150L, 165L, 180L, 
0L, 15L, 30L, 45L, 60L, 75L, 90L, 105L, 120L, 135L, 150L, 165L, 
180L), X1 = c(2099L, 11070L, 17273L, 21374L, 23555L, 23952L, 
23811L, 21908L, 19747L, 17561L, 12668L, 6008L, 362L, 53L, 21L, 
36L, 1418L, 6506L, 10922L, 12239L, 8727L, 4424L, 314L, 38L, 21L, 
50L), X2 = c(2126L, 10934L, 17361L, 21301L, 23101L, 23968L, 23923L, 
21940L, 19777L, 17458L, 12881L, 6051L, 323L, 40L, 34L, 46L, 1352L, 
6569L, 10880L, 12534L, 8956L, 4418L, 344L, 58L, 24L, 68L), X3 = c(2074L, 
11109L, 17377L, 21399L, 23159L, 23861L, 23739L, 21910L, 20088L, 
17445L, 12733L, 6046L, 317L, 45L, 26L, 46L, 1432L, 6495L, 10862L, 
12300L, 8720L, 4343L, 343L, 38L, 34L, 60L), average = c(2099.6666666667, 
11037.6666666667, 17337, 21358, 23271.6666666667, 23927, 23824.3333333333, 
21919.3333333333, 19870.6666666667, 17488, 12760.6666666667, 
6035, 334, 46, 27, 42.6666666667, 1400.6666666667, 6523.3333333333, 
10888, 12357.6666666667, 8801, 4395, 333.6666666667, 44.6666666667, 
26.3333333333, 59.3333333333)), .Names = c("Collimator", "angle", 
"X1", "X2", "X3", "average"), row.names = c(NA, -26L), class = "data.frame")

I wish to plot detector counts versus angle with and without a collimator attached to the device. 我希望在有和没有将准直仪连接到设备的情况下绘制检测器计数与角度的关系图。 I guess geom_point is probably the best way to summarise the data 我猜想geom_point可能是汇总数据的最佳方法

p <- ggplot(df, aes(x=angle,y=average,col=Collimator)) + geom_point() + geom_line()

Instead of plotting average count in the y-axis, I would prefer to rescale the data so that the angle with max counts has a value 1 for both collimator Y and N. The way I have done this seems quite cumbersome 我不希望在y轴上绘制平均计数,而是希望重新缩放数据,以使最大计数的角度对准直仪Y和N的值均为1。我这样做的方法似乎很麻烦

range01 <- function(x){(x-min(x))/(max(x)-min(x))}
coly = subset(df,Collimator=='y')
coly$norm_count = range01(coly$average)
coln = subset(df,Collimator=='n')
coln$norm_count = range01(coln$average)
df = rbind(coln,coly)
p <- ggplot(df, aes(x=angle,y=norm_count,col=Collimator) + geom_point() + geom_line()

I'm sure this can be done in a more efficient manner, applying the function to the data.frame based on the variable 'Collimator'. 我敢肯定,这可以通过将变量基于“ Collimator”应用于data.frame来以更有效的方式完成。 How can I do this? 我怎样才能做到这一点?

Also I want to fit a function to the data rather than using geom_line. 我也想使函数适合数据而不是使用geom_line。 I think a Gaussian function may work in this case but have no idea how/if I can implement this in stat_smooth . 我认为高斯函数可以在这种情况下工作,但不知道如何/是否可以在stat_smooth实现这stat_smooth Also can I pull out mead/standard deviation from such a fit? 我还能从这种拟合中提取米德/标准偏差吗?

ggplot2 goes hand in hand with the package plyr : ggplot2plyr软件包并驾齐驱

df <- ddply(df,.(Collimator),
            transform,
            norm_count1 = (average - min(average)) / (max(average) - min(average)) )

joran's answer scales the highest value to 1 and the lowest to 0; joran的答案将最高值缩放为1,将最低值缩放为0; if you just want to scale to make the highest value 1 (and leaving 0 as 0), it is even simpler. 如果您只想缩放以使最大值为1(并将0保留为0),则它甚至更简单。

library("plyr")
df <- ddply(df, .(Collimator), transform,
            norm.average = average / max(average))

The the plot is 情节是

ggplot(df, aes(x=angle,y=norm.average,col=Collimator)) + 
  geom_point() + geom_line()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM