赞
踩
par(mfrow=c(2,2))
barplot(counts,main=“Simple Bar Plot”,xlab=“Improvement”,ylab=“Frequency”)
barplot(counts,main=“Horizontal Bar Plot”,xlab=“Frequency”,ylab=“Improvement”,horiz=TRUE)//把上一个图颠倒
counts=table(Arthritis
I
m
p
r
o
v
e
d
,
A
r
t
h
r
i
t
i
s
Improved,Arthritis
Improved,ArthritisTreatment)
barplot(counts,main=“Stacked Bar Plot”,xlab=“Treatment”,ylab=“Frequency”,col=c(“red”,“yellow”,“green”),
legend=rownames(counts)) //设置堆叠颜色
barplot(counts,main=“Grouped Bar Plot”,xlab=“Treatment”,ylab=“Frequency”,col=c(“red”,“yellow”,“green”),
legend=rownames(counts),beside=TRUE)
library(plotrix)
par(mfrow=c(2,2))
slices=c(10,12,4,16,8)
lbls=c(“US”,“UK”,“Australia”,“Germany”,“France”)
pie(slices,labels=lbls,main=“Simple Pie Chart”,edges=300,radius=1)
pct=round(slice/sum(slices)*100) //计算各部分比例
lbls=paste(lbls," “,pct,”%",sep=" “) //将列表里字段和对应比例串起来
pie(slices,labels=lbls2,col=rainbow(length(lbls2)), main=“Pie Chart with Percentage”, edges=300,radius=1)//edges越小越接近多边形
pie3D(slices,labels=lbls,explode=0.1,main=“3D Pie Chart”,edge=300,radius=1) //explode越大分的越开
mytable=table(state.region) //注意3D饼图需要“plotrix”包
lbls3=paste(names(mytable),”\n",mytable,sep=" ")
pie(mytable,labels=lbls3,main=“Pie Chart from a Table\n(with sample size)”,edges=300,radius=1)
slices=c(10,12,4,16,8)
lbls=c(“US”,“UK”,“Australia”,“Germany”,“France”)
fan.plot(slices,labels=lbls,main=“Fan Plot”)
dotchart(mtcars$mpg,labels=row.names(mtcars),cex=0.7,main=“Gas Mileage for Car Models”,xlab=“Miles per Gallon”)
type <- c("速度", "力道", "耐力", "精神力","技术") actual <- c(3,3,3,4,5) plan <- c(4,4,4,5,8) highchart() %>% #polar设置极坐标 hc_chart(polar = TRUE,type = "line") %>% hc_title(text = "网球王子五维数据",x=-60) %>% #设置图形大小 hc_pane(size = "80%") %>% #设置图例位置 hc_legend(align = "right",verticalAlign = "top",y = 70,layout = "vertical") %>% hc_xAxis(categories = type, #横轴线宽设为0,隐藏横轴 lineWidth = 0, #旋转图表分割线至多边形的顶点 tickmarkPlacement = "on") %>% hc_yAxis(#设置图形为多边形 gridLineInterpolation = "polygon", #纵轴线宽设为0,隐藏纵轴 lineWidth = 0, min = 0) %>% hc_tooltip(#提示框同时显示两条线的同一个类别下的值 shared = TRUE, #提示框格式 pointFormat = '<span style="color:{series.color}">{series.name}: <b>${point.y:,.0f}</b><br/>')%>% hc_add_series(name = "不二周助",actual) %>% hc_add_series(name = "手冢国光",plan) %>% hc_add_theme(hc_theme_google())
rose_data <- melt(citytemp,"month") # melt将数据变形为一条条单独数据 rose_data[,rate:= round(ifelse(value>= 0,value,0)/ sum(ifelse(value>= 0,value,0))*100,2)] highchart() %>% #polar设置极坐标 hc_chart(polar = TRUE,type = "column") %>% hc_title(text = "各城市数据分布",x=-50) %>% #设置图形大小 hc_pane(size = "85%") %>% #设置图例位置 hc_legend(align = "right",verticalAlign = "top",y = 100,layout = "vertical") %>% hc_yAxis(title = list(text = "占比 (%)", #设置轴标题的位置 x= 0, y=-30), #颠倒堆积的顺序 reversedStacks = FALSE) %>% hc_xAxis(categories = unique(rose_data$month))%>% hc_tooltip( #提示框的值显示百分比 valueSuffix = "%") %>% hc_plotOptions(series = list(stacking = "normal",pointPlacement= "on",groupPadding = 0,shadow = FALSE))%>% hc_add_series(data=rose_data,type = "column",hcaes(y=value,group = variable)) %>% hc_add_theme(hc_theme_google())
m <- c(1746181,1884428,2089758,2222362,2537431,2507081,2443179,2664537,3556505,3680231, 3143062 ,2721122, 2229181 ,2227768, 2176300, 1329968 , 836804,354784,90569,28367,3878) f <- c(1656154, 1787564, 1981671, 2108575, 2403438, 2366003, 2301402, 2519874,3360596, 3493473, 3050775, 2759560, 2304444, 2426504, 2568938, 1785638,1447162, 1005011, 330870, 130632, 21208) class <- c('0-4', '5-9', '10-14', '15-19','20-24', '25-29', '30-34', '35-39', '40-44','45-49', '50-54','55-59', '60-64', '65-69', '70-74', '75-79', '80-84', '85-89', '90-94','95-99', '100 + ') highchart() %>% hc_xAxis( list( categories = class, #颠倒坐标轴 reversed = FALSE, labels = list(step = 1)), list( #调整坐标轴位置至右侧 opposite = TRUE, categories = class, reversed = FALSE, #关联第一个坐标轴 linkedTo = 0, labels = list(step = 1)) ) %>% hc_plotOptions(series = list(#图内柱子边界线宽,设置为0,取消显示边界线 borderWidth = 0))%>% hc_yAxis( #坐标轴显示值的格式设置,负值取绝对值 labels = list(formatter = JS("function () { return (Math.abs(this.value) / 1000000) + 'M';}")), #设置坐标轴范围 min = -4000000,max = 4000000)%>% hc_tooltip(#设置提示框的格式 formatter = JS("function () { return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' + '人口: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0);}"))%>% hc_title(text = "2015年德国人口金字塔图",align="center")%>% hc_plotOptions(series= list( #设置堆叠柱状图,实现金字塔形状 stacking = "normal")) %>% hc_add_series(name = "男", #其中一类取负值,对称显示数据 data = -m, type = "bar") %>% hc_add_series(name = "女",data = f,type = "bar") %>% hc_add_theme(hc_theme_538())
套柱状图将两个柱子套在一起,展示两个值的相对大小,可用于展示比如订单量、营收等的同比周期或环比周期值的变化,表达一种“突破”的视觉效果。
city <- c("北京市","上海市","广州市","深圳市") Aug <- c(180,150,170,130) Jul <- c(200,135,185,100) highchart() %>% hc_title(text = "各城市月度营收 ( 百万)", align="center") %>% hc_xAxis(categories = city) %>% hc_plotOptions(column = list( #将两组值合并在一起,默认值是TRUE grouping = FALSE, #设置图的阴影,默认值是FALSE shadow = FALSE, #合并后柱子的边界宽度 borderWidth = 0)) %>% hc_add_series( #名称 name = "七月营收", data = Jul, type = "column", color = "rgba(165,170,217,1)", #合并后柱子的相对粗细,结合第二个“hc_add_series”中的该参数进行相对调整 pointPadding = 0.3, #轴值与柱子中心线的相对位置 pointPlacement = 0) %>% hc_add_series( name = "八月营收", data = Aug, type = "column", #设置透明度为0.9,可以稍微减少第一个柱子被遮挡的程度 color = "rgba(126,86,134,0.9)", pointPadding = 0.4, pointPlacement = 0) %>% hc_add_theme(hc_theme_flat())
柱形范围图用于展示范围变化的数,可直观的体现出组内范围和组间差异。
month <- c('一月','二月','三月','四月','五月', '六月','七月','八月','九月','十月','十一月','十二月') low_temp <- c(-9.7,-8.7,-3.5,-1.4,0,2.9,9.2,7.3,4.4,-3.1,-5.2,-13.5) high_temp <- c(9.4,6.5,9.4,19.9,22.6,29.5,30.7,26.5,18.0,11.4,10.4,9.8) month_temp <- data.table(month,low_temp,high_temp) highchart() %>% hc_chart( #旋转坐标轴 inverted = TRUE)%>% hc_title(text = "每月温度变化范围( °C )", align="center")%>% hc_xAxis(categories = month) %>% hc_yAxis(title = list(text = "温度 ( °C )")) %>% hc_plotOptions(columnrange = list(dataLabels = list(enabled = TRUE)))%>% hc_legend(enabled = FALSE)%>% hc_plotOptions(series = list(#图内柱子边界线宽,设置为0,取消显示边界线 borderWidth = 0))%>% hc_add_series(data = month_temp[,2:3], name = "温度", #图表类型 type = "columnrange", hcaes( #最低值 low = low_temp, #最高值 high = high_temp)) %>% hc_add_theme(hc_theme_ffx())
误差图通过误差条体现值的不确定性范围,误差条越短标明值的分布越集中,确定性越高,一般可在条形图、折线图、散点图中使用误差条。
dataset <- data.table( month = c('一月','二月','三月','四月','五月', '六月','七月','八月','九月','十月','十一月','十二月'), rain = c(49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4), low = c(48, 68, 92, 128, 140, 171, 135, 142, 204, 189, 95, 52), high = c(51, 73, 110, 136, 150, 179, 143, 149, 220, 199, 110, 56)) highchart()%>% hc_title(text="误差图",align="center")%>% hc_plotOptions(series = list(#图内柱子边界线宽,设置为0,取消显示边界线 borderWidth = 0)) %>% hc_legend(enabled = FALSE)%>% hc_xAxis(categories = dataset$month)%>% hc_yAxis(title = list(text= "降水"),labels = list(format = "{value} mm")) %>% hc_add_series(name = "降水",type = "column",data = dataset$rain)%>% hc_add_series(name = "降雨误差",type = "errorbar",data = dataset,hcaes(low = low,high = high))%>% hc_add_theme(hc_theme_flat())
explorer_rate <- data.table(name = c('Firefox','IE','Chrome','Safari','Opera','other'), rate = c(45, 26.8, 12.8, 8.5,6.2,0.7)) highchart() %>% hc_title( #图表标题,“<br>” 换行 text = "浏览器<br>份额占比", #标题位置,水平方向 align = "center", #标题位置,垂直方向 verticalAlign = "middle", #标题位置,基于上方位置,进行微调 y = 60) %>% hc_plotOptions(pie = list( dataLabels = list( #显示标签 enabled = TRUE, #标签显示位置调整 distance = -50, #标签的格式设置 style = list(fontWeight = "bold",color = "white")), #圆环的开始角度 startAngle = -90, #圆环的结束角度 endAngle = 90, center = c('50%','75%')))%>% hc_tooltip( #提示框格式显示 headerFormat = "{series.name}<br>", pointFormat = "{point.name}: <b>{point.percentage:.1f}%</b>")%>% hc_add_series(explorer_rate,type = "pie",hcaes(name = name, y = rate),name = "份额", #控制环的粗细 innerSize = "50%") %>% hc_add_theme(hc_theme_google())
explorer_rate <- data.table(name = c('Firefox','IE','Chrome','Safari','Opera','other'),
rate = c(45, 26.8, 12.8, 8.5,6.2,0.7))
highchart() %>%
hc_title(text = "浏览器<br>份额占比",align = "center",verticalAlign = "middle",y = -10) %>%
hc_tooltip(headerFormat = "{series.name}<br>",pointFormat = "{point.name}: <b>{point.percentage:.1f}%</b>")%>%
hc_plotOptions(pie = list(dataLabels = list(enabled = TRUE,distance = -50
,style = list(fontWeight = "bold",color = "white")),
#设置图形显示的位置
center = c('50%','50%')
))%>%
hc_add_series(explorer_rate, type = "pie", hcaes(name = name, y = rate), name = "份额",
#控制环的粗细,当为 0%时,就是饼图
innerSize = "50%") %>%
hc_add_theme(hc_theme_google())
当需要对多个对象的变化趋势进行比较时,我们会用到组合图,即将多个图作在同一个作图框内。
highchart() %>% hc_title(text = "组合图示例一", align="center") %>% hc_xAxis(categories = c(10:20)) %>% hc_yAxis_multiples( #创建第一个y轴 list( #控制图与上面单元间距 top = "0%", #控制图的高度 height = "30%", #y轴的线宽 lineWidth = 5), #创建第二个y轴 list( #第一个y轴的高度是30%,设置top = "32%",两张图留出2%的间距 top = "32%", #控制图的高度 height = "70%", #轴与图的间距,一般设置为0 offset = 0, #y轴的线宽 lineWidth = 5, #是否显示轴的开始值 showFirstLabel = TRUE #是否显示轴的结束值 ,showLastLabel = FALSE) ) %>% hc_add_series(data = round(rnorm(10),2), #指定数据显示的坐标轴,0 为第一个坐标轴 yAxis = 0) %>% hc_add_series(data = round(rexp(10),2), type = "spline", #指定数据显示的坐标轴,1 为第二个坐标轴 yAxis = 1) %>% hc_add_theme(hc_theme_538())
highchart() %>% hc_title(text = "组合图示例二", align="center") %>% hc_yAxis_multiples( #创建坐标轴 create_yaxis( #指定轴的个数 naxis = 3, #指定各个轴的线宽 lineWidth = 5 )) %>% hc_add_series(data = c(1,3,2)) %>% hc_add_series(data = c(20, 40, 10), yAxis = 1) %>% #在第3个坐标轴上放第一组数 hc_add_series(data = c(200, 400, 500), type = "column", yAxis = 2) %>% #在第3个坐标轴上放第二组数 hc_add_series(data = c(500, 300, 400), type = "column", yAxis = 2) %>% hc_add_theme(hc_theme_elementary())
词云图用于突出显示文本中高频关键词汇,展示文本的核心重点。
text_data <- data.table(text =c("巴西", "加拿大", "墨西哥", "美国", "葡萄牙", "法国", "西班牙", "英国", "南非", "安哥拉", "塞内加尔", "马里", "摩洛哥", "中国", "印度", "日本"),
weight =c(2, 3, 1, 1, 4, 5, 4, 3, 1, 1, 2, 3, 2, 6, 1, 1))
highchart() %>%
#hc_title(text = "词云图") %>%
hc_add_series(data = text_data,type = "wordcloud",name= "得分",hcaes(name = text,weight = weight)) %>%
hc_add_theme(hc_theme_flat())
以特殊高亮的形式显示特定区域。
name <- c("Alexander", "Marie", "Maximilian", "Sophia", "Lukas", "Maria", "Leon", "Anna", "Tim", "Laura") weekday <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") heatmap_data = data.table(name =rep(name,each = length(weekday)), weekday =rep(weekday,times = length(name)), sales = round(runif(length(weekday) * length(name),min=20,max=100),0)) highchart() %>% hc_title(text = "每周销售数据",align="center")%>% hc_xAxis(categories = name) %>% hc_yAxis(categories = weekday)%>% hc_colorAxis(min = 0,minColor = "#FFFFFF") %>% hc_legend(align = "right",layout = "vertical",margin = 0,verticalAlign = "top",y = 25,symbolHeight = 400) %>% hc_tooltip(formatter = JS("function () {return '<b>' + this.series.xAxis.categories[this.point.x] + '</b> sold <br><b>' + this.point.value + '</b> items on <br><b>' + this.series.yAxis.categories[this.point.y] + '</b>';}")) %>% hc_add_series(name = "Sales per employee",data = heatmap_data,type = "heatmap", hcaes(x = name,y = weekday,value = sales), dataLabels = list(enabled = TRUE)) %>% hc_add_theme(hc_theme_538())
waterfall_data <- data.table(name = c("启动资金","产品收入","服务收入","固定成本","可变成本","汇总"), y = c(120000,569000,231000,-342000,-233000,NA), isSUM = c(FALSE,FALSE,FALSE,FALSE,FALSE,TRUE), isIntermediateSum = c(FALSE,FALSE,FALSE,FALSE,FALSE,TRUE), color = c("#008FD5","#008FD5","#008FD5","#FF2700","#FF2700","#77AB43")) highchart() %>% hc_title(text = "瀑布图",align="center") %>% hc_xAxis(#坐标轴类型,设置后在hcaes中设置x即可 type = "category") %>% hc_yAxis(title = list(text = "USD")) %>% hc_legend(enabled = FALSE) %>% hc_plotOptions(series = list(#图内柱子边界线宽,设置为0,取消显示边界线 borderWidth = 0))%>% hc_tooltip(pointFormat = "<b>${point.y}</b> USD") %>% hc_add_series(data = waterfall_data,type = "waterfall", hcaes(y = y,x= name, #设置自动计算汇总项 isSUM = isSUM,isIntermediateSum =isIntermediateSum, color=color))%>% hc_add_theme(hc_theme_538())
漏斗图能够直观形象的展示数据的转化路径。
funnel_data <- data.table(type=c("访问商品","加入购物车","提交订单","点击支付","支付成功"),
pv = c(15654,4064,1987,976,451))
highchart() %>%
hc_title(text = "电商转化漏斗") %>%
hc_plotOptions(series = list(dataLabels = list(
format = "<b>{point.name}</b> ({point.y:,.0f})"),
#控制漏斗图的长宽
neckWidth = "25%",neckHeight = "25%"
))%>%
hc_legend(enabled = FALSE)%>%
hc_add_series(name ="电商",type = "funnel",data = funnel_data,hcaes(x= type,y = pv))%>%
hc_add_theme(hc_theme_elementary())
桑基图展现的是数据的流向,有起点和终点,连接起点和终点的曲线宽度表示数值的相对大小。
sankey_data <- data.table(from = c("巴西", "巴西", "巴西", "巴西", "加拿大", "加拿大", "加拿大", "墨西哥", "墨西哥", "墨西哥", "墨西哥", "美国", "美国", "美国", "美国", "葡萄牙", "葡萄牙", "葡萄牙", "葡萄牙", "法国", "法国", "法国", "法国", "法国", "西班牙", "西班牙", "西班牙", "英国", "英国", "英国", "英国", "南非", "南非", "南非", "安哥拉", "安哥拉", "安哥拉", "塞内加尔", "塞内加尔", "塞内加尔", "马里", "马里", "马里", "摩洛哥", "摩洛哥", "摩洛哥"),
to = c("葡萄牙", "法国", "西班牙", "英国", "葡萄牙", "法国", "英国", "葡萄牙", "法国", "西班牙", "英国", "葡萄牙", "法国", "西班牙", "英国", "安哥拉", "塞内加尔", "摩洛哥", "南非", "安哥拉", "塞内加尔", "马里", "摩洛哥", "南非", "塞内加尔", "摩洛哥", "南非", "安哥拉", "塞内加尔", "摩洛哥", "南非", "中国", "印度", "日本", "中国", "印度", "日本", "中国", "印度", "日本", "中国", "印度", "日本", "中国", "印度", "日本"),
weight = c(5, 1, 1, 1, 1, 5, 1, 1, 1, 5, 1, 1, 1, 1, 5, 2, 1, 1, 3, 1, 3, 3, 3, 1, 1, 3, 1, 1, 1, 2, 7, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3))
highchart() %>%
hc_title(text = "桑基图") %>%
hc_add_series(data = sankey_data,type = "sankey",hcaes(from = from,to = to,weight = weight)) %>%
hc_add_theme(hc_theme_google())
帕累托图是将出现的问题或改进项目按照重要程度依次排列而采用的一种图表,是二八法则的图形体现,用于分析问题或项目的核心影响因素。帕累托图有条形图和折线图组合而成。
type = c("价格过高", "分量过小", "等待时间过长", "食物难吃", "氛围不好", "不卫生", "太吵", "服务态度不好") num = c(755, 222, 151, 86, 72, 51, 36, 10) rate = round(cumsum(num) / sum(num) *100,2) highchart() %>% hc_title(text = "餐厅投诉情况",align = "center") %>% hc_plotOptions(series = list(#图内柱子边界线宽,设置为0,取消显示边界线 borderWidth = 0)) %>% hc_xAxis(categories = type) %>% hc_yAxis_multiples( list( title = list(text = ""), tickPositions = c(0,200,400,600,800)), list( title = list(text = ""), minPadding = 0, maxPadding = 0, max = 100, min = 0, opposite = TRUE, labels = list(format = "{value}%"), tickPositions = c(0,25,50,75,100) ) )%>% hc_tooltip(pointFormat = "{series.name} {point.y:.2f} %")%>% hc_add_series(name = "投诉次数",type = "column",zIndex=1,data = num) %>% hc_add_series(type = "line",name = "累计比率",data = rate,yAxis =1,zIndex = 2) %>% hc_add_theme(hc_theme_economist())
tree_data <- data.table(country_name =c("巴西", "加拿大", "墨西哥", "美国", "葡萄牙", "法国", "西班牙", "英国", "南非", "安哥拉", "塞内加尔", "马里", "摩洛哥", "中国", "印度", "日本"),
value =c(2, 3, 1, 1, 4,5, 4, 3, 1, 1, 2, 3, 2, 6, 1, 1))
highchart() %>%
hc_colorAxis( maxColor = "#0043AE")%>%
hc_title(text = "矩形树图")%>%
hc_add_series(data = tree_data,type = "treemap",hcaes(name = country_name,value = value,colorValue =value),layoutAlgorithm = "squarified") %>%
hc_add_theme(hc_theme_google())
1.https://www.jianshu.com/p/1000b7fdd571
2.https://www.jianshu.com/p/c87eaf68fd85
3.https://www.bilibili.com/video/av5625356?from=search&seid=2829520952582005465
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。