2017-02-22 23 views
1
的简单例子

我有一个伟大的时间去学习R,但我已经有一个东西在我Plotly图挣扎..我怎样才能截距在plotly设置到零问题

下面是这是我遇到的问题的一个简单例子。基本上情节是在我的第二个数据点“3.5%”设置y轴截距。我想拦截到位于y穿越= 0,这样我的两个值的并排显示的柱状图侧

library(plotly) 

x <- c("Unlevered", "Levered") 
y <- c("4.5%","3.5%") 
summary_plot <- data.frame(x, y) 
plot_ly(data = summary_plot, x = ~x, y = ~y, type = "bar") 

这里是我的阴谋目前的样子: Example Plot

回答

0

试着这么做:

s <- seq(0, 4.5) 
p <- plot_ly(x = ~x, y = ~s) 
+0

谢谢Jaap,纠正格式。 – satan

0

通过relevel试试这个在y -ing您factor值:

library(plotly) 
x <- c("Unlevered", "Levered") 
y <- c("4.5%","3.5%") 
summary_plot <- data.frame(x, y) 
levels(summary_plot$y) <- c(levels(summary_plot$y), '0.0%') 
summary_plot$y <- factor(summary_plot$y, levels = sort(levels(summary_plot$y))) 
plot_ly(data = summary_plot, x = ~x, y = ~y, type = "bar") 

enter image description here

+0

嗯。我将上面的代码复制到RStudio中的新脚本中,并且我的图形看起来不同。我得到了3.5%的条子,然后是4.5%的条子。我的图表上没有显示0.0%的坐标轴。我对这个练级中发生的事情有些困惑。 不应该积极智慧,并找出0应该是图表的底部?我想我很惊讶,像这样的一个简单的图表导致我这么多问题 –

+0

我意识到我的问题是我的data.frame已经有'%'符号附加。我从代码中删除了它们,这样我的data.frame就是数字。 有谁知道如何格式化Y轴的百分比?! –