【问题标题】:How to arrange values for line and point chart如何排列折线图和点图的值
【发布时间】: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 数据框没有年份范围内的那些特定值,这就是您没有得到任何东西的原因。我已经添加了它并在答案中编写了一个通用代码。让我知道这是否有帮助。

标签: r ggplot2 dplyr ggplotly


【解决方案1】:

请注意:在您的示例 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')

如果这能解决你的目的,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2014-07-09
    • 1970-01-01
    相关资源
    最近更新 更多