在 R 中使用 Stack Bar 计数和百分比

Count and Percent Together using Stack Bar in R

我正在尝试在同一图表中创建包含计数和百分比的堆栈条。我从 Showing data values on stacked bar chart in ggplot2 那里得到帮助并添加组总数并将我的绘制为

By using code 

### to plot stacked bar graph with total on the top and
###    distribution of the frequency;

library(ggplot2);
library(plyr);
library(dplyr);

Year      <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category  <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data      <- data.frame(Year, Category, Frequency);


sum_count <- 
   Data %>%
  group_by(Year) %>%
  summarise(max_pos = sum(Frequency));

sum_count;


Data <- ddply(Data, .(Year), transform, pos = 
cumsum(Frequency) - (0.5 * Frequency));

Data;



# plot bars and add text
p <- ggplot(Data, aes(x = Year, y = Frequency)) +
     geom_bar(aes(fill = Category), stat="identity") +
     geom_text(aes(label=Frequency,y = pos), size = 3) +  
     geom_text(data = sum_count, 
     aes(y = max_pos, label = max_pos), size = 4,
     vjust = -0.5);

print(p);

/现在我想用计数覆盖每个组的百分比这是我的 approach.merge 数据,我们可以这样计算 您正在处理的每个组的百分比/

    MergeData <- merge(Data,sum_count,by="Year");

    MergeData <- transform(MergeData,
    per_cent=round((pos/max_pos)*100,0));
    MergeData<- ddply(MergeData, .(Year), transform, per_pos = 
    cumsum(per_cent) - (0.5 * per_cent));

    # calculate percent and attach % sign;

    MergeData <- transform(MergeData,
    per_cent=paste(round((pos/max_pos)*100,0),"%"));

    # Data only with percents

    Percent_Data <- subset(MergeData,select 
    = c("Year","Category","per_cent","per_pos"));

/我想知道是否可以将百分比数据叠加到我使用以前的代码创建的图像上,以便数字和百分比可以一起显示。/

我想你快到了。 使用 MergeData 作为数据框的来源并添加一个对 geom_text

的调用
p <- ggplot(MergeData, aes(x = Year, y = Frequency, group = Category)) +
 geom_bar(aes(fill = Category), stat="identity") +
 geom_text(aes(label=Frequency,y = pos), size = 3, vjust = 1) +  
 geom_text(
        aes(y = max_pos, label = max_pos), size = 4,
        vjust = -.5) + 
 geom_text(aes(x = Year, y = pos, label = per_cent), vjust = -1, size = 4)

  print(p);

您可能需要 fiddle 与 hjustvjust 来获得您喜欢的文本。

感谢您的回复。我觉得很好

p <- ggplot(MergeData, aes(x = Year, y = Frequency, group = Category)) +
     geom_bar(aes(fill = Category), stat="identity") +
     geom_text(aes(label=Frequency,y = pos),  vjust = 1,size = 2,hjust = 0.5) +  
     geom_text(aes(y = max_pos, label = max_pos), size = 3,vjust = -.1) + 
     geom_text(aes(x = Year, y = pos, label = per_cent), vjust = -.4, size = 2)+
     xlab("Year") + ylab(" Number of People") +            # Set axis labels
     ggtitle("Distribution by Category over Year") +  # Set title
     theme(panel.background = 
     element_rect(fill = 'white', colour = 'white'),
     legend.position = "bottom" ,
     legend.title = element_text(color="black",
     size=7),
     legend.key.width = unit(1,"inch") );

 print(p);

现在我的百分比在数字之上,换句话说,它是“17%”和“168”,但我想要“168”和“17%”。我尝试切换 geom_text() 的位置,但它没有用。我想知道你是否知道如何修复它。

是的,它有帮助。我固定数字使每个堆栈居中。因此我需要更改代码下方的百分比以解决我的问题。非常感谢您的帮助。

p <- ggplot(MergeData, aes(x = Year, y = Frequency, group = Category)) +
     geom_bar(aes(fill = Category), stat="identity") +
     geom_text(aes(label=Frequency,y = pos),  vjust = 1,
     size = 2,hjust = 0.5) +  
     geom_text(aes(y = max_pos, label = max_pos), size = 3,vjust = -.1) + 
     geom_text(aes(x = Year, y = pos, label = per_cent), vjust = 1.95, 
     size = 2,hjust=0.3)+
     xlab("Year") + ylab(" Number of People") +            # Set axis labels
     ggtitle("Distribution by Category over Year") +       # Set title;
      theme(panel.background = 
    element_rect(fill = 'white', colour = 'white'),
    legend.position = "bottom" ,
    legend.title = element_text(color="black",
    size=7) );
 print(p);