我想绘制一个图表,其中 y 轴以百分比表示:
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()
现在我想将 y 轴上限值设置为 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()
但结果与上图相同,y 轴限制相同。 当我不将 y 轴设置为百分比时它会起作用:
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()
此外,当我将 y 轴设置为百分比时,使用 ggplotly 当我将鼠标放在图表的某个点上时,该值不是百分比:
最佳答案
我知道你问这个问题已经有一段时间了,但是你可以在 scale_y_continuous()
中使用 limits
,如下所示:
scale_y_continuous(标签=尺度::百分比,限制=c(0,1))
关于r - ggplot、ggplotly、scale_y_连续、ylim 和百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37375768/