How can I put the percentage in the Barplot (ggplot2)

I have the scrip below, and can´t put the percetage in the bar plot.

p1 <- explore_data %>% 
    ggplot(aes(nosis_worst_situation, fill=target)) +
    ggtitle("\n Histogram") +
    theme(plot.title = element_text(hjust = 0.5),
         legend.position="none") +
    geom_bar(stat='count', position = 'stack') + 
    facet_grid(is_for_train~.)


p2 <- explore_data %>% 
    ggplot(aes(nosis_worst_situation, fill=target)) +
    ggtitle("\n Percent Stacked Bar Chart") +
    theme(plot.title = element_text(hjust = 0.5)) +
    geom_bar(stat='count', position = 'fill') + 
    facet_grid(is_for_train~.)

options(repr.plot.width=12, repr.plot.height=4)
grid.arrange(p1, p2, nrow = 1)

Anyone can help me?
Thanks!

Hi, and welcome!

Thanks for including code. It's more useful in the form of a reproducible example, called a reprex. In this case, to answer it it necessary 1) to track down the grid.arrange function (found in the gridExtra package and 2) to guess what data explore_data represents.

The data does not have to be your actual data, just data (preferably from mtcars or another standard built-in data set) that produces the same issue.

Thanks for your advice technocrat!
This is the dataset I chose, and below the code:

##                  model  mpg cyl  disp  hp drat     wt  qsec    vs    am
## 1            Mazda RX4 21.0   6 160.0 110 3.90 131.00 16.46 FALSE  TRUE
## 2        Mazda RX4 Wag 21.0   6 160.0 110 3.90 143.75 17.02 FALSE  TRUE
## 3           Datsun 710 22.8   4 108.0  93 3.85 116.00 18.61  TRUE  TRUE
## 4       Hornet 4 Drive 21.4   6 258.0 110 3.08 160.75 19.44  TRUE FALSE
## 5    Hornet Sportabout 18.7   8 360.0 175 3.15 172.00 17.02 FALSE FALSE
## 6              Valiant 18.1   6 225.0 105 2.76 173.00 20.22  TRUE FALSE
## 7           Duster 360 14.3   8 360.0 245 3.21 178.50 15.84 FALSE FALSE
## 8            Merc 240D 24.4   4 146.7  62 3.69 159.50 20.00  TRUE FALSE
## 9             Merc 230 22.8   4 140.8  95 3.92 157.50 22.90  TRUE FALSE
## 10            Merc 280 19.2   6 167.6 123 3.92 172.00 18.30  TRUE FALSE
## 11           Merc 280C 17.8   6 167.6 123 3.92 172.00 18.90  TRUE FALSE
## 12          Merc 450SE 16.4   8 275.8 180 3.07 203.50 17.40 FALSE FALSE
## 13          Merc 450SL 17.3   8 275.8 180 3.07 186.50 17.60 FALSE FALSE
## 14         Merc 450SLC 15.2   8 275.8 180 3.07 189.00 18.00 FALSE FALSE
## 15  Cadillac Fleetwood 10.4   8 472.0 205 2.93 262.50 17.98 FALSE FALSE
## 16 Lincoln Continental 10.4   8 460.0 215 3.00 271.20 17.82 FALSE FALSE
## 17   Chrysler Imperial 14.7   8 440.0 230 3.23 267.25 17.42 FALSE FALSE
## 18            Fiat 128 32.4   4  78.7  66 4.08 110.00 19.47  TRUE  TRUE
## 19         Honda Civic 30.4   4  75.7  52 4.93  80.75 18.52  TRUE  TRUE
## 20      Toyota Corolla 33.9   4  71.1  65 4.22  91.75 19.90  TRUE  TRUE
## 21       Toyota Corona 21.5   4 120.1  97 3.70 123.25 20.01  TRUE FALSE
## 22    Dodge Challenger 15.5   8 318.0 150 2.76 176.00 16.87 FALSE FALSE
## 23         AMC Javelin 15.2   8 304.0 150 3.15 171.75 17.30 FALSE FALSE
## 24          Camaro Z28 13.3   8 350.0 245 3.73 192.00 15.41 FALSE FALSE
## 25    Pontiac Firebird 19.2   8 400.0 175 3.08 192.25 17.05 FALSE FALSE
## 26           Fiat X1-9 27.3   4  79.0  66 4.08  96.75 18.90  TRUE  TRUE
## 27       Porsche 914-2 26.0   4 120.3  91 4.43 107.00 16.70 FALSE  TRUE
## 28        Lotus Europa 30.4   4  95.1 113 3.77  75.65 16.90  TRUE  TRUE
## 29      Ford Pantera L 15.8   8 351.0 264 4.22 158.50 14.50 FALSE  TRUE
## 30        Ferrari Dino 19.7   6 145.0 175 3.62 138.50 15.50 FALSE  TRUE
## 31       Maserati Bora 15.0   8 301.0 335 3.54 178.50 14.60 FALSE  TRUE
## 32          Volvo 142E 21.4   4 121.0 109 4.11 139.00 18.60  TRUE  TRUE

p1 <- explore_data %>%
ggplot(aes(cyl, fill=vs)) +
ggtitle("\n Histogram") +
theme(plot.title = element_text(hjust = 0.5),
legend.position="none") +
geom_bar(stat='count', position = 'stack') +
facet_grid(is_for_train~.)

p2 <- explore_data %>%
ggplot(aes(cyl, fill=vs)) +
ggtitle("\n Percent Stacked Bar Chart") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(stat='count', position = 'fill') +
facet_grid(is_for_train~.)

options(repr.plot.width=12, repr.plot.height=4)
grid.arrange(p1, p2, nrow = 1)

Great. Small adjustment. Here is what a reprex (an addin option--use the search box) should look like

library(dplyr)     # necessary libraries
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(gridExtra)
#> 
#> Attaching package: 'gridExtra'
#> The following object is masked from 'package:dplyr':
#> 
#>     combine

explore_data <- mtcars # mtcars is always available without calling it

p1 <- explore_data %>%
ggplot(aes(cyl, fill=vs)) +
ggtitle("\n Histogram") +
theme(plot.title = element_text(hjust = 0.5),
legend.position="none") +
geom_bar(stat='count', position = 'stack') # +
# facet_grid(is_for_train~.) # omitted because is_for_train not defined

p1 # show the result of defining the plot


p2 <- explore_data %>%
ggplot(aes(cyl, fill=vs)) +
ggtitle("\n Percent Stacked Bar Chart") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(stat='count', position = 'fill') # + omitted
# facet_grid(is_for_train~.) # omitted because is_for_train not defined

p2 # show he result 

Created on 2019-12-30 by the reprex package (v0.3.0)

Now, an assumption is needed about put the percentage in the bar plot. I'm going to assume that means the y-axis being expressed in percentage.

For p2

# reprex above omitted
p2 + scale_y_continuous(labels = scales::percent)

Created on 2019-12-30 by the reprex package (v0.3.0)

1 Like

I´m sory, I expressed wrong. What I´m trying to do is to show the percentage into the bars in the Barplot (p2)
This are the library I´m using:
library(ggplot2)
library(magrittr)
library(dplyr)
library(tidyr)
library(lubridate)
library(Amelia)
library(tabplot)
library(gridExtra)

Do you mean on top of the bars? (BTW: not all of those libraries are needed to illustrate the question, and there's no need to include those.)

1 Like

Yes, I want to show the percentages of trues in each bar on the top or inside each bar.

This is a job for geom_text()

While you can do the percentage calculations within ggplot, because geom_text() takes character arguments, such as 25.2%, it's easier to do the calculation outside and use the object names, such as bar1.

Work the examples in help(geom_text) to get the placement you want. The final example will be particularly helpful.

Come back if you run into roadblocks!

I still can't solve the problem.
The problem is that I want to visualize the percentage of trues in lots of variables, and if I create an argument by result of each variable I'm going to have to generate a lot of arguments, that is why I was traing to do this calculate on the ggplot function.

Here I write what I´m tring to do. The problem is that it gives me the total percentage in each bar, and not the percentage of the individual bars.

p1 <- explore_data %>%
ggplot(aes(cyl, fill=vs)) +
ggtitle("\n Histogram") +
theme(plot.title = element_text(hjust = 0.5),
legend.position="none") +
geom_bar(stat='count', position = 'stack')

p2 <- explore_data %>%
ggplot(aes(cyl, fill=vs)) +
ggtitle("\n Percent Stacked Bar Chart") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(stat='count', position = 'fill') +
geom_text(aes
(y = sum(tcount)/sum(count),
x = nosis_worst_situation,
label=paste0(round(sum(tcount)/sum(count), 4)*100,'%'))) +
scale_y_continuous(labels = scales::percent)

options(repr.plot.width=12, repr.plot.height=4)
grid.arrange(p1, p2, nrow = 1)

Where 'tcount' is 1 always 'vs = True' and 'count' is 1 in each row.

I don't understand well what you really trying to do, but when I need to plot a stacked bar plot I create a percentage variable like this:

library(tidyverse)
library(gapminder)

explore_data <- gapminder %>% 
  filter(continent=="Oceania") %>% 
  select(c(1,3,5)) %>% 
  group_by(year) %>% 
  mutate(perp=round(pop/sum(pop),2)*100)

ggplot(explore_data, aes(x = year, y = perp, fill = country, 
                      label = paste0(perp,"%"))) +
  geom_bar(stat = "identity") +
  geom_text(size = 2, position = position_stack(vjust = 0.5)) +
  labs(x = "Years", y = "Percentage of the population with respect to the continent") +
  theme(legend.text=element_text(size=14))

Hope it helps you

I couldn't find what I want, the best I've done is to limit the axis y

This I the code:

p1 <- explore_data %>%
ggplot(aes(nosis_worst_situation, fill=target)) +
ggtitle("\n Histogram") +
theme(plot.title = element_text(hjust = 0.5),
legend.position="none") +
geom_bar(stat='count', position = 'stack') +
geom_text(aes(label=..count..),
stat='count',
size = 2) +
facet_grid(is_for_train~.) +
scale_x_continuous(breaks = c(0,1,2,3,4,5))

p2 <- explore_data %>%
ggplot(aes(x=nosis_worst_situation, fill=target)) +
facet_grid(is_for_train~.) +
ggtitle("\n Percent Stacked Bar Chart") +
theme(plot.title = element_text(hjust = 0.5),
legend.text=element_text(size=14)) +
geom_bar(stat='count', position = 'fill') +

scale_y_continuous(labels = scales::percent, limits = c(0, 0.3)) + 
scale_x_continuous(breaks = c(0,1,2,3,4,5))

options(repr.plot.width=12, repr.plot.height=4)
grid.arrange(p1, p2, nrow = 1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.