gpt4 book ai didi

r - plotly R : bar chart with two axes

转载 作者:行者123 更新时间:2023-12-03 23:15:33 25 4
gpt4 key购买 nike

是否可以使用 plotly for 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

这给出:

bar plot 1

初级产品的值比次级产品小得多 - 我的目的是按月比较比例,同时仍然让 y 轴显示实际值而不是标准化值,所以它看起来像这样(模拟):

enter image description here

我试过 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

enter image description here

最佳答案

我玩过 this answer并找到以下导数以获得最佳结果。

这是结果图:

/image/CLRNe.png

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

关于r - plotly R : bar chart with two axes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51740331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com