PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How can I unify the number of decimal places in ggplot?

 August 11, 2022     decimal, ggplot2, r     No comments   

Issue

I have a more complex code compared to the examples provided and cannot imagine how I have to change the code to obtain the same number of decimal places for all bars of my bar chart. For most of the bars, R already shows two decimal places. Whenever the second decimal place is a "0", however, R only shows one decimal place. Can someone please show me how to standardize the decimal place to 2 and how to integrate the command in my code?

I already tried "digits" and specify_decimal but failed to correctly integrate it into my code (in scale_x_continuous).

df1_long %.>%
  ggplot(
    data = .,
    aes(
      x = xpos,
      y = val,
      fill = Ebene
  )) +
  geom_rect(aes(
      xmin = xpos - .5,
      xmax = xpos + .5,
      ymin = -Inf,
      ymax = Inf,
      fill = Problemzahl
    ),
    alpha = .3
  ) +
  geom_col(
    position = position_dodge(.5),
    colour="black",
    width = .5
  ) +
  geom_errorbar(aes(
      ymin = val - SE,
      ymax = val + SE
    ),
    position = position_dodge(.5),
    width = .2
  ) +
  geom_text(aes(
      y = val + SE + .02,
      label = val %>% str_replace('\\.', ',')
    ),
    position = position_dodge(.5)
  ) +
  scale_fill_manual(
    values = c(
      '#aaaaaa',  # Beide Problemarten
      '#000000',  # Co
      '#dddddd',  # Keine Probleme
      '#CCCCCC',  # Motivationale Probleme
      '#333333',  # Self
      '#666666',  # Shared
      '#bbbbbb'   # Verständnisbezogene Probleme  
    ),
    breaks = c(
      'Self',
      'Co',
      'Shared'
    )
  ) +
  scale_x_continuous(
    breaks = .$xpos %>% unique(),
    labels = .$Problemzahl %>% unique(),
    expand = c(0, 0)
  ) +
  scale_y_continuous(
    labels = function(x) str_replace(x, '\\.', ','),
    limits = c(0, 1.0),
    expand = c(0, 0)
  ) +
  ylab('Allgemeine Regulationsaktivität auf der Self-, Co-, und Shared Ebene\n(KI 95%)') +
  theme_classic() +
  theme(
    legend.position = 'top',
    legend.title = element_blank(),
    legend.spacing.x = unit(1, 'mm'),
    axis.title.x = element_blank()
  )

Solution

I would alter the code in your pipe before calling ggplot. Here's a nice example using the formattable package.

library(tidyverse)
library(formattable)

df1_long %>%
  mutate(val = formattable(val, digits = 2, format = "f")) %>%
  ggplot(
  ...
)

You can substitute whatever variable name for val, depending on which one you want to standardize to two decimal places.



Answered By - Trent
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing