2017-05-17 148 views
2

所以我试图做一个堆积条形图。这是所有需要在情节中的数据。我想让a-e在f-j旁边。我也试图让y轴合理。出于某种原因,它将数字而不是限制。我可能完全错了。我已经欺骗了R中的堆积条形图

data<- rbind(c(4655, 4212, 3838, 3583, 3124, 3078, 1411, 2589, 2524, 2429), 
     c(2826, 2589, 2557, 2713, 2497, 1, 2, 66, 1757, 1822)) 

a <- cbind(data[, 1], 1, c("50-60", "40-49")) 
b <- cbind(data[, 2], 2, c("50-60", "40-49")) 
c <- cbind(data[, 3], 3, c("50-60", "40-49")) 
d <- cbind(data[, 4], 4, c("50-60", "40-49")) 
e <- cbind(data[, 5], 5, c("50-60", "40-49")) 
f <- cbind(data[, 6], 1, c("50-60", "40-49")) 
g <- cbind(data[, 7], 2, c("50-60", "40-49")) 
h <- cbind(data[, 8], 3, c("50-60", "40-49")) 
i <- cbind(data[, 9], 4, c("50-60", "40-49")) 
j <- cbind(data[, 10], 5, c("50-60", "40-49")) 

data <- as.data.frame(rbind(a, b, c, d, e, f, g, h, i, j)) 
colnames(data) <-c("Number", "Year", "Age") 
data$Year  <- factor(data$Year, labels = c("FY13", 
         "FY14","FY15","FY16","FY17")) 

library(ggplot2) 

ggplot(data = data, aes(x = data$Year, y = Number, fill = Age)) + 
     geom_bar(stat = "identity") 

我可能是完全错误的。我看了看,但看不到其他东西来帮助我。

+1

我想说的数字是你的例子中的一个因素,你需要将它转换为数字'数据$数字< - as.numeric(数据$数字)'...检查答案@JanLauGe看看是否这是你需要的... – Umberto

回答

3

这听起来像你不想年度,年龄组的用户总和。所以我创造了另一个变量,让他们分开,然后用facet_grid

data <- data.frame(as.vector(data), rep(rep(1:5, each=2),2), c("50-60","40-49"), rep(c("Grp1","Grp2"), each=10)) 

#data <- as.data.frame(rbind(a, b, c, d, e, f, g, h, i, j)) 
colnames(data) <-c("Number", "Year", "Age", "Grp") 
data$Year  <- factor(data$Year, labels = c("FY13", 
               "FY14","FY15","FY16","FY17")) 

library(ggplot2) 

ggplot(data = data, aes(x = Grp, y = Number, fill = Age)) + facet_grid(~Year) + 
    geom_bar(stat = "identity") 

enter image description here

0

你想要这个吗?

# Your original data 
data %>% 
    # Number should be numeric 
    mutate(Number = as.numeric(Number), 
     # Groups ('a-e' or 'f-j') aren't in the data 
     # so I am randomly generating them here 
     Group = sample(c('a-e', 'f-j'), n(), replace = TRUE)) %>% 
    # I assume you want one (overall) value per year, age, and group ('a-e' or 'f-j') 
    group_by(Year, Age, Group) %>% 
    # So we get the sum of number 
    summarise(Number = sum(Number)) %>% 
    # And make the plot 
    ggplot(aes(x = Year, y = Number, fill = Age, group = Group)) + 
    geom_bar(stat = 'identity', position = 'dodge', width = 0.9) 

enter image description here

+0

这是非常接近我想要的。但是我希望在f-j旁边有一个侧面条形图。 –

+0

'a-e'和'f-j'不在您的示例数据中,因此我随机将它们制作成 – JanLauGe

+0

我想通了。 –

1

您需要dodge您在GGPLOT2位置如下图所示:

数据

data <- as.data.frame(rbind(a, b, c, d, e, f, g, h, i, j)) 
data <- cbind(data,Group = c(rep("a-e",5),rep("f-j",5))) 
colnames(data) <-c("Number", "Year", "Age", 
        "Group") 
data$Year <- factor(data$Year, labels = c("FY13","FY14","FY15","FY16","FY17")) 
data$Age <- factor(data$Age) 
data$Group <- factor(data$Group) 

绘制

library(ggplot2) 


ggplot(data = data, aes(x = Age , y = as.numeric(Number), fill = Year)) + 
    geom_bar(stat = "identity",position="dodge") + facet_grid(~Group) 

?ggplot2::position_dodge

闪躲保留一个的geom的垂直位置而调整 水平位置。

输出

enter image description here