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

标签 r ggplot2

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

相关文章:

r - ggplot2:如何使用 geom_jitter 和 geom_point 控制点颜色?

r - R 包文档中的短 url

r - gganimate 循环之间的暂停

r - 为什么连续数据的一些 fiddle 图看起来呈波浪状?

r - 传单未在动态生成的 R markdown html knitr 中呈现

r - 在 R 中计算购买旅程长度

r - 在 ggplot 中向 barplot 添加图例

r - 轴标签中的绝对值

r - 更改 R 中饼图中切片的顺序

r - 将空条添加到(百分比)条形图(从长数据格式生成)