readdata <- read.csv(file="rawdata.csv", header = TRUE, stringsAsFactors = FALSE)
readdata
Month Percentage
1 01 09 2015 0.13
2 01 10 2015 0.14
3 01 11 2015 0.14
4 01 12 2015 0.13
5 01 01 2016 0.13
6 01 02 2016 0.13
7 01 03 2016 0.13
8 01 04 2016 0.14
9 01 05 2016 0.14
10 01 06 2016 0.14
11 01 07 2016 0.14
12 01 08 2016 0.15
13 01 09 2016 0.16
我希望绘制一个简单的折线图,其中所有日期(月份)格式为“mmm-yy”,值为百分比。
我的看法
library(plotly)
library(lubridate)
finaldata <- dmy(readdata$Month)
plot_ly(x=finaldata, y=Percentage, name ="Test", mode = "lines+markers", text = paste(Percentage, "%", sep = ""), hoverinfo = "text")
这会产生
但是,每个月都看不到?我希望x轴为9月15日,10月15日,11月15日,12月15日......等等。
如果我添加%>% layout(xaxis = list(type = "category"))
。 x轴绘制为
P.S:我想要一个我计划在Shiny仪表板中使用的折线图。如果需要,我可以更改rawdata.csv
的格式。
答案 0 :(得分:0)
放大时会出现丢失的“Oct 2015”。它在原始缩放级别中不可见,因为plotly的设计者选择了默认的视觉混乱或“忙碌”级别。设计师的决定往往非常好。即使你不同意,反对软件默认设置可能会使未来的开发变得比它需要的更难,或者在更新API时更容易破坏你的代码。
如果确实希望每个月都在最外面的缩放级别,您可以这样做:
dates_with_months <- format(finaldata,"%Y-%m")
plot_ly(x=dates_with_months,
y=Percentage, name ="Test",
mode = "lines+markers",
text = paste(Percentage, "%", sep = ""), hoverinfo = "text")
请注意,这仅适用于等间距数据。