gpt4 book ai didi

r - ggplot、ggplotly、scale_y_连续、ylim 和百分比

转载 作者:行者123 更新时间:2023-12-02 09:15:47 35 4
gpt4 key购买 nike

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

enter image description here

现在我想将 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()

enter image description here

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

enter image description here

最佳答案

我知道你问这个问题已经有一段时间了,但是你可以在 scale_y_continuous() 中使用 limits,如下所示:

scale_y_continuous(标签=尺度::百分比,限制=c(0,1))

关于r - ggplot、ggplotly、scale_y_连续、ylim 和百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37375768/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com