ggproto

Adding table to ggplot with facets

断了今生、忘了曾经 提交于 2019-12-04 15:03:46
Reproducible code: x = sample(1:12,100,replace=TRUE) y = rnorm(100) z = sample(c('Sample A','Sample B'),100,replace=TRUE) d = data.frame(x,y,z) ggplot(data=d, aes(factor(x),y)) + geom_boxplot() + stat_summary(fun.y=mean, geom="line", aes(group=1), color ='red') + stat_summary(fun.y=mean, geom="point", color='red') + xlab('Months') + ylab('Metric') + facet_wrap(~z) I want to add a table at the end of this chart that displays the summary statistics- mean, median, quartiles and number of records for each month on the x-axis. I am not sure how this is possible for a facet layout. This is a

Why is bins parameter unknown for the stat_density2d function? (ggmap)

故事扮演 提交于 2019-12-03 22:48:31
I have been stuck with this for hours. When I run this : library(ggmap) set.seed(1) n=100 df <- data.frame(x=rnorm(n, 0, 1), y=rnorm(n, 0, 1)) TestData <- ggplot (data = df) + stat_density2d(aes(x = x, y = y,fill = as.factor(..level..)),bins=4, geom = "polygon",) + geom_point(aes(x = x, y = y)) + scale_fill_manual(values = c("yellow","red","green","royalblue", "black")) I get this error message : Error: Unknown parameters: bins Does anyone know why? Okay, adding this one as a second answer because I think the descriptions and comments in the first answer are useful and I don't feel like

plot a circle/convex hull arround a given percentage of points

与世无争的帅哥 提交于 2019-12-03 21:27:29
I have x=rnorm(100) y=rnorm(100) plot(x,y) abline(h=0); abline(v=0) From point (0,0) and going outwards I would like to draw a contour/circle/ellipse/freehand convex hull that encloses any given percentage of points. Is there any function or package that can automate this? I have tried the following so far but I can only get a circle with some extrapolation and approximation. I have tried this so far: #calculate radius r<- sqrt(x^2+y^2) df<-data.frame(radius=seq(0,3,0.1), percentage=NA) #get the percentage of points that have a smaller radius than i k<-1 for (i in seq(0,3,0.1)){ df$percentage

ggplot2: Adding sample size information to x-axis tick labels

自古美人都是妖i 提交于 2019-12-03 10:36:37
This question is related to Create custom geom to compute summary statistics and display them *outside* the plotting region (NOTE: All functions have been simplified; no error checks for correct objects types, NAs, etc.) In base R, it is quite easy to create a function that produces a stripchart with the sample size indicated below each level of the grouping variable: you can add the sample size information using the mtext() function: stripchart_w_n_ver1 <- function(data, x.var, y.var) { x <- factor(data[, x.var]) y <- data[, y.var] # Need to call plot.default() instead of plot because # plot(

How to make a custom ggplot2 geom with multiple geometries

血红的双手。 提交于 2019-12-03 04:34:59
问题 I've been reading the vignette on extending ggplot2, but I'm a bit stuck on how I can make a single geom that can add multiple geometries to the plot. Multiple geometries already exist in ggplot2 geoms, for example, we have things like geom_contour (multiple paths), and geom_boxplot (multiple paths and points). But I can't quite see how to extend those into new geoms. Let's say I'm trying to make a geom_manythings that will draw two polygons and one point by computing on a single dataset. One

How to make a custom ggplot2 geom with multiple geometries

孤人 提交于 2019-12-02 18:53:20
I've been reading the vignette on extending ggplot2 , but I'm a bit stuck on how I can make a single geom that can add multiple geometries to the plot. Multiple geometries already exist in ggplot2 geoms, for example, we have things like geom_contour (multiple paths), and geom_boxplot (multiple paths and points). But I can't quite see how to extend those into new geoms. Let's say I'm trying to make a geom_manythings that will draw two polygons and one point by computing on a single dataset. One polygon will be a convex hull for all the points, the second polygon will be a convex hull for a

ggplot2: geom_text resize with the plot and force/fit text within geom_bar

£可爱£侵袭症+ 提交于 2019-11-30 06:46:27
This is actually two questions in one (not sure if goes against SO rules, but anyway). First question is how can I force a geom_text to fit within a geom_bar ? (dynamically according to the values plotted) Looking around, the solutions I found were changing the size of the label. That certainly works, but not for every case. You can change the size for a specific plot to make the text fit within the bar, but when the data changes, you may need to manually change the size of the text again. My real-life problem is that I need to generate the same plot for constantly changing data (daily), so I

How to adjust facet size manually

被刻印的时光 ゝ 提交于 2019-11-30 03:35:59
I have a faceted plot with very diverse data. So some facets have only 1 x value, but some others have 13 x values. I know there is the parameter space='free' which adjusts the width of each facet by the data it represents. My question, is there a possibility to adjust this space manually? Since some of my facets are so small, it is no longer possible to read the labels in the facets. I made a little reproducible example to show what I mean. df <- data.frame(labelx=rep(c('my long label','short'), c(2,26)), labely=rep(c('a','b'), each=14), x=c(letters[1:2],letters[1:26]), y=LETTERS[6:7], i

How to extend ggplot2 boxplot with ggproto?

Deadly 提交于 2019-11-29 14:43:23
问题 I'm often using boxplots in my work and like ggplot2 aesthetics. But standard geom_boxplot lacks two things important for me: ends of whiskers and median labels. Thanks to information from here I've written a function: gBoxplot <- function(formula = NULL, data = NULL, font = "CMU Serif", fsize = 18){ require(ggplot2) vars <- all.vars(formula) response <- vars[1] factor <- vars[2] # A function for medians labelling fun_med <- function(x){ return(data.frame(y = median(x), label = round(median(x

Split violin plot with ggplot2 with quantiles

六眼飞鱼酱① 提交于 2019-11-29 10:33:40
In order to plot half densities, I am using the function described in this post: Split violin plot with ggplot2 However, when I want to draw the quantiles on the densities, like on a normal geom_violin() or geom_boxplot() , I obtain an error message. I would also be interested in adding the number of observations above each half density. Here is an example of what I would like to obtain: data("diamonds") library(ggplot2) # Function described in a previous post GeomSplitViolin <- ggproto("GeomSplitViolin", GeomViolin, draw_group = function(self, data, ..., draw_quantiles = NULL){ data <-