Shiny / R:线图上的因素太多

时间:2016-05-19 15:29:38

标签: r ggplot2 shiny

我正在使用ggplot2和gl来创建图形,但是x轴上出现的因素太多了。

enter image description here

  output$housePlot <- renderPlot({
  ggplot(data=houseratio, aes(x=Year, y=Ratio, group=Region, colour=Region)) +
  geom_line() +
  geom_point()
  })

我已经尝试过阅读post,但我无法获得seq()。我的数据格式很长,如下所示:

Year    Ratio   Region
1983 Q1 2.9 Northern
1983 Q2 3   Northern
1983 Q3 3.1 Northern
1983 Q4 3   Northern
...
2015 Q2 5.1 UK
2015 Q3 5.1 UK
2015 Q4 5.2 UK
2016 Q1 5.2 UK

使用此代码:

output$housePlot <- renderPlot({
ggplot(data=houseratio, aes(x=Year, y=Ratio, group=Region, colour=Region)) +
scale_x_discrete(breaks = seq(1, 1864, by = 4)) +
geom_line() +
geom_point()
})

所有因素都消失了!

enter image description here

我只需要每年展示,而不是每个季度。有什么建议?

(感谢)

1 个答案:

答案 0 :(得分:1)

最快的路线最终是制作“年”。数字类型。这需要一些转换:

library("zoo")
library("dplyr")

houseratio <- houseratio %>% mutate(Year = Year %>% as.character() %>% 
                                    as.yearqtr() %>% as.numeric())