Changing values to percentages

status1 <- c("No Phonecall", "Phonecall")
> no1 <- c(444, 176)
> yes1 <- c(110, 24)
> tata <- data.frame(status1, no1, yes1)

below is an example of a dataframe..essentially it is the number of patients that need to be seen in a practice after an intervention (phonecall). One cohort (the larger one) was our comparator (no phonecall took place) and the second cohort (the smaller one),our intevention (a phonecall to patient) was invovled.

I can get the graphs I require based on the folllowing code

tata %>%
  gather(key = "success", value = value, -status1) %>%
  ggplot(aes(y = value, x = status1, fill = success)) + 
  geom_bar(position = "dodge", stat = "identity") + scale_fill_manual(values = c("green", "red"))

However, I would much rather the Y axis was displayed in percentages rather than the actual value, as this would clearly be more meaningful. Can anyone help please?

Thanks,
S

Do you want the percentages calculated withing each group, as in the following code?

status1 <- c("No Phonecall", "Phonecall")
no1 <- c(444, 176)
yes1 <- c(110, 24)
tata <- data.frame(status1, no1, yes1)
library(tidyr)
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
library(dplyr)
#> 
#> 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
tata %>%
  gather(key = "success", value = value, -status1) %>%
  group_by(status1) %>% 
  mutate(value = value/sum(value)*100) %>% 
  ggplot(aes(y = value, x = status1, fill = success)) + 
  geom_bar(position = "dodge", stat = "identity") + labs(y = "percent") + 
  scale_fill_manual(values = c("green", "red"))

Created on 2021-08-25 by the reprex package (v0.3.0)

Perfect

However, I need few more things if that's ok

  1. increase the font size of the tick values on the Y axis and .i.e. the percentage

  2. can the space between the paired graphs be increased..i.e. between 'no phone call' and 'phone call' ..just to make it look neater

  3. can the 'Yes' be grouped together rather than the 'phonecall' Vs 'nophonecall' as on second thoughts, it appears to better represent the difference caused by the intervention (i.e. 12% Vs 20% next to each other).

Thanks again

suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
  library(tidyr)
})

status1 <- c("No Phonecall", "Phonecall")
no1 <- c(444, 176)
yes1 <- c(110, 24)
tata <- data.frame(status1, no1, yes1)


tata %>%
  gather(key = "success", value = value, -status1) %>%
  group_by(status1) %>% 
  mutate(value = value/sum(value)*100) %>% 
  ggplot(aes(y = value, x = status1, fill = success)) + 
  geom_bar(position = "dodge", stat = "identity") + 
  labs(y = "Response") + 
  scale_y_continuous(labels = scales::label_percent(scale = 1, accuracy = 1)) +
  scale_fill_manual(values = c("darksalmon", "palegreen")) + theme_minimal()

Many thanks,

Any chance you can introduce more Y axis ticks ? I want it to go to till 100% as one of the values of > 75% ? I'm not able to do this for some reason using coord_cartesian

Thanks

Try using the limits argument of scale_y_continuous().

tata %>%
  gather(key = "success", value = value, -status1) %>%
  group_by(status1) %>% 
  mutate(value = value/sum(value)*100) %>% 
  ggplot(aes(y = value, x = status1, fill = success)) + 
  geom_bar(position = "dodge", stat = "identity") + 
  labs(y = "Response") + 
  scale_y_continuous(labels = scales::label_percent(scale = 1, accuracy = 1), 
                     limits = c(0,100)) +
  scale_fill_manual(values = c("darksalmon", "palegreen")) + theme_minimal()
1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.