Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R中的堆栈条将计数和百分比加在一起_R_Graph_Ggplot2_Stat - Fatal编程技术网

使用R中的堆栈条将计数和百分比加在一起

使用R中的堆栈条将计数和百分比加在一起,r,graph,ggplot2,stat,R,Graph,Ggplot2,Stat,我正在尝试在同一个图形中创建包含计数和百分比的堆栈条。我接受了来自的帮助并添加了组总数,并绘制了我的as 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", "

我正在尝试在同一个图形中创建包含计数和百分比的堆栈条。我接受了来自的帮助并添加了组总数,并绘制了我的as

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);
通过使用代码
###在顶部和顶部绘制总计的堆叠条形图的步骤
###频率分布;
图书馆(GG2);
图书馆(plyr);
图书馆(dplyr);

年我想你就快到了。 使用
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);

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

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);

p是的,这很有帮助。我固定了数字,使每个堆栈的中心。因此,我需要在代码下面进行百分比更改,以修复我的问题。非常感谢你的帮助

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);

p使用
geom_text
中的
vjust
参数。如上所述,交换两个
vjust
参数就可以了
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);