是否有两个轴可用于R的条形图?
该代码已从plotly bar chart example修改
library(plotly)
x <- c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
y1 <- c(20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17)/10
y2 <- c(19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16)
data <- data.frame(x, y1, y2)
#The default order will be alphabetized unless specified as below:
data$x <- factor(data$x, levels = data[["x"]])
p <- plot_ly(data, x = ~x, y = ~y1, type = 'bar', name = 'Primary Product', marker = list(color = 'rgb(49,130,189)')) %>%
add_trace(y = ~y2, name = 'Secondary Product', marker = list(color = 'rgb(204,204,204)')) %>%
layout(xaxis = list(title = "", tickangle = -45),
yaxis = list(title = ""),
margin = list(b = 100),
barmode = 'group')
p
这给出了:
主要产品的值比次要产品小得多-我的目的是按月比较比例,同时让y轴显示实际值而不是归一化值,因此看起来像这样(模拟):< / p>
我尝试plotly help添加另一个轴,这是代码。但是,它似乎什么也没做,结果图表与第一个图表相同。
library(plotly)
x <- c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
y_1 <- c(20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17)/10
y_2 <- c(19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16)
data <- data.frame(x, y_1, y_2)
#The default order will be alphabetized unless specified as below:
data$x <- factor(data$x, levels = data[["x"]])
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
p <- plot_ly(data, x = ~x, y = ~y_1, type = 'bar', name = 'Primary Product', marker = list(color = 'rgb(49,130,189)')) %>%
add_trace(y = ~y_2, name = 'Secondary Product', marker = list(color = 'rgb(204,204,204)'), axis = "y2") %>%
layout(yaxis2 = ay,
xaxis = list(title = "", tickangle = -45),
yaxis = list(title = ""),
margin = list(b = 100),
barmode = 'group')
p
更新2018年8月9日
MLavoie发布了此代码,与尝试类似,但是它提供了堆积的条形图-这不是预期的结果。
data$x <- factor(data$x, levels = data[["x"]])
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
p <- plot_ly() %>%
add_bars(data, x = ~x, y = ~y1, name = 'Primary Product', marker = list(color = 'rgb(49,130,189)')) %>%
add_bars(data, x = ~x, y = ~y2, name = 'Secondary Product', marker = list(color = 'rgb(204,204,204)'), yaxis = "y2") %>%
layout(yaxis2 = ay,
xaxis = list(title = "", tickangle = -45),
yaxis = list(title = ""),
margin = list(b = 100),
barmode = 'group')
p
答案 0 :(得分:3)
我和this answer一起玩,发现以下派生词能给出最佳结果。
这是结果图:
library(plotly)
library(dplyr)
x <- c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
y1 <- c(20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17)/10
y2 <- c(19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16)
data <- data.frame(x, y1, y2)
data$x <- factor(data$x, levels = data[["x"]])
ay <- list(
overlaying = "y",
side = "right",
title = "Right Hand Axis"
)
p <- plot_ly() %>%
add_bars(data, x = ~x, y = ~y1, name = 'Primary Product (LHS)',
marker = list(color = 'rgb(49,130,189)'), offsetgroup = 1) %>%
add_bars(data, x = ~x, y = ~y2, name = 'Secondary Product (RHS)',
marker = list(color = 'rgb(204,204,204)'), yaxis = "y2", offsetgroup = 2) %>%
layout(yaxis2 = ay,
xaxis = list(title = "Time Period", tickangle = -45),
yaxis = list(title = "Left Hand Axis"),
margin = list(b = 100),
barmode = 'group',
legend = list(x = 1.1, y = 1))
p
答案 1 :(得分:0)
我认为您有一些错别字,但这可以解决问题。
data$x <- factor(data$x, levels = data[["x"]])
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
p <- plot_ly() %>%
add_bars(data, x = ~x, y = ~y1, name = 'Primary Product', marker = list(color = 'rgb(49,130,189)')) %>%
add_bars(data, x = ~x, y = ~y2, name = 'Secondary Product', marker = list(color = 'rgb(204,204,204)'), yaxis = "y2") %>%
layout(yaxis2 = ay,
xaxis = list(title = "", tickangle = -45),
yaxis = list(title = ""),
margin = list(b = 100),
barmode = 'group')
p
答案 2 :(得分:0)
一个解决方案是创建两个额外的跟踪。这是Python中的示例:
此代码基于以下单独问题的代码:Plotly Python: Align X-Axes in a grouped bar chart with multiple Y-axis
答案 3 :(得分:0)
根据讨论here,这应该可行。
x <- c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
y1 <- c(20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17)/10
y2 <- c(19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16)
data <- data.frame(x, y1, y2)
data$x <- factor(data$x, levels = data[["x"]])
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
p <- plot_ly() %>%
add_bars(data, x = ~x, y = ~y1, name = 'Primary Product',
marker = list(color = 'rgb(49,130,189)'), offsetgroup = 1) %>%
add_bars(data, x = ~x, y = ~y2, name = 'Secondary Product',
marker = list(color = 'rgb(204,204,204)'), yaxis = "y2", offsetgroup = 2) %>%
layout(yaxis2 = ay,
xaxis = list(title = "", tickangle = -45),
yaxis = list(title = ""),
margin = list(b = 100),
barmode = 'group')
p
通过offsetgroup
属性,您可以更好地控制交叉迹线偏移量和宽度值的计算方式。