Question

I was following this https://stackoverflow.com/a/3542115/3483997 approach in order to get 2 histograms (with different populations) integrated in the same data-frame.

ROS_SPITFIRE <- data.frame(length = rnorm(100, 0.76406353, 0.500970292)) ROS_FARSITE <- data.frame(length = rnorm(398, 3.48366834170854,2.19050069588744))

#Now, combine your two dataframes into one. First make a new column in each. ROS_SPITFIRE$veg <- 'ROS_SPITFIRE' ROS_FARSITE$veg <- 'ROS_FARSITE'

#and combine into your new data frame vegLengths vegLengths <- rbind(ROS_SPITFIRE, ROS_FARSITE)

#now make your lovely plot ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.3) ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.3)

ggplot(vegLengths, aes(length, fill = veg)) + geom_histogram(alpha = 0.5, aes(y = ..density..), position = 'identity') ggplot = ggplot + xlim((0,15))

My problem popped up when I´ve created the new column in each data-frame. It generates negative values, hence my final distribution plots have negative values on the X-axes. Does anyone know how to fix it?

Thx

Was it helpful?

Solution

If you need to stick with the specific means and sds used but exclude any negative values you can filter the datasets e.g. ROS_SPITFIRE[ROS_SPITFIRE$length>0,] or do limits on the chart e.g. xlim(0,12).

If you can amend the distribution you can pick distributions or values that do not result in negative values. @Dave and @jbaum provide guidance like using sample(seq(.1, 1, by = .1), 100, replace = T) or evaluating other distribution options for going down this route.

You can also cut out some steps by going straight to charting and provide a limit:

ggplot(ROS_SPITFIRE, aes(length, y = ..density..,fill="spitfire")) + 
geom_histogram(alpha = 0.5,  position = 'identity')+ 
geom_histogram(data=ROS_FARSITE, aes(fill="farsite"), 
                 alpha = 0.5, position = 'identity')+
xlim(0,12)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top