I would like to plot a graph where the y axis is in percentage:
p = ggplot(test, aes(x=creation_date, y=value, color=type)) +
geom_line(aes(group=type)) +
scale_colour_manual(values=c("breach"="red","within_promise"="green","before_promise"="blue")) +
geom_vline(xintercept=c(as.numeric(as.Date('2016-05-14'))),linetype="dotted") +
scale_y_continuous(labels=percent)
ggplotly()
Now I would like to set the y axis superior limit to be 100%
p = ggplot(test, aes(x=creation_date, y=value, color=type)) +
geom_line(aes(group=type)) +
scale_colour_manual(values=c("breach"="red","within_promise"="green","before_promise"="blue")) +
geom_vline(xintercept=c(as.numeric(as.Date('2016-05-14'))),linetype="dotted") +
scale_y_continuous(labels=percent) +
ylim(0, 1)
ggplotly()
But result is the same as the previous plot, the y axis limits are the same. It works when I don't put the y axis to be in percent:
p = ggplot(test, aes(x=creation_date, y=value, color=type)) +
geom_line(aes(group=type)) +
scale_colour_manual(values=c("breach"="red","within_promise"="green","before_promise"="blue")) +
geom_vline(xintercept=c(as.numeric(as.Date('2016-05-14'))),linetype="dotted") +
ylim(0, 1)
ggplotly()
Moreover using ggplotly when I set the y axis to be in percent when I put my mouse on a point of the graph the value is not in percent:
Since we need to add percentages in the labels of the Y-axis, the keyword “labels” is used. Now use scales: : percent to convert the y-axis labels into a percentage. This will scale the y-axis data from decimal to percentage. It simply multiplies the value by 100.
You can use the scale_y_continuous() function in ggplot2 to customize the y-axis of a given plot. This function uses the following basic syntax: p + scale_y_continuous(breaks, n.
scale_x_continuous() and scale_y_continuous() are the default scales for continuous x and y aesthetics. There are three variants that set the trans argument for commonly used transformations: scale_*_log10() , scale_*_sqrt() and scale_*_reverse() .
scale_x_log10() and scale_x_log10() are shortcuts for the base-10 logarithmic transformation of an axis. The same could be achieved by using, e.g., scale_x_continuous(trans = "log10") . The latter can take a selection of options, namely "reverse" , "log2" , or "sqrt" .
I'm aware it's been a whle since you asked, but you could use limits
inside scale_y_continuous()
, like this:
scale_y_continuous(labels = scales::percent, limits=c(0,1))
Minor suggested edit to the response above:
It seems that you have to specify the limits within the scale_y_continuous
call prior to setting the values as percentages:
scale_y_continuous(limits=c(0,1), labels = scales::percent)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With