I am trying to force my y axis to be 0 - 100 as it is showing percentage. The code below gives me ay axis of 0-15 but when i add "+ scale_y_continuous(labels=percent)" which i thought was the solution it comes up with '0% - 1500%'? Can anyone help? Thanks
april_graph <- ggplot(data, aes(fill=year, y=value, x=treatment)) +
geom_bar(position="dodge", stat="identity") + scale_fill_discrete(breaks=c('2016', '2017',
'2018', '2022')) +
xlab('Treatment Type') +
ylab('Species Richness (%)') + theme_classic() +
labs(fill = 'Year of Survey')
april_graph
The percent
function you use ( labels = percent
) expects inputs to be between 0 and 1: 33% is the same as 0.33, not 33. You can divide by 100 inside aes
, y = value / 100
and then use
scale_y_continuous(labels = percent, limits = c(0, 1))
(Instead of value / 100
, if you calculated the percents you very likely multiplied by 100 earlier in your code, and you can omit that.)
In setting limits like that, I usually like to remove the padding:
scale_y_continuous(
labels = percent,
limits = c(0, 1),
expand = expansion(0, 0)
)
If you want more flexibility in the percent formatting, use the more fully featured scale::label_percent
function. It's help page has nice example. You can customize how many digits of accuracy to show, whether or not to multiply by 100, etc. Something like below (with y = value
, not y = value / 100
scale_y_continuous(
labels = scales::label_percent(accuracy = .1, scale = 1),
limits = c(0, 100),
expand = expansion(0, 0)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.