nesta1990
nesta1990

Reputation: 295

Adding a % symbol on the y-axis (ggplot2)

I am trying to display a % on the y-axis in a bar graph in ggplot2.

Here is a data example:

# data example
dput(head(user_patterns,5))

output:

structure(list(socio_egotropic = c("egotropic", "egotropic", 
"egotropic", "sociotropic", "sociotropic"), active = c("both", 
"post-treatment", "pre-treatment", "both", "post-treatment"), 
    n = c(155L, 55L, 225L, 236L, 32L), percentage = c(35.632183908046, 
    12.6436781609195, 51.7241379310345, 37.4009508716323, 5.07131537242472
    )), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L), groups = structure(list(socio_egotropic = c("egotropic", 
"sociotropic"), .rows = structure(list(1:3, 4:5), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))

Currently, my graph and code look as follows:

ggplot(user_patterns, aes(active, percentage, fill = socio_egotropic)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  theme_bw()                         

output: enter image description here

I have tried tweaking the code as below to add a % symbol and adjust the breaks in the y-axis, but I keep receiving the error message below:

ggplot(user_patterns, aes(active, percentage, fill = socio_egotropic)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
        scale_y_continuous(labels = percent_format(accuracy = 1)) +
    scale_y_continuous(breaks = seq(0, .85, .10),
                      labels = scales::percent,
                      limits = c(0, .85)) + 
     scale_x_discrete(limits = c("both", "post-treatment", "pre-treatment")) +
   theme_bw()

Error message: "Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale. Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale."

Updated post: There seems to be an issue with the code once I use a multinomial rather than binomial X-variable. The code and graph below work well, but not when I use more than two groups for the x-variable.

ggplot(data = user_patterns, aes(x = treatment_implementation, fill = socio_egotropic, y = socio_egotropic_percentage)) + 
    geom_bar(stat = "identity", position=position_dodge()) + 
    scale_fill_grey() +
    xlab("Sociotropic vs. Egotropic experiences relative to implementation") + 
    ylab("Sociotropic vs. Egotropic share by period") + 
    theme(text=element_text(size=20)) + 
  scale_fill_manual(values = c("sociotropic" = "purple",
                               "egotropic" = "blue")) +
    theme(plot.title = element_text(size = 18, face = "bold")) +
      scale_y_continuous(labels = percent_format(accuracy = 1)) +
   scale_y_continuous(breaks = seq(0, .85, .10),
                     labels = scales::percent,
                     limits = c(0, .85)) + 
    scale_x_discrete(limits = c("pre", "post")) +
  theme_bw()

output: enter image description here

Upvotes: 2

Views: 1056

Answers (1)

Nimzo
Nimzo

Reputation: 492

Maybe you need to simply change the y axis title (function ylab)

ggplot(user_patterns, aes(active, percentage, fill = socio_egotropic)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  theme_bw() +
  ylab("percentage %")

Upvotes: 1

Related Questions