0

I ran the codes as below:

ggplot(data, aes(fill=bolumtam, y=Languagevalue/tapply(Languagevalue, Language ,sum)[Language], x=Language)) + 
    geom_bar(aes( y=Languagevalue/tapply(Languagevalue, Language ,sum)[Language]), position="dodge", stat="identity")+
 geom_text(aes( y=Languagevalue/tapply(Languagevalue, Language ,sum)[Language], label=scales::percent(Languagevalue/tapply(Languagevalue, Language ,sum)[Language]) ),
            stat="identity", position=position_dodge(0.9), vjust=-0.5)+
 scale_y_continuous(labels = scales::percent)

produces: enter image description here

but I want to produce a graph with patterns and percent labels similar to this:

enter image description here

Some fake random data:

set.seed(100)
Language <- sample(c("Language 1", "Language 2", "Language 3", "Language 4"), 100, TRUE)
bolumtam <- sample(c("Associate", "Natural Sciences", "Social Sciences"), 100, TRUE)
Languagevalue <- sample(c(0, 1, 2, 3, 4, 5), 100, TRUE)

data <- data.frame(Language, bolumtam, Languagevalue)
stefan
  • 90,330
  • 6
  • 25
  • 51
Rira A
  • 1
  • 1
  • It would be eaiser to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. If you want to post your data type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 10))`. – stefan Feb 18 '22 at 19:52
  • This said: Doing all the computations of counts and percentages inside ggplot2 is most of the time the most complicated and error-prone way. Instead I would suggest to aggregate your data before passing it to ggplot similar to [Adding percentage labels to a bar chart in ggplot2](https://stackoverflow.com/questions/40249943/adding-percentage-labels-to-a-bar-chart-in-ggplot2) – stefan Feb 18 '22 at 19:54
  • Thank you stefan for reply. here is a reproducible example. – Rira A Feb 20 '22 at 12:20
  • `set.seed(100)` `Language <- sample(c("Language 1", "Language 2", "Language 3", "Language 4"),100, TRUE)` `bolumtam <- sample(c("Associate", "Natural Sciences", "Social Sciences"),100, TRUE)` `Languagevalue <- sample(c(0,1,2,3,4,5),100, TRUE)` `data <- data.frame(Language, bolumtam,Languagevalue)` – Rira A Feb 20 '22 at 12:24
  • Could you help me how to use patterns instead of colors and percents only for total bars? – Rira A Feb 20 '22 at 12:26

1 Answers1

0

As I mentioned in my comment doing all the computations of counts and percentages inside ggplot2 is most of the time the most complicated and error-prone way. Instead I would suggest to aggregate your data before passing it to ggplot. Personally I would not recommend to use patterns but if you could do so using the ggpattern package:

library(ggplot2)
library(ggpattern)
library(dplyr)

data1 <- data %>%
  group_by(Language, bolumtam) %>%
  summarise(Languagevalue = sum(Languagevalue), .groups = "drop") %>%
  mutate(pct = Languagevalue / sum(Languagevalue))

ggplot(data1, aes(y = pct, x = Language)) +
  geom_col_pattern(
    aes(pattern = bolumtam, pattern_angle = bolumtam, pattern_spacing = bolumtam), 
    pattern_fill = 'black',
    fill    = 'white',
    colour  = 'black', 
    position = "dodge"
  ) +
  geom_text(aes(label = scales::percent(pct), group = bolumtam), position = position_dodge(0.9), vjust = -0.5) +
  scale_y_continuous(labels = scales::percent) +
  scale_pattern_spacing_discrete(range = c(.01, .03))

As an alternative to patterns and colors you could use a grey fill scale:

ggplot(data1, aes(y = pct, x = Language, fill = bolumtam)) +
  geom_col(
    position = "dodge"
  ) +
  geom_text(aes(label = scales::percent(pct), group = bolumtam), position = position_dodge(0.9), vjust = -0.5) +
  scale_y_continuous(labels = scales::percent) +
  scale_fill_grey()

stefan
  • 90,330
  • 6
  • 25
  • 51