【发布时间】:2021-10-29 16:06:38
【问题描述】:
在此代码的帮助下,我可以得到每个折线图的总数,但我不知道如何对各个年份范围的值进行分组,以及如果没有数据如何安排线从零开始
x = structure(list(Sector = c("Agri", "Agri", "Agri",
"Agri", "Agri", "Education",
"Education ", "Education", "Education",
"Education", "Energy ", "Energy ",
"Energy", "wealth", "wealth", "wealth", "wealth",
"wealth"), year_range = c("2017-2018", "2018-2019", "2019-2020",
"2020-2021", "2021-2022", "2017-2018", "2018-2019", "2019-2020",
"2020-2021", "2021-2022", "2019-2020", "2020-2021", "2021-2022",
"2017-2018", "2018-2019", "2019-2020", "2020-2021", "2021-2022"
), Month = structure(c(12L, 12L, 12L, 12L, 5L, 12L, 12L, 12L,
12L, 5L, 12L, 12L, 5L, 12L, 12L, 12L, 12L, 5L), .Label = c("Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan",
"Feb", "Mar"), class = "factor"), total = c(1, 3, 9, 13, 13,
1, 6, 3, 1, 3, 1, 6, 9, 9, 12, 10, 4, 4)), row.names = c(NA,
-18L), class = c("data.table", "data.frame"), sorted = c("Sector",
"year_range")
y = structure(list(year_range = c("2019-2020", "2019-2020", "2019-2020",
"2019-2020", "2020-2021", "2020-2021", "2020-2021", "2020-2021",
"2020-2021", "2020-2021", "2020-2021", "2021-2022", "2021-2022",
"2021-2022"), total = c(0, 9, 10, 22, 0, 2, 14, 27, 30, 22, 13,
0, 39, 15)), row.names = c(NA, -14L), groups = structure(list(
year_range = c("2019-2020", "2020-2021", "2021-2022"), .rows = structure(list(
1:4, 5:11, 12:14), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Plan <- "#288D55"
x_labels = list(tickangle = -45,tickfont = list(family = "Arial",size = 10,color = "black",face="bold"))
y_labels = list(tickfont = list(family = "Arial",size = 10,color = "black",face="bold"))
ggplotly(
x %>%
group_by(year_range) %>%
mutate(total_year_range = sum(total)) %>%
ungroup() %>%
ggplot(aes(x = as.character(year_range), y = total, text = total_year_range))+
geom_col(aes(fill = Sector))+theme_classic()+
geom_line(data = y %>%
mutate(total_year_range = sum(total)), aes(x=as.character(year_range), y= total, group=1,color="Plan"),size=0.8)+
scale_colour_manual(name="",values=Plan)+
geom_point(data = y %>%
mutate(total_year_range = sum(total)), aes(x=as.character(year_range), y=total),color="#288D55")+
theme(axis.line.y = element_blank(),axis.ticks = element_blank(),legend.position = "bottom")+
labs(x="", y="No.of total companies", fill="")+
theme(axis.title.y =element_text(size=8))+
scale_fill_manual(values=c("#1F7A3F","#0AAA4D","#70B821","#9BDFAF"))+
scale_y_continuous(labels = function(x) format(x, scientific = FALSE),expand = expansion(mult = c(0,.3))),tooltip = c("text"))%>%
layout(legend = list(orientation = "h", x = 0.1, y = -0.2,font=list( family='Arial', size=10, color='black')),xaxis = x_labels,yaxis = y_labels)%>%
config(displaylogo = FALSE,modeBarButtonsToRemove = list('sendDataToCloud', 'autoScale2d', 'resetScale2d', 'toggleSpikelines','hoverClosestCartesian', 'hoverCompareCartesian','zoom2d','pan2d','select2d','lasso2d','zoomIn2d','zoomOut2d'))
【问题讨论】:
-
用折线图,你想把各个年份范围的值分组,然后显示总和,对吧?
-
是的,而且在点图中,点显示在多个地方需要修复那个@Shibaprasadb
-
y数据框没有年份范围内的那些特定值,这就是您没有得到任何东西的原因。我已经添加了它并在答案中编写了一个通用代码。让我知道这是否有帮助。