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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.