我有一个整洁的数据文件,类似于以下内容:
tidyDF <- data.frame(PORT_NAME = c("South Louisiana, LA, Port of",
"Houston, TX", "Long Beach, CA",
"New York, NY and NJ",
"Los Angeles, CA", "Beaumont, TX",
"Corpus Christi, TX", "New Orleans, LA",
"Baton Rouge, LA", "Mobile, AL"),
TOTAL_TONS = c(267390209, 234304391, 170052128,
126158655, 122033848, 87283716, 84928330,
84465052, 69185878, 64287565),
portSel = c("NO", "NO", "NO", "NO", "NO", "YES",
"NO", "NO", "NO", "NO"))我想要创建一个基于portSel变量的具有特定颜色的桶形图。
下面是我使用的代码:
library(highcharter)
myColors <- c("#002F80", "#F9AF38")
highchart() %>%
hc_add_series_df(data = tidyDF,
type = "bar",
x = PORT_NAME,
y = TOTAL_TONS,
group = portSel) %>%
hc_xAxis(title = list(text = "Ports"),
tickmarkPlacement = "on",
tickLength = 0,
labels = list(
enabled = FALSE
)) %>%
hc_yAxis(title = list(text = "2014 Total Tonnage")) %>%
hc_legend(enabled = FALSE) %>%
hc_colors(myColors)我试过group和color在hc_add_series_df。两者都不能正常工作。当我使用group = portSel时,如上面所示,颜色是正确的,但是它将单个YES端口移动到第一个NO端口。当我使用color = portSel时,它将YES端口放在正确的位置,但它不再使用myColors中的自定义颜色。
欢迎任何建议!谢谢。
发布于 2016-10-27 17:56:35
我修改了先前的答案。
你不能以正确的方式使用团队。group选项是创建/添加多个系列,这就是为什么第一个yes位于第一个no旁边,因为值是按顺序排列的。
关于颜色,函数hc_add_series_df根据给定的颜色变量对点(条、列)进行着色,所以不要使用hc_colors给出的颜色。
因此,我认为一个简单的方法是添加“手动”系列。这意味着从数据中删除一个包含特定数据(和颜色)的列表。
tidyDF2 <- tidyDF %>%
mutate(color = ifelse(portSel == "NO", myColors[1], myColors[2])) %>%
select(y = TOTAL_TONS, color)
highchart() %>%
hc_chart(type = "bar") %>%
hc_xAxis(labels = list(
enabled = FALSE
)) %>%
hc_add_series(data = list_parse(tidyDF2), showInLegend = FALSE)这对你有帮助吗?
先前的回答。
您可以尝试在hc_xAxis中添加下一个参数:type = "categorical", categories = tidyDF$PORT_NAME,,并在hc_add_series_df中使用group。您将看到一个条形图,其中带有"yes“列,这是因为您放置了2个系列(每个组一个),例如http://www.highcharts.com/demo/column-parsed。
highchart() %>%
hc_add_series_df(data = tidyDF,
type = "bar",
x = PORT_NAME,
y = TOTAL_TONS,
group = portSel) %>%
hc_xAxis(title = list(text = "Ports"),
type = "categorical",
categories = tidyDF$PORT_NAME,
tickmarkPlacement = "on",
tickLength = 0,
labels = list(
enabled = TRUE
)) %>%
hc_yAxis(title = list(text = "2014 Total Tonnage")) %>%
hc_legend(enabled = FALSE) %>%
hc_colors(myColors)https://stackoverflow.com/questions/40286297
复制相似问题