使用 ggplot 对堆积条形图中的簇重新排序
Reorder the clusters in stacked bar plot using ggplot
假设我有这些数据集:
test <- data.frame(person=c("0-4", "5-9", "10-14"),
value1=c(100,150,120), # male
value2=c(25,30,45) , # female
value3=c(25,30,45), # male
value4=c(100,120,150), # female
value5=c(10,12,15), # male
value6=c(50,40,70)) # female
我使用 ggplot2 创建了一个集群堆叠条形图。我的 R 代码是这样的:
library(reshape2) # for melt
melted <- melt(test, "person")
melted$cat <- ''
melted[melted$variable == 'value1' | melted$variable == 'value2',]$cat <- "sub group 1"
melted[melted$variable == 'value3' | melted$variable == 'value4',]$cat <- "sub group 2"
melted[melted$variable == 'value5' | melted$variable == 'value6',]$cat <- "sub group 3"
melted$gender <- ''
melted[melted$variable %in% sprintf("value%i",c(1,3,5)),]$gender <- "female"
melted[melted$variable %in% sprintf("value%i",c(2,4,6)),]$gender <- "male"
p = ggplot(melted, aes(x = cat, y = value, fill = gender))
p + geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ person) +
scale_fill_manual(values = c("orangered","dodgerblue2")) +
theme(panel.background = element_rect(fill = 'white'))
我有下图:
大家可以看到人组排列为0-4、10-14、5-9。我希望它被排列为 0-4、5-9、10-14。我检查了 str()
并将变量显示为因子。我的问题是:
- 知道如何将 person 变量重新排序为 0-4、5-9、10-14 吗?
- 如何命名图表标题 x-axis 和 y-axis?
这是重新排序网格所需的内容:
levels(melted$person) <- c("0-4", "5-9", "10-14") # change the order of the levels
当您不知道如何更改 factor
的顺序时,总是打印 levels
并查看它们是如何排序的。
使用labs
你可以改变坐标轴和标题:
p = ggplot(melted, aes(x = cat, y = value, fill = gender))
p + geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ person) +
scale_fill_manual(values = c("orangered","dodgerblue2")) +
theme(panel.background = element_rect(fill = 'white')) +
labs(title="abc title", x="aaa",y="bbb")
#+theme(plot.title = element_text(hjust = 0.5)) # center title
假设我有这些数据集:
test <- data.frame(person=c("0-4", "5-9", "10-14"),
value1=c(100,150,120), # male
value2=c(25,30,45) , # female
value3=c(25,30,45), # male
value4=c(100,120,150), # female
value5=c(10,12,15), # male
value6=c(50,40,70)) # female
我使用 ggplot2 创建了一个集群堆叠条形图。我的 R 代码是这样的:
library(reshape2) # for melt
melted <- melt(test, "person")
melted$cat <- ''
melted[melted$variable == 'value1' | melted$variable == 'value2',]$cat <- "sub group 1"
melted[melted$variable == 'value3' | melted$variable == 'value4',]$cat <- "sub group 2"
melted[melted$variable == 'value5' | melted$variable == 'value6',]$cat <- "sub group 3"
melted$gender <- ''
melted[melted$variable %in% sprintf("value%i",c(1,3,5)),]$gender <- "female"
melted[melted$variable %in% sprintf("value%i",c(2,4,6)),]$gender <- "male"
p = ggplot(melted, aes(x = cat, y = value, fill = gender))
p + geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ person) +
scale_fill_manual(values = c("orangered","dodgerblue2")) +
theme(panel.background = element_rect(fill = 'white'))
我有下图:
大家可以看到人组排列为0-4、10-14、5-9。我希望它被排列为 0-4、5-9、10-14。我检查了 str()
并将变量显示为因子。我的问题是:
- 知道如何将 person 变量重新排序为 0-4、5-9、10-14 吗?
- 如何命名图表标题 x-axis 和 y-axis?
这是重新排序网格所需的内容:
levels(melted$person) <- c("0-4", "5-9", "10-14") # change the order of the levels
当您不知道如何更改 factor
的顺序时,总是打印 levels
并查看它们是如何排序的。
使用labs
你可以改变坐标轴和标题:
p = ggplot(melted, aes(x = cat, y = value, fill = gender))
p + geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ person) +
scale_fill_manual(values = c("orangered","dodgerblue2")) +
theme(panel.background = element_rect(fill = 'white')) +
labs(title="abc title", x="aaa",y="bbb")
#+theme(plot.title = element_text(hjust = 0.5)) # center title