histogram

How To Avoid Density Curve Getting Cut Off In Plot

别说谁变了你拦得住时间么 提交于 2019-11-27 07:56:04
问题 This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 4 years ago . I am working on an assignment using R and the fitted density curve that is overlaid on the histogram is cut off at it's peak. Example: x <- rexp(1000, 0.2) hist(x, prob = TRUE) lines(density(x), col = "blue", lty = 3, lwd = 2) I have done a search on the internet for this but didn't find anything addressing this problem. I have tried playing with the margins, but that doesn't

Extract data from a ggplot

最后都变了- 提交于 2019-11-27 07:26:06
I have made a plot using ggplot2 geom_histogram from a data frame. See sample below and link to the ggplot histogram Need to label each geom_vline with the factors using a nested ddply function and facet wrap I now need to make a data frame that contains the summarized data used to generate the ggplot above. Sector2 Family Year Length BUN Acroporidae 2010 332.1300496 BUN Poritidae 2011 141.1467966 BUN Acroporidae 2012 127.479 BUN Acroporidae 2013 142.5940556 MUR Faviidae 2010 304.0405 MUR Faviidae 2011 423.152 MUR Pocilloporidae 2012 576.0295 MUR Poritidae 2013 123.8936667 NTH Faviidae 2010 60

How to fill histogram with color gradient?

为君一笑 提交于 2019-11-27 07:12:34
问题 I have a simple problem. How to plot histogram with ggplot2 with fixed binwidth and filled with rainbow colors (or any other palette)? Lets say I have a data like that: myData <- abs(rnorm(1000)) I want to plot histogram, using e.g. binwidth=.1 . That however will cause different number of bins, depending on data: ggplot() + geom_histogram(aes(x = myData), binwidth=.1) If I knew number of bins (e.g. n=15 ) I'd use something like: ggplot() + geom_histogram(aes(x = myData), binwidth=.1, fill

R histogram with multiple populations

醉酒当歌 提交于 2019-11-27 06:44:22
问题 I'm interested in creating a histogram in R that will contain two (or more) population on top of each other, meaning - I don't want a two histograms sharing the same graph but a bar containing two colors or more. Found the image below - this is what I want to accomplish. Any ideas? 回答1: That is actually the annoying default in ggplot2: library(ggplot2) ggplot(iris, aes(x=Sepal.Length, fill=Species)) + geom_histogram() 回答2: Here is another option without using ggplot: #plot the entire data set

Create a histogram for weighted values

梦想的初衷 提交于 2019-11-27 06:38:39
问题 If I have a vector (e.g., v<-runif(1000) ), I can plot its histogram (which will look, more or less, as a horizontal line because v is a sample from the uniform distribution). However, suppose I have a vector and its associated weights (e.g., w<-seq(1,1000) in addition to v<-sort(runif(1000)) ). E.g., this is the result of table() on a much larger data set. How do I plot the new histogram? (it should look more of less like the y=x line in this example). I guess I could reverse the effects of

Normalizing y-axis in histograms in R ggplot to proportion

放肆的年华 提交于 2019-11-27 06:20:47
I have a very simple question causing me to bang my head on the wall. I would like to scale the y-axis of my histogram to reflect the proportion (0 to 1) that each bin makes up, instead of having the area of the bars sum to 1, as using y=..density.. does, or having the highest bar be 1, as y=..ncount.. does. My input is a list of names and values, formatted like so: name value A 0.0000354 B 0.00768 C 0.00309 D 0.000123 One of my failed attempts: library(ggplot2) mydataframe < read.delim(mydata) ggplot(mydataframe, aes(x = value)) + geom_histogram(aes(x=value,y=..density..)) This gives me a

Plot histogram with colors taken from colormap

有些话、适合烂在心里 提交于 2019-11-27 05:32:30
问题 I want to plot a simple 1D histogram where the bars should follow the color-coding of a given colormap. Here's an MWE : import numpy as n import matplotlib.pyplot as plt # Random gaussian data. Ntotal = 1000 data = 0.05 * n.random.randn(Ntotal) + 0.5 # This is the colormap I'd like to use. cm = plt.cm.get_cmap('RdYlBu_r') # Plot histogram. n, bins, patches = plt.hist(data, 25, normed=1, color='green') plt.show() which outputs this: Instead of the color being green for the entire histogram, I

How to get the cumulative distribution function with NumPy?

情到浓时终转凉″ 提交于 2019-11-27 05:25:33
问题 I want to create a CDF with NumPy, my code is the next: histo = np.zeros(4096, dtype = np.int32) for x in range(0, width): for y in range(0, height): histo[data[x][y]] += 1 q = 0 cdf = list() for i in histo: q = q + i cdf.append(q) I am walking by the array but take a long time the program execution. There is a built function with this feature, isn't? 回答1: I'm not really sure what your code is doing, but if you have hist and bin_edges arrays returned by numpy.histogram you can use numpy

fast 2dimensional histograming in matlab

帅比萌擦擦* 提交于 2019-11-27 04:41:43
I have written a 2D histogram algorithm for 2 matlab vectors. Unfortunately, I cannot figure out how to vectorize it, and it is about an order of magnitude too slow for my needs. Here is what I have: function [ result ] = Hist2D( vec0, vec1 ) %Hist2D takes two vectors, and computes the two dimensional histogram % of those images. It assumes vectors are non-negative, and bins % are the integers. % % OUTPUTS % result - % size(result) = 1 + [max(vec0) max(vec1)] % result(i,j) = number of pixels that have value % i-1 in vec0 and value j-1 in vec1. result = zeros(max(vec0)+1, max(vec1)+1); fvec0 =

R code to categorize age into group/ bins/ breaks

旧城冷巷雨未停 提交于 2019-11-27 04:32:49
I am trying to categorize age into group so it will not be continuous. I have this code: data$agegrp(data$age>=40 & data$age<=49) <- 3 data$agegrp(data$age>=30 & data$age<=39) <- 2 data$agegrp(data$age>=20 & data$age<=29) <- 1 the above code is not working under survival package. It's giving me: invalid function in complex assignment Can you point me where the error is? data is the dataframe I am using. A5C1D2H2I1M1N2O1R2T1 I would use findInterval() here: First, make up some sample data set.seed(1) ages <- floor(runif(20, min = 20, max = 50)) ages # [1] 27 31 37 47 26 46 48 39 38 21 26 25 40