我使用 r 创建了一个分组条形图,以图表方式显示英国各个国家/地区的 LGBT 人口(按年份分组)。现在,我尝试向第一个和最后一个栏添加标签以显示数据的开始年份和结束年份,而不是使用图例,但我正在努力使标签与正确的栏对齐。我使用了注释功能并使用 xshift 手动对齐两个标签,但此解决方案对于具有缩放等功能的 html 报告并不理想。我想找到一个更好的解决方案,自动将标签与分组条对齐。有没有一种简单的方法可以使标签按年份变量对齐,以便它们与正确的条形对齐?
此图表的我的数据:
# A tibble: 30 × 3
Year Country Percentage
<dbl> <chr> <dbl>
1 2017 Northern Ireland 0.016
2 2017 England 0.028
3 2017 Scotland 0.023
4 2017 Wales 0.025
5 2017 UK 0.027
6 2018 Northern Ireland 0.016
7 2018 England 0.029
8 2018 Scotland 0.026
9 2018 Wales 0.031
10 2018 UK 0.029
11 2019 Northern Ireland 0.017
12 2019 England 0.034
13 2019 Scotland 0.031
14 2019 Wales 0.039
15 2019 UK 0.034
16 2020 Northern Ireland 0.018
17 2020 England 0.037
18 2020 Scotland 0.036
19 2020 Wales 0.042
20 2020 UK 0.038
21 2021 Northern Ireland 0.023
22 2021 England 0.036
23 2021 Scotland 0.038
24 2021 Wales 0.047
25 2021 UK 0.036
26 2022 Northern Ireland 0.023
27 2022 England 0.039
28 2022 Scotland 0.04
29 2022 Wales 0.053
30 2022 UK 0.039
这是我当前的代码块。
Figure5$Country = factor(Figure5$Country, levels = c("Northern Ireland","England","Scotland","Wales", "UK"))
#wrapping labels on chart
Figure5$wrappedy <- sapply(Figure5$Country,
FUN = function(Country) {paste(strwrap(Country, width = 10), collapse = " <br> ")})
# named color vector
cols <- setNames(c("#12436D","#28A197","#801650","#F46A25","#3D3D3D","#A285D1"), c("2017","2018","2019","2020","2021","2022"))
annoa <- list(x = "Northern Ireland", y = 0.3, text = "2018", xref = "2018", yref = "y", xshift='-354', textangle= -90, showarrow = FALSE, font = list(color = "White", family = 'mono',size = 16, fontface=2))
annob <- list(x = "Northern Ireland", y = 0.3, text = "2022", xref = "2022", yref = "y", xshift='-256', textangle= -90, showarrow = FALSE, font = list(color = "White", family = 'mono',size = 16, fontface=2))
hoverfonts <- list(
color = "white")
hoverlabels <- list(
bordercolor = "transparent",
font = hoverfonts)
#filtering out 2017
plot_ly(Figure5[Figure5$Year !="2017",], x = ~wrappedy, y = ~Percentage,
type = 'bar', color = ~Year, name = ~Year, colors = cols,
# marker = list(pattern = list(shape = rep(c("x", "/", ".","",""), each = 5)),
# line = list(width = 1.5)),
hoverlabel = hoverlabels) %>%
# layout to align bar and box traces
layout(yaxis = list(rangemode='tozero', ticksuffix = "%", title = ""),
xaxis = list(title = "",categoryarray = ~wrappedy, categoryorder = c("Northern Ireland","England","Scotland","Wales", "UK")))%>%
layout(margin=list(pad=5)) %>%
hide_colorbar()%>%
layout(annotations = annoa)%>%
layout(annotations = annob)%>%
config(displayModeBar = FALSE)%>%
layout(showlegend=FALSE)
#Create the CSV and Excel Files
downloadButtons(data = Figure5[,c('Year','Country','Percentage')],
title = "Figure 5: LGBT+ Across the UK Regions (16+)")
将来,如果您在提供数据时粘贴
dput(data)
的输出,那么帮助会更容易。
您没有提供您理想想要的示例,但我认为您正在寻找一个多类别条形图,其中显示了年份和国家/地区。您可以通过几种不同的方式来做到这一点。例如,年份可以是条形上的文本标签。另一种方法是为一个轴提供两个字段。
我从你的问题中使用的数据和对象。
Figure5 <- structure(list(
Year = c("2017", "2017", "2017", "2017", "2017",
"2018", "2018", "2018", "2018", "2018", "2019", "2019", "2019",
"2019", "2019", "2020", "2020", "2020", "2020", "2020", "2021",
"2021", "2021", "2021", "2021", "2022", "2022", "2022", "2022",
"2022"),
Country = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L),
levels = c("Northern Ireland", "England", "Scotland", "Wales", "UK"),
class = c("ordered", "factor")),
Percentage = c(0.016, 0.028, 0.023, 0.025, 0.027, 0.016,
0.029, 0.026, 0.031, 0.029, 0.017, 0.034, 0.031, 0.039, 0.034,
0.018, 0.037, 0.036, 0.042, 0.038, 0.023, 0.036, 0.038, 0.047,
0.036, 0.023, 0.039, 0.04, 0.053, 0.039),
wrappedy = c("Northern<br>Ireland",
"England", "Scotland", "Wales", "UK", "Northern<br>Ireland",
"England", "Scotland", "Wales", "UK", "Northern<br>Ireland",
"England", "Scotland", "Wales", "UK", "Northern<br>Ireland",
"England", "Scotland", "Wales", "UK", "Northern<br>Ireland",
"England", "Scotland", "Wales", "UK", "Northern<br>Ireland",
"England", "Scotland", "Wales", "UK")),
row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"))
# named color vector
cols <- setNames(c("#12436D","#28A197","#801650","#F46A25","#3D3D3D","#A285D1"),
c("2017","2018","2019","2020","2021","2022"))
hoverlabels <- list(bordercolor = "transparent",
font = list(color = "white"))
向栏中添加文本:您会看到我已将
text
和 textposition
添加到 plot_ly
并合并了您在问题中设置的参数。
plot_ly(Figure5[Figure5$Year !="2017",], x = ~wrappedy, y = ~Percentage,
type = 'bar', color = ~Year, name = ~Year, text = ~Year, textposition = "outside",
colors = cols, hoverlabel = hoverlabels, showlegend = F) %>%
layout(yaxis = list(title = "", ticksuffix = "%"),
xaxis = list(title = "", categoryarray = ~wrappedy,
categoryorder = c("Northern Ireland","England","Scotland","Wales", "UK")),
margin = list(pad = 5)) %>% config(displayModeBar = F)
在这里您会看到我翻转了轴,这更多的是为了空间和清晰度。对于
y
,我从数据框中添加了 2 列来创建多类别轴。由于条形图位于 y
上,因此我添加了 orientation = "h"
表示水平方向。
当您翻转轴时,它会颠倒顺序,因此我可以使用
autorange = "reversed"
将正确的顺序返回到 layout(yaxis...
中的 y 轴。代码的其余部分基于您问题中提供的格式。
plot_ly(Figure5[Figure5$Year !="2017",], y = c(~wrappedy, ~Year), x = ~Percentage,
type = 'bar', color = ~Year, name = ~Year,
colors = cols, orientation = "h",
hoverlabel = hoverlabels, showlegend = F) %>%
layout(yaxis = list(autorange = "reversed"),
xaxis = list(ticksuffix = "%", title = ""),
margin = list(pad = 5)) %>% config(displayModeBar = F)
如果有任何不清楚的地方或者您还有其他问题,请告诉我。