ggproto

ggplot2: How to conditionally change geom_text's vjust when low bars make text exceed bar's bottom

陌路散爱 提交于 2021-02-09 09:27:49
问题 When plotting a bar chart, I often add labels to bars to signify the y-value for each bar. However, I run into trouble when the bar becomes too low, making the label unreadable or simply ugly. Example library(ggplot2) df_blood <- data.frame(blood_type = c("O-", "O+", "A-", "A+", "B-", "B+", "AB-", "AB+"), frequency = c(0.13, 0.35, 0.08, 0.3, 0.02, 0.08, 0.01, 0.02)) ggplot(df_blood, aes(x = blood_type, y = frequency, fill = blood_type)) + geom_bar(stat = "identity") + geom_text(aes(label =

ggplot2: How to conditionally change geom_text's vjust when low bars make text exceed bar's bottom

a 夏天 提交于 2021-02-09 09:27:25
问题 When plotting a bar chart, I often add labels to bars to signify the y-value for each bar. However, I run into trouble when the bar becomes too low, making the label unreadable or simply ugly. Example library(ggplot2) df_blood <- data.frame(blood_type = c("O-", "O+", "A-", "A+", "B-", "B+", "AB-", "AB+"), frequency = c(0.13, 0.35, 0.08, 0.3, 0.02, 0.08, 0.01, 0.02)) ggplot(df_blood, aes(x = blood_type, y = frequency, fill = blood_type)) + geom_bar(stat = "identity") + geom_text(aes(label =

ggplot2: How to conditionally change geom_text's vjust when low bars make text exceed bar's bottom

∥☆過路亽.° 提交于 2021-02-09 09:23:22
问题 When plotting a bar chart, I often add labels to bars to signify the y-value for each bar. However, I run into trouble when the bar becomes too low, making the label unreadable or simply ugly. Example library(ggplot2) df_blood <- data.frame(blood_type = c("O-", "O+", "A-", "A+", "B-", "B+", "AB-", "AB+"), frequency = c(0.13, 0.35, 0.08, 0.3, 0.02, 0.08, 0.01, 0.02)) ggplot(df_blood, aes(x = blood_type, y = frequency, fill = blood_type)) + geom_bar(stat = "identity") + geom_text(aes(label =

ggplot2: How to conditionally change geom_text's vjust when low bars make text exceed bar's bottom

ⅰ亾dé卋堺 提交于 2021-02-09 09:23:02
问题 When plotting a bar chart, I often add labels to bars to signify the y-value for each bar. However, I run into trouble when the bar becomes too low, making the label unreadable or simply ugly. Example library(ggplot2) df_blood <- data.frame(blood_type = c("O-", "O+", "A-", "A+", "B-", "B+", "AB-", "AB+"), frequency = c(0.13, 0.35, 0.08, 0.3, 0.02, 0.08, 0.01, 0.02)) ggplot(df_blood, aes(x = blood_type, y = frequency, fill = blood_type)) + geom_bar(stat = "identity") + geom_text(aes(label =

ggplot: percentile lines by group automation

泄露秘密 提交于 2020-01-13 10:12:33
问题 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

ggplot: percentile lines by group automation

谁说胖子不能爱 提交于 2020-01-13 10:12:28
问题 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

R: adding alpha bags to a 2d or 3d scatterplot

荒凉一梦 提交于 2020-01-09 18:37:09
问题 I know that in ggplot2 one can add the convex hull to a scatterplot by group as in library(ggplot2) library(plyr) data(iris) df<-iris find_hull <- function(df) df[chull(df$Sepal.Length, df$Sepal.Width), ] hulls <- ddply(df, "Species", find_hull) plot <- ggplot(data = df, aes(x = Sepal.Length, y = Sepal.Width, colour=Species, fill = Species)) + geom_point() + geom_polygon(data = hulls, alpha = 0.5) + labs(x = "Sepal.Length", y = "Sepal.Width") plot I was wondering though how one could

Add unit labels to radar plot and remove outer ring ggplot2 (spider web plot, coord_radar)

狂风中的少年 提交于 2019-12-22 08:54:14
问题 I've been working on a radar plot recently and found a great bit of code on from Erwan Le Pennec. I've made a few edits, however there are some parts I couldn't work out how to remove the outermost circle as it doesn't define a value. Currently the code is plotting the major grid lines. I've tried to trick it into removing both axis and only plotting the minor increments however either has worked. Cheers! Here is my code: coord_radar <- function (theta = "x", start = 0, direction = 1) { theta

Adding table to ggplot with facets

痞子三分冷 提交于 2019-12-21 22:40:37
问题 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

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

谁都会走 提交于 2019-12-21 06:38:34
问题 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