Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot, ggplotly, scale_y_continuous, ylim and percentage

Tags:

r

ggplot2

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()

enter image description here

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()

enter image description here

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:

enter image description here

like image 964
user1836529 Avatar asked May 22 '16 14:05

user1836529


People also ask

How do I change y-axis to percentage in Ggplot?

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.

What is scale_y_continuous in R?

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.

What does Scale_x_continuous mean?

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() .

What does scale_x_log10 do in R?

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" .


2 Answers

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))

like image 71
dudu Avatar answered Oct 20 '22 23:10

dudu


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)

like image 29
MH765 Avatar answered Oct 20 '22 23:10

MH765