代码之家  ›  专栏  ›  技术社区  ›  CurtLH

ggplot2圆形堆积条形图不带任何条形图重复标签

  •  1
  • CurtLH  · 技术社区  · 6 年前

    我想重新创造 this example 使用ggplot创建圆形条形图。但是,我想创建一个堆积条形图,而不是标准条形图。我已经接近了,但由于某种原因,标签在这个圆形条形图中重复出现。我认为问题在于 id 我正在创建以匹配示例,但我不确定如何修复它。

    df <- structure(list(team = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
    8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, NA, 1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, NA, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, NA), .Label = c("Team1", 
    "Team2", "Team3", "Team4", "Team5", "Team6", "Team7", "Team8", "Team9", "Team10", 
    "Team11", "Team12", "Team13", "Team14", "Team15"), class = "factor"), 
        variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
        2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
        3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("variable1", 
        "variable2", "variable3"), class = "factor"), value = c(3.91666666666667, 
        3.25, 3.88888888888889, 2.83333333333333, 3.16666666666667, 
        2.93333333333333, 2.66666666666667, 3.4, 3.33333333333333, 
        3.44444444444444, 3.41666666666667, 4, 4, 3.5, 4, 3.33333333333333, 
        3.8, 3.5, 3.86666666666667, 3, 2.96666666666667, 3.2, 3, 
        3.52, 3.26666666666667, 3.2, 3.45, 3.9, 3.6, 3.35, 3.86666666666667, 
        3, 3.91666666666667, 3.58333333333333, 4, 3.83333333333333, 
        3.44444444444444, 3.26666666666667, 3, 3.6, 3.33333333333333, 
        3.55555555555556, 3.66666666666667, 3.83333333333333, 3.5, 
        3.41666666666667, 4, 2.33333333333333)), row.names = c(NA, 
    -48L), class = "data.frame")
    
    df$id=seq(1, nrow(df))
    label_data=df
    number_of_bar=nrow(label_data)
    angle= 90 - 360 * (label_data$id-0.5) /number_of_bar
    label_data$hjust<-ifelse( angle < -90, 1, 0)
    label_data$angle<-ifelse(angle < -90, angle+180, angle)
    
    ggplot(data=df, aes(x=team, y=value, fill=variable)) +
      geom_bar(stat='identity') +
      ylim(-100,120) +
      theme_minimal() +
      theme(
        axis.text = element_blank(),
        axis.title = element_blank(),
        panel.grid = element_blank(),
        plot.margin = unit(rep(-1,4), "cm") 
      ) +
      coord_polar(start = 0) +
      geom_text(data=label_data, aes(x = id, y = 20, label=team, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE )
    

    2 回复  |  直到 6 年前
        1
  •  4
  •   Ronak Shah    6 年前

    是的,有个问题 id 创建。我们需要独特的 身份证件 对于每一个 team 以及 number_of_bar 基于 团队 .

    number_of_bar = length(unique(df$team))
    df$id = as.numeric(as.factor(df$team))
    label_data = df
    angle = 90 - 360 * (label_data$id-0.5) /number_of_bar
    label_data$hjust <-ifelse(angle < -90, 1, 0)
    label_data$angle <-ifelse(angle < -90, angle+180, angle)
    
    ggplot(data=df, aes(x=team, y=value, fill=variable)) +
       geom_bar(stat='identity') +
       ylim(-100,120) +
       theme_minimal() +
       theme(
         axis.text = element_blank(),
         axis.title = element_blank(),
         panel.grid = element_blank(),
         plot.margin = unit(rep(-1,4), "cm") 
         ) +
       coord_polar(start = 0) +
      geom_text(data=label_data, aes(x = id, y = 20, label=team, hjust=hjust),
      color="black", fontface="bold",alpha=0.6, size=2.5,
      angle= label_data$angle, inherit.aes = FALSE )
    

    enter image description here

        2
  •  1
  •   Marius    6 年前

    因为每个团队有多行,所以需要重新编写代码,以便X值和角度都基于团队,所以不能只使用行号:

    number_of_bar= length(unique(label_data$team))
    angle= 90 - 360 * (as.numeric(str_extract(label_data$team, "\\d+$")) - 0.5) / number_of_bar
    label_data$hjust<-ifelse( angle < -90, 1, 0)
    label_data$angle<-ifelse(angle < -90, angle+180, angle)
    
    ggplot(data=df, aes(x=team, y=value, fill=variable)) +
    geom_bar(stat='identity') +
    ylim(-100,120) +
    theme_minimal() +
    theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-1,4), "cm") 
    ) +
    coord_polar(start = 0) +
    geom_text(data=label_data, aes(x = team, y = 20, label=team, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE )