我有以下名为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& Stassun,Astrophysical Journal,747:51,2012),如下所示:
我不知道如何创建这样的直方图。我最近的重现是一个带有以下代码的条形图:
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))
它会生成以下条形图:
显然,这个情节看起来不像第一个,并且有一些问题,例如它不会像第一个图一样从0到1显示(我理解是这种情况,因为后者是条形图而不是直方图)。
有人可以指导我如何基于我的数据集重现第一个数字吗?
答案 0 :(得分:0)
如果我运行你的代码,我会收到错误。您需要使用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()
答案 1 :(得分:0)
我终于在StackOverflow中找到了解决问题的方法。我想解决的问题措辞与我的不同,所以当我最初寻找它时,我找不到它。解决方案如下:How to plot a histogram with a custom distribution?