如何排列折线图和点图的值
How to arrange values for line and point chart
借助这段代码,我可以获得每个折线图的总和,但我不知道如何对各个年份范围的值进行分组,也不知道如果没有数据,如何安排从零开始的线
任何人都可以让我知道如何实现这个...提前致谢
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'))
请注意:您的示例 x
中的 Sector
变量有两个额外的空格,但未给出正确答案。所以,我重新检查并删除了它们,然后继续。
前两个柱状图没有任何 0,因为那些年的数据框中没有任何 0。所以我有 运行 一个简单的循环并为 y
数据框
创建了两行
i<-1
j<-1
year_range <- c()
while (i <= nrow(x)) {
if(!(x$year_range[i] %in% y$year_range)){
year_range[j] <- x$year_range[i]
j <- j+1
}
i <- i+1
}
year_range <- unique(year_range)
total <- rep(0, length(year_range))
y_temp <- data.frame(cbind(year_range, total))
y_temp$total<-as.numeric(y_temp$total)
y <- rbind(y, y_temp)
现在,画剧情,如愿以偿。这里我给出了一个简单的 ggplot 示例,但您可以根据需要自定义它:
ggplot()+
geom_col(data=x%>%
group_by(year_range)%>%
mutate(total_year_range = sum(total)) %>%
ungroup(), aes(x=year_range, y=total, fill=Sector))+
geom_line(data=y%>%
group_by(year_range)%>%
mutate(total_year_range = sum(total))%>%
distinct(year_range, .keep_all = T), aes(x=year_range, y=total_year_range, group = 1))+
geom_point(data=y%>%
group_by(year_range)%>%
mutate(total_year_range = sum(total))%>%
distinct(year_range, .keep_all = T), aes(x=year_range, y=total_year_range))+
xlab('Range of the year')+
ylab('Total number of companies')+
theme_bw()+
theme(legend.position = 'top')
如果这能解决您的问题,请告诉我。
借助这段代码,我可以获得每个折线图的总和,但我不知道如何对各个年份范围的值进行分组,也不知道如果没有数据,如何安排从零开始的线
任何人都可以让我知道如何实现这个...提前致谢
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'))
请注意:您的示例 x
中的 Sector
变量有两个额外的空格,但未给出正确答案。所以,我重新检查并删除了它们,然后继续。
前两个柱状图没有任何 0,因为那些年的数据框中没有任何 0。所以我有 运行 一个简单的循环并为 y
数据框
i<-1
j<-1
year_range <- c()
while (i <= nrow(x)) {
if(!(x$year_range[i] %in% y$year_range)){
year_range[j] <- x$year_range[i]
j <- j+1
}
i <- i+1
}
year_range <- unique(year_range)
total <- rep(0, length(year_range))
y_temp <- data.frame(cbind(year_range, total))
y_temp$total<-as.numeric(y_temp$total)
y <- rbind(y, y_temp)
现在,画剧情,如愿以偿。这里我给出了一个简单的 ggplot 示例,但您可以根据需要自定义它:
ggplot()+
geom_col(data=x%>%
group_by(year_range)%>%
mutate(total_year_range = sum(total)) %>%
ungroup(), aes(x=year_range, y=total, fill=Sector))+
geom_line(data=y%>%
group_by(year_range)%>%
mutate(total_year_range = sum(total))%>%
distinct(year_range, .keep_all = T), aes(x=year_range, y=total_year_range, group = 1))+
geom_point(data=y%>%
group_by(year_range)%>%
mutate(total_year_range = sum(total))%>%
distinct(year_range, .keep_all = T), aes(x=year_range, y=total_year_range))+
xlab('Range of the year')+
ylab('Total number of companies')+
theme_bw()+
theme(legend.position = 'top')
如果这能解决您的问题,请告诉我。