闪亮/ R:线图上的因素太多 [英] Shiny/R: Too many factors on line graph

查看:190
本文介绍了闪亮/ R:线图上的因素太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

我试过阅读



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



(谢谢) 年是数字类型。这需要几次转换:

  library(zoo)
library(dplyr)

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


I'm using ggplot2 and shiny to create a graph, however there are too many factors appearing on the x axis.

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

I've tried reading this post but I can't get the seq() right. My data is in long format, looks like this:

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

Using this code:

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()
})

All the factors disappear!

I only need each year showing, not individual quarters. Any suggestions?

(Thanks)

解决方案

The fastest route is ultimately to make 'Year' a numeric type. This requires a few conversions:

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

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

这篇关于闪亮/ R:线图上的因素太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆