我想画一个图,其中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()

此外,使用ggplotly时,当我将y轴设置为百分比时,当我将鼠标放在图形的某个点上时,值不是百分比:

发布于 2017-03-21 04:35:11
我知道你问了很久,但你可以在scale_y_continuous()中使用limits,如下所示:
scale_y_continuous(labels = scales::percent, limits=c(0,1))
发布于 2018-12-03 00:35:13
建议对上面的回复进行较小的修改:
在将值设置为百分比之前,您似乎必须在scale_y_continuous调用中指定限制:
scale_y_continuous(limits=c(0,1), labels = scales::percent)
发布于 2016-05-23 01:52:54
由于您尚未给出数据集,因此我将尽我最大的努力进行猜测。
您需要在scale_y_continuous中提供limits选项。如您所见,ylim不会覆盖由scale_y_continuous设置的美学。你需要使用一个函数来改变y轴的美学。使用ylim或scale_y_continuous。
https://stackoverflow.com/questions/37375768
复制相似问题