首先,本人无任何编程经验,纯自学R语言,所以文章有不足之处还请指出。
1.用到几个包:library(RJDBC);library(highcharter)
2.最近领导对可视化方面有所要求,绘图过程中有学习到许多优秀的包,本人比较喜欢highcharter包绘图,今天举个简单的例子,图如下:
3.实现过程:
3.1 首先用RJDBC连接数据库取数
我用的oracle数据库,取数代码如下
drv <- JDBC("oracle.jdbc.driver.OracleDriver","C://Users//Administrator//Desktop//Rshiny_centos//ojdbc14-10.2.0.2.0.jar","`")
conn <- dbConnect(drv,"jdbc:oracle:thin:@192.168.*.*:****:**","用户名","密码")
data<-dbSendQuery(conn,"select * from table")
data<-dbFetch(data)
图片用到复合y轴,故这边用到hc_yAxis_multiples,上面的图是spline样式,下面的图是areaspline样式,画图代码如下:
highchart() %>%
hc_title(text = 'title')%>% #图标标题
hc_xAxis(categories = data$month,title = list(text = "月份")) %>% #设置x轴数据及标题
hc_yAxis_multiples(
list(top = "0%", height = "30%", lineWidth = 1,
title = list(text = "y轴标题(上面)"),
labels = list(format = "{value}%", rotation = 30 )
),
#设置上半部分y轴坐标属性,高度30%,线宽1,标签为%格式,标签倾斜30度
list(top = "30%", height = "70%",showFirstLabel = T,
showLastLabel = F, offset = 0,
title = list(text = "y轴标题(下面)"),
labels = list(format = "{value}")) #设置下半部分y轴坐标属性,顶端从30%的高度开始,整体高度70%,不显示第一个标签和最后一个标签
) %>% hc_add_series(name='colname1', data = data$colname1, type = "spline", dataLabels = list(enabled = F, color='skyblue')) %>%
data = data$colname2,
type = "areaspline", color = "steelblue", yAxis = 1, dataLabels = list(enabled = F, color='yellow')) %>% hc_add_series(name='colname3', data = data$colname3, type = "areaspline", color = "turquoise", yAxis = 1, dataLabels = list(enabled = F, color='yellow')) %>% hc_legend(enabled = T,align = "right", verticalAlign = "top",padding = 20 )
当然,绘图的过程中我也遇到很多细节问题,欢迎讨论。