geom-bar

ggplot2 How to create vertical line corresponding to quantile in geom_bar plot

这一生的挚爱 提交于 2019-12-06 04:08:32
Currently, I can create a plot such as this: geom_bar ggplot(df.Acc, aes(x = reorder(cities, -accidents), y = accidents)) + geom_bar(stat = "identity", fill="steelblue", alpha=0.75) + geom_hline(yintercept=0, size=0.4, color="black") It's a plot with, let's say, number of bike accidents per year on the y-axis, and the city name would be on the x-axis. I want to add a vertical line to separate all the cities above the 70th percentile and below it. So I've tried with > vlinAcc <- quantile(df.Cities$accidents, .70) > vlinAcc 70% 41.26589 This looks good, all the cities which have value of

Move axis labels in between plot and facet strip

我与影子孤独终老i 提交于 2019-12-05 07:12:40
I am trying to move the facet strip to the far left of my plot, so that the labels sit adjacent to the bars, and the grouping strips sit to their left (see my example below). I assume this must be done with Grobs, but I have little experience with that level of manipulation and would appreciate guidance. Data: structure(list(dept = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Distribution Centre Services", "IT", "Marketing", "Merchandise &

ggplot2: create ordered group bar plot - (use reorder)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 20:34:54
I want to create grouped bar plot while keeping order. If it was single column and not a grouped bar plot use of reorder function is obvious. But not sure how to use it on a melted data.frame. Here is the detail explanation with code example: Lets say we have following data.frame: d.nfl <- data.frame(Team1=c("Vikings", "Chicago", "GreenBay", "Detroit"), Win=c(20, 13, 9, 12)) plotting a simple bar plot while flipping it. ggplot(d.nfl, aes(x = Team1, y=Win)) + geom_bar(aes(fill=Team1), stat="identity") + coord_flip() above plot will not have an order and if I want to order the plot by win I can

Grouped bar chart on R using ggplot2

寵の児 提交于 2019-12-04 20:06:04
How do I create a grouped bar chart on R using ggplot2 using this data? Person Cats Dogs Mr. A 3 1 Mr. B 4 2 So that it shows that shows number of pets owned per person, with this layout Bar chart of pets I have a text file with this data and have used read.delim to read the file on R. I have used this code but it does not produce the bar plot I am looking for. ggplot(data=pets, aes(x=Person, y=Cats, fill=Dogs)) + geom_bar(stat="identity", position=position_dodge()) I am new to R, any help would be appreciated. Thanks in advance. To prepare data for grouped bar plot, use melt() function of

ggplot2: Change color for each facet in bar chart

本小妞迷上赌 提交于 2019-12-04 15:26:18
I have a faceted bar chart done with ggplot2 with this code: ggplot(data_long, aes(x=region, y=wert)) + geom_bar(aes(fill = kat ), position = "dodge", width=.5, stat="identity") + labs(y = "Wähleranteil [ % ]", x = NULL, fill = NULL) + facet_grid(. ~ type) + theme_bw() + theme( strip.background = element_blank(), panel.grid.major = element_line(colour = "grey80"), panel.border = element_blank(), axis.ticks = element_blank(), panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank() ) + theme(legend.position="bottom") + guides(fill=guide_legend(nrow=1,byrow=TRUE)) I would like that

Simple bar plot with multiple variables in R - Similar to Excel

℡╲_俬逩灬. 提交于 2019-12-04 05:45:09
问题 I have this data frame: Unit <- c(A, B, C, D) Yes <- c(50, 65, 20, 41) No <- c(70, 67, 40, 20) Missing <- c(10, 12, 8, 7) df <- data.frame(Unit, Yes, No, Missing) I want to use simple bar plot such as in Excel (Please see attached plot):Excel Plot https://i.stack.imgur.com/BvWSA.jpg I used ggplot but only for one Var, If I add others it gave me error: ggplot(data = df, aes(x = Unit, y = Yes)) + geom_col() + geom_text(aes(label = Yes), position = position_stack(vjust = 0.5)) Thank you. 回答1:

order and fill with 2 different variables geom_bar ggplot2 R

给你一囗甜甜゛ 提交于 2019-12-04 04:03:23
I have a question concerning the fill field in geom_bar of the ggplot2 package. I would like to fill my geom_bar with a variable (in the next example the variable is called var_fill ) but order the geom_plot with another variable (called clarity in the example). How can I do that? Thank you very much! The example: rm(list=ls()) set.seed(1) library(dplyr) data_ex <- diamonds %>% group_by(cut, clarity) %>% summarise(count = n()) %>% ungroup() %>% mutate(var_fill= LETTERS[sample.int(3, 40, replace = TRUE)]) head(data_ex) # A tibble: 6 x 4 cut clarity count var_fill <ord> <ord> <int> <chr> 1 Fair

In ggplot2, can borders of bars be changed on only one side? (color, thickness)

泄露秘密 提交于 2019-12-04 03:44:47
问题 I know, 3D Barcharts are a sin. But i´m asked to do them and as a trade-off i suggested to only make a border with a slightly darker color than the bar´s on the top and the right side of the bar. Like that, the bars would have some kind of "shadow" (urgh) but at least you still would be able to compare them. Is there any way to do this? ggplot(diamonds, aes(clarity)) + geom_bar() 回答1: Another possibility, using two sets of geom_bar . The first set, the green ones, are made slightly higher and

Control bar border (color) thickness with ggplot2 stroke

六眼飞鱼酱① 提交于 2019-12-03 05:49:51
Is it possible to use the stroke argument introduced with ggplot2 2.0 to adjust the thickness of borders around bars? If not, is there a way to control bar-border thickness along the lines of point-border thickness? Stroke applies to borders around certain shapes -- see the second answer A very modest MWE, showing fill only: factor <- c("One", "Two", "Three", "Four") value <- c(1, 2, 3, 4) factor2 <- c("A", "B", "A", "B") df <- data.frame(factor = factor(factor, levels = factor), value = value, factor2 = factor2) ggplot(df, aes(x = factor, y = value, color = factor2)) + geom_bar(stat =

Positioning values of stacked barchart in the center using ggplot2 [duplicate]

巧了我就是萌 提交于 2019-12-02 14:32:43
问题 This question already has answers here : Showing data values on stacked bar chart in ggplot2 (2 answers) Closed 2 years ago . I've created a stacked barchart and faceted using two variables. I'm unable to position the values in the barchart in the middle, it either appears at the extremes or overlaps. The expected image is the following: I've pasted the script below. Any help would be appreciated. library(dplyr) library(reshape2) library(ggplot2) year<-c("2000","2000","2010","2010","2000",