Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force y axis to 100% in ggplot2

Tags:

r

ggplot2

I have a data frame that looks like this:

df <- data.frame(
group = c(rep("A", 4),rep("B", 4), rep("C", 4)),
word = c(rep(c("first", "first", "second", "second"), 3)), 
emphasis = c(rep(c("normal", "emphatic"), 6)), 
percentage = c(.175, .07, .13, .04, .60, .43, .21, .28, .63, .63, .40, .29))

I want to create a line graph that looks pretty much like this:

library(ggplot2)
p <- ggplot(df, aes(x = group, y = percentage, group = emphasis, col = emphasis))
p + geom_line() + facet_wrap(~ word)+ scale_y_continuous(label = percent) + geom_point(size=4, shape=21, colour="black") 

I can't quite figure out how to get the y scale to 100%, since I don't have a data point that reaches that high. I thought scale_y_continuous(limits = c(0, 100) would do the trick, but it doesn't. I imagine it must be easy, but I can't find any examples of how to do it.

like image 705
JoeF Avatar asked Jan 08 '23 07:01

JoeF


1 Answers

This will give you the full range from 0% to 100% with percent labels:

library(scales) # For the percent_format() function

scale_y_continuous(labels = percent_format(), limits=c(0,1))
like image 52
eipi10 Avatar answered Jan 16 '23 02:01

eipi10