Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 生成可以计算恢复百分比的直方图_R_Plot_Histogram - Fatal编程技术网

R 生成可以计算恢复百分比的直方图

R 生成可以计算恢复百分比的直方图,r,plot,histogram,R,Plot,Histogram,我有以下名为df的数据集: Amp Injected Recovered Percent less_0.1_True 0.13175 25.22161274 0.96055540 3.81 0 0.26838 21.05919344 21.06294791 100.02 1 0.07602 16.88526724 16.91541763 100.18 1 0.04608 27.50209048 27.55404507 100.19 0 0.01729 8.31489333

我有以下名为df的数据集:

  Amp Injected Recovered Percent less_0.1_True
  0.13175 25.22161274 0.96055540 3.81 0
  0.26838 21.05919344 21.06294791 100.02 1
  0.07602 16.88526724 16.91541763 100.18 1
  0.04608 27.50209048 27.55404507 100.19 0
  0.01729 8.31489333 8.31326976 99.98 1
  0.31867 4.14961918 4.14876247 99.98 0
  0.28756 14.65843377 14.65248551 99.96 1
  0.26177 10.64754579 10.76435667 101.10 1
  0.23214 6.28826689 6.28564299 99.96 1
  0.20300 17.01774090 1.05925850 6.22 0
  ...
在这里,less_0.1_True列标记恢复的周期是否足够接近注入周期,以被视为成功恢复。如果标志为1,则表示成功恢复。基于此,我需要生成一个图(Henderson&Stasson,《天体物理学杂志》,747:512012),如下所示:

我不知道如何创建这样的直方图。我所做的最接近的是带有以下代码的条形图:

breaks <- seq(0,30,by=1)
df <- split(dat, cut(dat$Injected,breaks)) # I make bins with width = 1 day
x <- seq(1,30,by=1)

len <- numeric() #Here I store the total number of objects in each bin
sum <- numeric() #Here I store the total number of 1s in each bin

for (i in 1:30){
n <- nrow(df[[i]])
len <- c(len,n)

s <- sum(df[[i]]$less_0.1_True == 1, na.rm = TRUE)
sum <- c(sum,s)
}

percent = sum/len*100 #Here I calculate what the percentage is for each bin
barplot(percent, names = x, xlab = "Period [d]" , ylab = "Percent Recovered", ylim=c(0,100))

中断如果我运行您的代码,就会出现错误。您需要使用
border=NA
去除条形图边框:

set.seed(42)
hist(rnorm(1000,4), xlim=c(0,10), col = 'skyblue', border = NA, main = "Histogram", xlab = NULL)

使用
ggplot2
的另一个示例:

ggplot(iris, aes(x=Sepal.Length))+
  geom_histogram()

如果我运行你的代码,我会出错。您需要使用
border=NA
去除条形图边框:

set.seed(42)
hist(rnorm(1000,4), xlim=c(0,10), col = 'skyblue', border = NA, main = "Histogram", xlab = NULL)

使用
ggplot2
的另一个示例:

ggplot(iris, aes(x=Sepal.Length))+
  geom_histogram()

我终于找到了解决StackOverflow问题的方法。我猜解决的问题的措辞与我的不同,因此我在最初寻找时找不到它。解决方案如下:

我终于找到了StackOverflow问题的解决方案。我猜解决的问题的措辞与我的不同,因此我在最初寻找时找不到它。解决方案如下: