What is the easiest way to fill in the area under the geom_freqpoly line?

On the x axis, time is divided into time intervals. There is an interval column in the data frame that indicates the time for each row. A column is a factor, where each interval represents a different factor level.

Building a histogram or line using geom_histogram and geom_freqpoly works fine, but I would like to have a line like the one provided by geom_freqpoly with a filled area.

I am currently using geom_freqpoly as follows:

ggplot(quake.data, aes(interval, fill=tweet.type)) + geom_freqpoly(aes(group = tweet.type, colour = tweet.type)) + opts(axis.text.x=theme_text(angle=-60, hjust=0, size = 6)) 

plots

I would prefer to have a filled area, such as provided by geom_density , but without smoothing the line:

smoooth

geom_area has been suggested, is there a way to use the statistics generated by ggplot2, such as ..count .., for geom_area y values? Or, do you need to aggregate before using ggplot2?


As indicated in the answer, geom_area (..., stat = "bin") is the solution:

 ggplot(quake.data, aes(interval)) + geom_area(aes(y = ..count.., fill = tweet.type, group = tweet.type), stat = "bin") + opts(axis.text.x=theme_text(angle=-60, hjust=0, size = 6)) 

gives:

desired

+6
r ggplot2
source share
4 answers

Perhaps you want to:

 geom_area(aes(y = ..count..), stat = "bin") 
+9
source share

An additional answer that might help: geom_ribbon can be used to create a filled area between two lines without having to explicitly create a polygon. There is good documentation here: http://had.co.nz/ggplot2/geom_ribbon.html

+3
source share

I'm not quite sure what you are aiming for. You need a line or bars. You should check geom_bar for filled bars. Something like:

 p <- ggplot(data, aes(x = time, y = count)) p + geom_bar(stat = "identity") 

If you want the line to be filled at the bottom, you should look at geom_area , which I personally have not used, but it looks like the construction will be almost the same.

 p <- ggplot(data, aes(x = time, y = count)) p + geom_area() 

Hope this helps. Give more information, and we can probably be more helpful.

Actually, I would select an index, just a row of data, and use this as x, and then use

 p <- ggplot(data, aes(x = index, y = count)) p + geom_bar(stat = "identity") + scale_x_continuous("Intervals", breaks = index, labels = intervals) 
+1
source share

ggplot(quake.data, aes(interval, fill=tweet.type, group = 1)) + geom_density()

But I do not think that this is significant graphics.

+1
source share

All Articles