ggproto

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

邮差的信 提交于 2019-12-18 12:24:20
问题 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

How to adjust facet size manually

穿精又带淫゛_ 提交于 2019-12-18 11:25:24
问题 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',

How to determine the geom type of each layer of a ggplot2 object?

心不动则不痛 提交于 2019-12-18 03:38:29
问题 As part of an effort to remove a specific geom from a plot I've already created (SO link here), I'd like to dynamically determine the geom type of each layer of a ggplot2 object. Assuming I don't know the order in which I added layers, is there a way to dynamically find layers with a specific geom? If I print out the layers like I do below I can see that the layers are stored in a list, but I can't seem to access the geom type. library(ggplot2) dat <- data.frame(x=1:3, y=1:3, ymin=0:2, ymax=2

Split violin plot with ggplot2

本小妞迷上赌 提交于 2019-12-17 02:24:33
问题 I'd like to create a split violin density plot using ggplot, like the fourth example on this page of the seaborn documentation. Here is some data: set.seed(20160229) my_data = data.frame( y=c(rnorm(1000), rnorm(1000, 0.5), rnorm(1000, 1), rnorm(1000, 1.5)), x=c(rep('a', 2000), rep('b', 2000)), m=c(rep('i', 1000), rep('j', 2000), rep('i', 1000)) ) I can plot dodged violins like this: library('ggplot2') ggplot(my_data, aes(x, y, fill=m)) + geom_violin() But it's hard to visually compare the

Call setup_data from parent class

帅比萌擦擦* 提交于 2019-12-11 10:47:56
问题 Backround I was reading this excellent answer on how to place self adjusting text into bars: Resizeable text grobs inside bars After reading a bit on ggproto and especially the vignette on Extending ggplot I was wondering why the author had to define the setup_data routine as follows: GeomFit <- ggproto("GeomFit", GeomRect, setup_data = function(data, params) { data$width <- data$width %||% params$width %||% (resolution(data$x, FALSE) * 0.9) transform(data, ymin = pmin(y, 0), ymax = pmax(y, 0

ggplot not plotting ggmap object

こ雲淡風輕ζ 提交于 2019-12-08 03:54:51
问题 When I run the code from the accepted answer (Plot coordinates on map), I get the following error message on the first run after installing ggmap: # loading the required packages library(ggplot2) library(ggmap) # creating a sample data.frame with your lat/lon points lon <- c(-38.31,-35.5) lat <- c(40.96, 37.5) df <- as.data.frame(cbind(lon,lat)) # getting the map mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 4, maptype = "satellite", scale = 2) # plotting

Creating geom / stat from scratch

╄→гoц情女王★ 提交于 2019-12-06 21:56:30
问题 I just started working with R not long ago, and I am currently trying to strengthen my visualization skills. What I want to do is to create boxplots with mean diamonds as a layer on top (see picture in the link below). I did not find any functions that does this already, so I guess I have to create it myself. What I was hoping to do was to create a geom or a stat that would allow something like this to work: ggplot(data, aes(...))) + geom_boxplot(...) + geom_meanDiamonds(...) I have no idea

ggplot: percentile lines by group automation

别等时光非礼了梦想. 提交于 2019-12-05 09:26:34
I've found the dplyr %>% operator helpful with simple ggplot2 transformations (without resorting to ggproto , which is required for ggplot2 extensions ), e.g. library(ggplot2) library(scales) library(dplyr) gg.histo.pct.by.group <- function(g, ...) { g + geom_histogram(aes(y=unlist(lapply(unique(..group..), function(grp) ..count..[..group..==grp] / sum(..count..[..group..==grp])))), ...) + scale_y_continuous(labels = percent) + ylab("% of total count by group") } data = diamonds %>% select(carat, color) %>% filter(color %in% c('H', 'D')) g = ggplot(data, aes(carat, fill=color)) %>% gg.histo

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

家住魔仙堡 提交于 2019-12-05 07:12:41
问题 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? 回答1: Okay, adding this one as a second

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

十年热恋 提交于 2019-12-04 16:27:27
问题 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 <-