我有一个非常具体的问题,涉及R中的多线绘图。我发现了许多与条形图有关的问题,但与我目前的情况相去甚远。 我有以下简化的数据框和代码:
df <- data.frame(SEC_LABEL = c("3M","5Y",'10Y'), SEC1_VALUES = c(1.1, 1.4, 1.6), SEC2_VALUES = c(2.3, 2.6, 3.1), SORT=c(1,2,3))
color_line1 <- "#66C2A5" #('rgb(102, 194, 165)')
color_line2 <- "#8DA0CB" #('rgb(110, 194, 165)')
unique_line_names_1 <- "line1"
unique_line_names_2 <- "line2"
x_label < - as.character(df$SEC_LABEL)
p <- plot_ly(data = df, x = ~SORT) %>%
# Time series chart
add_lines(y = ~SEC1_VALUES, line = list(color = color_line1, width = 3),
hoverinfo = unique_line_names_1, text = unique_line_names_1, name = unique_line_names_1) %>%
add_lines(y = ~SEC2_VALUES, line = list(color = color_line2, width = 3),
hoverinfo = unique_line_names_2, text = unique_line_names_2, name = unique_line_names_2) %>%
layout(title=paste0("Curves"),
showlegend = TRUE,
margin = list(l=30, r = 20, b = 30, t = 30, pad =1),
legend = list(x = 0, y = 0.1,
font = list(size = 8),
#orientation = 'h',
bgcolor ='transparent' ),
xaxis=list(title='',
showline = FALSE,
zeroline = FALSE,
showticklabels = T,
showgrid = FALSE,
ticktext = x_label,
gridwidth =0
#gridcolor = toRGB("gray50")
),
yaxis=list(title='',
showline = F,
zeroline = FALSE,
showgrid = T,
gridwidth =2)
)
由于某些原因,未使用向量'x_label'中的值重新标记x轴上的刻度。 (顺便说一句,我之所以将SORT列而不是SEC_LABEL列用作x轴的唯一原因是,否则x轴将按字母顺序排序。理想情况下,所需的顺序应为:3M,5Y,10Y,但之后花了两个小时,我才意识到我无法修复它)。 感谢您的帮助
答案 0 :(得分:1)
我已经在我的评论中提到过,简单的解决方案是使用有序因子,即
df$SEC_LABEL <- factor(df$SEC_LABEL, levels = x_label)
并在x上映射SEC_LABEL
。这样,您将获得一个类别轴,其中类别按所需顺序排列。
但是,我使用schema()
浏览了一些阴暗的文档。根据密谋ticktext
设置通过
tickvals
在刻度位置显示的文本。只有tickmode
设置为 array 时的效果。与tickvals
一起使用。
要开始工作,必须在tickmode
中将"array"
设置为layout
,并在tickvals
中将c(1, 2, 3)
设置为# This is the most simple configuration
title = "FML rulez"
# We use ISO notations only, so no local styles
releaseDateTime = 2020-09-12T06:34
# Multiline strings
description = "So,
I'm curious
where this will end."
# Shorcut string; no quotes are needed in a simple property style assignment
# Or if a string is just one word. These strings are trimmed.
protocol = http
# Conditions allow for overriding, best match wins (most conditions)
# If multiple condition sets equally match, the first one will win.
title[env=production] = "One config file to rule them all"
title[env=production & os=osx] = "Even on Mac"
# Lists
hosts = [alpha, beta]
# Hierarchy is implemented using groups denoted by curly brackets
database {
# indenting is allowed and encouraged, but has no semantic meaning
url = jdbc://...
user = "admin"
# Strings support default encryption with a external key file, like maven
password = "FGFGGHDRG#$BRTHT%G%GFGHFH%twercgfg"
# groups can nest
dialect {
database = postgres
}
}
servers {
# This is a table:
# - the first row is a header, containing the id's
# - the remaining rows are values
| name | datacenter | maxSessions | settings |
| alpha | A | 12 | |
| beta | XYZ | 24 | |
| "sys 2" | B | 6 | |
# you can have sub blocks, which are id-less groups (id is the column)
| gamma | C | 12 | {breaker:true, timeout: 15} |
# or you reference to another block
| tango | D | 24 | $environment |
}
# environments can be easily done using conditions
environment[env=development] {
datasource = tst
}
environment[env=production] {
datesource = prd
}
你的情况。经过这些调整后,您的方法也将起作用。