hchart()条形图不显示R中的所有x轴标签

时间:2016-12-23 16:09:13

标签: r highcharts data-visualization

我有以下数据框,其中包含变量city,description和n。

city <- c("BIG COTTONWOOD", "BOUNTIFUL", "CANYON RIM", "CANYON RIM", 
"CANYON RIM", "COPPERTON", "COTTONWOOD HEIGHTS", "DRAPER", "DRAPER", 
"EAST MILLCREEK", "FARMINGTON", "FARMINGTON", "HERRIMAN", "HERRIMAN", 
"HERRIMAN", "HERRIMAN", "HOLLADAY", "KEARNS", "KEARNS", "MAGNA", 
"MAGNA", "MAGNA", "MAGNA", "MAGNA", "MIDVALE", "MIDVALE", "MIDVALE", 
"MIDVALE", "MIDVALE", "MURRAY", "MURRAY", "MURRAY", "MURRAY", 
"MURRAY", "MURRAY", "MURRAY", "NORTH SALT LAKE", "OLYMPUS COVE", 
"OLYMPUS COVE", "PARK CITY", "RIVERTON", "SALT LAKE CITY", "SALT LAKE CITY", 
"SALT LAKE CITY", "SALT LAKE CITY", "SALT LAKE CITY", "SALT LAKE CITY", 
"SALT LAKE CITY", "SALT LAKE CITY", "SALT LAKE CITY", "SALT LAKE CITY", 
"SALT LAKE COUNTY", "SALT LAKE COUNTY", "SANDY", "SANDY", "SANDY", 
"SANDY", "SANDY", "SANDY", "SOUTH JORDAN", "SOUTH SALT LAKE", 
"SOUTH SALT LAKE", "SOUTH SALT LAKE", "SOUTH SALT LAKE", "SOUTH SALT LAKE", 
"SOUTH SALT LAKE", "SOUTH SALT LAKE", "SOUTH SALT LAKE", "SOUTH WEST SL COUNTY", 
"TAYLORSVILLE", "TAYLORSVILLE", "TAYLORSVILLE", "TOOELE", "UTAH COUNTY", 
"WEST JORDAN", "WEST JORDAN", "WEST JORDAN", "WEST JORDAN", "WEST MILLCREEK", 
"WEST MILLCREEK", "WEST MILLCREEK", "WEST MILLCREEK", "WEST MILLCREEK", 
"WEST MILLCREEK", "WEST MILLCREEK", "WEST VALLEY CITY", "WEST VALLEY CITY", 
"WEST VALLEY CITY", "WEST VALLEY CITY", "WEST VALLEY CITY", "WEST VALLEY CITY", 
"WEST VALLEY CITY", "WEST VALLEY CITY", "WOODS CROSS", "WOODS CROSS"
)
description <- c("PUBLIC ORDER", "PUBLIC ORDER", "LARCENY", "PUBLIC ORDER", 
"PUBLIC PEACE", "PUBLIC ORDER", "PUBLIC ORDER", "NONREPTABL TA", 
"PUBLIC ORDER", "PUBLIC ORDER", "DAMAGED PROP", "PUBLIC ORDER", 
"DAMAGED PROP", "DRUGS", "LARCENY", "PUBLIC ORDER", "PUBLIC ORDER", 
"PUBLIC ORDER", "PUBLIC PEACE", "ASSAULT", "DRUGS", "ESCAPE", 
"LARCENY", "PUBLIC ORDER", "DRUGS", "ESCAPE", "PUBLIC ORDER", 
"PUBLIC PEACE", "REPORTABLE TA", "ASSAULT", "DAMAGED PROP", "DRUGS", 
"ESCAPE", "LARCENY", "PUBLIC ORDER", "STOLEN VEHICLE", "PUBLIC ORDER", 
"ESCAPE", "PUBLIC ORDER", "PUBLIC ORDER", "PUBLIC ORDER", "ASSAULT", 
"DAMAGED PROP", "DRUGS", "ESCAPE", "LARCENY", "NONREPTABL TA", 
"PUBLIC ORDER", "PUBLIC PEACE", "REPORTABLE TA", "STOLEN VEHICLE", 
"PUBLIC ORDER", "REPORTABLE TA", "DAMAGED PROP", "DRUGS", "ESCAPE", 
"NONREPTABL TA", "PUBLIC ORDER", "PUBLIC PEACE", "PUBLIC ORDER", 
"ASSAULT", "DRUGS", "ESCAPE", "LARCENY", "PUBLIC ORDER", "PUBLIC PEACE", 
"REPORTABLE TA", "STOLEN VEHICLE", "PUBLIC ORDER", "ESCAPE", 
"PUBLIC ORDER", "PUBLIC PEACE", "DAMAGED PROP", "PUBLIC PEACE", 
"DRUGS", "ESCAPE", "PUBLIC ORDER", "PUBLIC PEACE", "ASSAULT", 
"DRUGS", "ESCAPE", "LARCENY", "NONREPTABL TA", "PUBLIC ORDER", 
"PUBLIC PEACE", "ASSAULT", "DRUGS", "ESCAPE", "NONREPTABL TA", 
"PUBLIC ORDER", "PUBLIC PEACE", "REPORTABLE TA", "STOLEN VEHICLE", 
"DAMAGED PROP", "PUBLIC ORDER")

n <- c(3L, 2L, 1L, 2L, 1L, 1L, 4L, 1L, 16L, 6L, 1L, 4L, 1L, 1L, 2L, 
2L, 6L, 13L, 2L, 2L, 1L, 2L, 1L, 8L, 3L, 1L, 11L, 1L, 2L, 1L, 
1L, 2L, 3L, 1L, 8L, 1L, 1L, 1L, 2L, 1L, 4L, 4029L, 2496L, 4837L, 
3637L, 11432L, 2348L, 8008L, 3798L, 2210L, 2341L, 1L, 1L, 1L, 
4L, 1L, 1L, 10L, 1L, 3L, 1L, 11L, 9L, 6L, 13L, 1L, 2L, 1L, 3L, 
1L, 12L, 2L, 1L, 1L, 3L, 1L, 7L, 1L, 2L, 1L, 3L, 2L, 1L, 15L, 
1L, 2L, 6L, 7L, 4L, 16L, 5L, 2L, 1L, 1L, 1L)

slc <- data.frame(city,description,n)

我正在使用J.Kunst提供的highcharter()包在分组条形图中显示上述数据框。我为此运行了以下命令。

library(highcharter)
library(dplyr)
colors <- c("#487098","#484898","#34348d","#19198b","#208582","#942f59","#dd125b","#000000","#c61051","#000FFF")


slc %>%  hchart("column",x=city,y=log(n*100),group=description)%>%hc_add_theme(hc_theme_google()) %>% hc_colors(colors) 

正确显示分组条形图,但有一个小问题。并非所有slc$city变量都显示在x轴上。这可能是由于可用的城市变量数量。有没有办法减少文本显示大小? (类似于geom_text中的ggplot2)提前谢谢你!

1 个答案:

答案 0 :(得分:0)

您可以通过将xAxis.labels.step设置为1来强制显示所有xAxis标签。每次都不会很好地计算标签的旋转度,因此您可以通过设置将rotation设置为-90

slc %>%  hchart("column",x=city,y=log(n*100),group=description) %>%
    hc_add_theme(hc_theme_google()) %>%
    hc_colors(colors) %>% 
    hc_xAxis(labels = list(rotation = -90, step = 1))

结果:

enter image description here

Highcharts演示(不含R):http://jsfiddle.net/Lv12u7cg/