[英]Editing R plotly axis labels in radar chart
I am trying to edit the axis of a plotly
radar chart.我正在尝试编辑
plotly
雷达图的轴。 The following code produces this plot .以下代码生成此 plot 。 Now I would like to add a percentage sign to the horizontal axis so that it says "0% 1% 2% 3% 4% 5%" instead of just the numbers?
现在我想在水平轴上添加一个百分号,这样它就可以显示“0% 1% 2% 3% 4% 5%”,而不仅仅是数字? How do I manage to do that?
我该如何做到这一点? Unfortunately, I do not really get along with the documentation with regard to changing the x axis.
不幸的是,我并没有真正理解有关更改 x 轴的文档。
fig <- plot_ly(
type = "scatterpolar",
fill = "toself",
mode = "lines+markers"
)
for (i in 1:3) {
d1 <- d %>% filter(Region %in% regions[i])
fig <- fig %>%
add_trace(
r = d1 %>% slice(1:3, 1) %>% pull(Wachstum),
theta = d1 %>% slice(1:3, 1) %>% pull(Bezeichnung),
name = d1 %>% slice(1) %>% pull(Region),
hovertemplate = paste0(
"%{fullData.name}<br>",
"%{theta}<br>",
"Wachstum: %{r:.3} %",
"<extra></extra>"
)
)
}
fig
On a related note, how would I edit the axis in the following case : I would like to change the "M" on the horizontal axis to "Mio."在相关说明中,在以下情况下我将如何编辑轴:我想将水平轴上的“M”更改为“Mio”。 so that it says "0 0.2Mio. 0.4Mio."
所以它说“0 0.2Mio。0.4Mio。” and so on?
等等?
fig <- plot_ly(
type = "scatterpolar",
fill = "toself",
mode = "lines+markers"
)
for (i in 1:3) {
d1 <- d %>% filter(Region %in% regions[i])
fig <- fig %>%
add_trace(
r = d1 %>% slice(1:3, 1) %>% pull(Wert),
theta = d1 %>% slice(1:3, 1) %>% pull(Bezeichnung),
name = d1 %>% slice(1) %>% pull(Region),
hovertemplate = paste0(
"%{fullData.name}<br>",
"%{theta}<br>",
"Wert: %{r:}",
"<extra></extra>"
)
)
}
fig
My data:我的数据:
library(tidyverse)
library(plotly)
d <- structure(
list(
Region = c("A", "A", "A", "B", "B", "B", "C",
"C", "C"),
Bezeichnung = c(
"Agriculture",
"Industry",
"Services",
"Agriculture",
"Industry",
"Services",
"Agriculture",
"Industry",
"Services"
),
Diff = c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L),
Wachstum = c(5,
3, 3, 5, 4, 4, 3, 3, 4),
Wert = c(
1322956.10582307,
1362979.06356215,
352138.918156773,
340230.355633795,
83354.3466503918,
1476435.89480847,
1321136.99315399,
388579.265012443,
715511.4116247
)
),
class = c("tbl_df",
"tbl", "data.frame"),
row.names = c(NA,-9L)
)
regions <- c("A", "B", "C")
Thanks a lot!非常感谢!
For the first part of your question, to show the radial axis as percentages (which is what I think you are asking), you have to use layout.polar.radialaxis.ticksuffix
.对于您问题的第一部分,要将径向轴显示为百分比(我认为您要问的是),您必须使用
layout.polar.radialaxis.ticksuffix
。
fig %>% layout(polar = list(
radialaxis = list(ticksuffix = '%')))
To format change the abbreviation from M
for Millions to Mio
for Millionen, you have a few options.要将缩写形式从
M
代表 Millions 更改为Mio
代表 Millionen,您有几个选择。 The first thing I tried was setting the locale to Deutschland, but that didn't work.我尝试的第一件事是将语言环境设置为德国,但这没有用。 (It was still
M
.) (它仍然是
M
。)
You could set the suffix, but the 0 would be 0io
, which doesn't make sense.您可以设置后缀,但 0 将是
0io
,这没有意义。 In the end, I specifically designated all of the values you see and what they represent.最后,我特别指定了您看到的所有值以及它们所代表的含义。
I initially used seq
, but it doesn't use a trailing zero;我最初使用
seq
,但它不使用尾随零; I didn't like how it turned out.我不喜欢结果。
fig %>% layout(polar = list(radialaxis = list(
tickmode = "array",
tickvals = seq(0, 1400000, by = 200000),
ticktext = paste0(seq(0, 1.4, by = .2), "Mio")
)))
fig %>% layout(polar = list(radialaxis = list(
tickmode = "array",
tickvals = c(0, 200000, 400000, 600000, 800000, 1000000, 1200000, 1400000),
ticktext = c("0", "0.2Mio", "0.4Mio", "0.6Mio", "0.8Mio", "1.0Mio", "1.2Mio", "1.4Mio")
)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.