Questions tagged [geom-bar]

`geom_bar` is the geometric object description for creating a barplot in `ggplot2`

geom_bar is the geometric object description for creating a barplot in ggplot2, which is an actively maintained open-source chart-drawing library for R. is written by Hadley Wickham and based upon the principles of "Grammar of Graphics".

Resources for using geom_bar:

1029 questions
87
votes
2 answers

Change bar plot colour in geom_bar with ggplot2 in r

I have the following in order to bar plot the data frame. c1 <- c(10, 20, 40) c2 <- c(3, 5, 7) c3 <- c(1, 1, 1) df <- data.frame(c1, c2, c3) ggplot(data=df, aes(x=c1+c2/2, y=c3)) + geom_bar(stat="identity", width=c2) + …
tuttifolies
  • 1,417
  • 2
  • 14
  • 20
65
votes
3 answers

customize ggplot2 axis labels with different colors

I have a basic bar graph I've created from ggplot2. The y variable contains both positive and negative values and about half the vector of values are negative. I would like to customize the axis labels such that when the y value of that…
Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
49
votes
2 answers

geom_text how to position the text on bar as I want?

I would like to adjust the text on the barplot. I tried to adjust hjust/vjust to display as I like it but it seems like it's not working properly. ggplot(data) + geom_bar(aes(name, count, fill = week), stat='identity', position =…
tmhs
  • 998
  • 2
  • 14
  • 27
49
votes
4 answers

Stacked bar chart

I would like to create a stacked chart using ggplot2 and geom_bar. Here is my source data: Rank F1 F2 F3 1 500 250 50 2 400 100 30 3 300 155 100 4 200 90 10 I want a stacked chart where x is the rank and…
WongSifu
  • 547
  • 1
  • 5
  • 11
42
votes
2 answers

ggplot geom_bar: meaning of aes(group = 1)

I am learning geom_bar on section 3.7 of r4ds.had.co.nz. I run a code like this: library(ggplot2) ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1)) Then I have this plot: The point is, if I exclude the "group = 1"…
Novembrain
  • 423
  • 1
  • 4
  • 7
37
votes
1 answer

ggplot2 geom_bar - how to keep order of data.frame

I have a question concerning the order of data in my geom_bar. This is my dataset: SM_P,Spotted melanosis on palm,16.2 DM_P,Diffuse melanosis on palm,78.6 SM_T,Spotted melanosis on trunk,57.3 DM_T,Diffuse melanosis on trunk,20.6 …
Stücke
  • 868
  • 3
  • 14
  • 41
37
votes
1 answer

How to add frequency count labels to the bars in a bar graph using ggplot2?

I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that. What I can't figure out is how to add frequency…
Tamer Koksal
  • 393
  • 1
  • 3
  • 5
29
votes
1 answer

ggplot2 and a Stacked Bar Chart with Negative Values

Given the following dataset: Division Year OperatingIncome 1 A 2012 11460 2 B 2012 7431 3 C 2012 -8121 4 D 2012 15719 5 E 2012 364 6 A 2011 12211 7 B 2011 …
sogrady
  • 756
  • 3
  • 8
  • 12
26
votes
3 answers

ggplot replace count with percentage in geom_bar

I have a dataframe d: > head(d,20) groupchange Symscore3 1 4 1 2 4 2 3 4 1 4 4 2 5 5 0 6 5 0 7 5 0 8 4…
Donbeo
  • 17,067
  • 37
  • 114
  • 188
24
votes
4 answers

ggplot bar plot with facet-dependent order of categories

I've seen many questions (often linked to Order Bars in ggplot2 bar graph) about how to (re)order categories in a bar plot. What I am after is just a touch different, but I haven't found a good way to do it: I have a multi-faceted bar plot, and I…
Pierre D
  • 24,012
  • 7
  • 60
  • 96
21
votes
2 answers

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

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…
elikesprogramming
  • 2,506
  • 2
  • 19
  • 37
19
votes
1 answer

Control bar border (color) thickness with ggplot2 stroke

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…
lawyeR
  • 7,488
  • 5
  • 33
  • 63
18
votes
2 answers

How to increase the space between grouped bars in ggplot2?

[Data and code to produce plot at the end of the post] Using ggplot, I plotted a bargraph with error bars, bars are grouped by two factors (one on X axis, one with fill). I would like to increase the green distance between the groups on the xaxis to…
Kastany
  • 427
  • 1
  • 5
  • 16
17
votes
4 answers

ggplot: Order bars in faceted bar chart per facet

I have a dataframe in R that I want to plot in a faceted ggplot bar chart. I use this code in ggplot: ggplot(data_long, aes(x = partei, y = wert, fill = kat, width=0.75)) + labs(y = "Wähleranteil [ % ]", x = NULL, fill = NULL) + …
Mario
  • 2,393
  • 2
  • 17
  • 37
16
votes
2 answers

How expand ggplot bar scale on one side but not the other without manual limits

The goal is to get rid of the space between the tick marks and the base of the bars without cutting off any of the percentage labels beyond the other end of the bars. I am running dozens of bar graphs using R's ggplot2 and trying to follow our…
Tom SLC
  • 183
  • 1
  • 1
  • 5
1
2 3
68 69