博客地址:https://www.jianshu.com/u/619b87e54936
目录
R语言之可视化①误差棒
R语言之可视化②点图
R语言之可视化③点图续
R语言之可视化④点韦恩图upsetR
R语言之可视化⑤R图形系统
R语言之可视化⑥R图形系统续
R语言之可视化⑦easyGgplot2散点图
R语言之可视化⑧easyGgplot2散点图续
R语言之可视化⑨火山图
R语言之可视化⑩坐标系统
R语言之可视化①①热图绘制heatmap
R语言之可视化①②热图绘制2
R语言之可视化①③散点图 拟合曲线
R语言之可视化①④一页多图(1)
R语言之可视化①⑤ROC曲线
R语言之可视化①⑥一页多图(2)
R语言之可视化①⑦调色板
R语言之可视化①⑧子图组合patchwork包
R语言之可视化①⑨之ggplot2中的图例修改
R语言之可视化(20)之geom_label()和geom_text()
R语言之可视化(21)令人眼前一亮的颜色包
R语言之可视化(22)绘制堆积条形图
R语言之可视化(23)高亮某一元素
R语言之可视化(24)生成带P值得箱线图
R语言之可视化(25)绘制相关图(ggcorr包)
R语言之可视化(26)ggplot2绘制饼图
R语言之可视化(27)通过R语言制作BBC风格的精美图片
R语言之可视化(28)蜜蜂图
R语言之可视化(29)如何更改ggplot2中堆积条形图中的堆积顺序
问题:如何控制由ggplot2创建的堆积条的堆积顺序。
原始图表
代码语言:javascript复制library(reshape2)
library(ggplot2)
ra.melt <- melt(ra)
p <- ggplot(ra.melt, aes(x = variable, y = value))
p geom_bar(aes(fill = quality), stat = "identity")
labs(x = "group", y = "percentage (%)")
levels(ra.melt$quality)
[1] "A" "B" "C" "D" "E" "F"
[7] "G" "H" "I" "J" "K" "L"
[13] "M" "other"
数据
代码语言:javascript复制ra <- structure(list(quality = structure(c(2L, 6L, 13L, 1L, 7L, 5L,
10L, 4L, 3L, 9L, 11L, 12L, 8L, 14L), .Label = c("A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "other"), class = "factor"), GY = c(25.5932389, 13.9567819, 11.7333, 8.6439, 7.8351008,
8.29, 6.059643, 3.8608517, 4.3277, 3.1710598, 1.2021144,
1.0986329, 0.9369271, 3.2907496), TH = c(23.0215577, 15.8975387,
10.5075, 7.8888, 8.8121504, 8.276, 6.4356378, 3.9147685,
5.4838, 3.9339386, 1.3921565, 0.742406, 0.8795013, 2.8142444
), SZ = c(21.217152, 16.0363831, 11.154, 8.5094, 7.945884,
10.2717, 5.4962929, 4.1381946, 5.1141, 3.4053352, 1.8472827,
1.0648934, 0.9792861, 2.820096), DZ = c(23.7548859, 14.4732136,
12.1292, 10.4722, 8.0838209, 7.0575, 5.144598, 4.174118,
5.4004, 3.1940577, 1.4427867, 1.1323864, 0.8986281, 2.6422047
), FP = c(19.9848602, 16.420735, 12.9683, 11.2059, 7.5143706,
6.8747, 5.8653262, 5.5412901, 4.182, 3.5347168, 1.3838249,
0.8811604, 0.8585596, 2.7842562)), .Names = c("quality",
"GY", "TH", "SZ", "DZ", "FP"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14"))
解决方案
堆叠在数据框的原始顺序中
代码语言:javascript复制ra.melt$quality <- factor(ra.melt$quality, levels = ra$quality)
p <- ggplot(ra.melt, aes(x = variable, y = value))
p geom_bar(aes(fill = quality), stat = "identity")
labs(x = "group", y = "percentage (%)")
factor(ra.melt$quality, levels = ra$quality)
[1] B F M A G E J D
[9] C I K L H other B F
[17] M A G E J D C I
[25] K L H other B F M A
[33] G E J D C I K L
[41] H other B F M A G E
[49] J D C I K L H other
[57] B F M A G E J D
[65] C I K L H other
Levels: B F M A G E J D C I K L H other
颠倒堆叠顺序
代码语言:javascript复制ra.melt$quality <- factor(ra.melt$quality, levels = rev(ra$quality))
p <- ggplot(ra.melt, aes(x = variable, y = value))
p geom_bar(aes(fill = quality), stat = "identity")
labs(x = "group", y = "percentage (%)")
代码语言:javascript复制factor(ra.melt$quality, levels = rev(ra$quality))
代码语言:javascript复制 [1] B F M A G E J D
[9] C I K L H other B F
[17] M A G E J D C I
[25] K L H other B F M A
[33] G E J D C I K L
[41] H other B F M A G E
[49] J D C I K L H other
[57] B F M A G E J D
[65] C I K L H other
Levels: other H L K I C D J E G A M F B
代码语言:javascript复制library(forcats)
p <- ggplot(ra.melt, aes(x = variable, y = value))
p geom_bar(aes(fill = fct_rev(quality)), stat = "identity")
labs(x = "group", y = "percentage (%)")
如果我们想颠倒堆叠顺序但同时保留图例的顺序,则使用参数* position_stack(reverse = TRUE)*
代码语言:javascript复制p <- ggplot(ra.melt, aes(x = variable, y = value))
p geom_bar(aes(fill = fct_rev(quality)), stat = "identity",
position = position_stack(reverse = TRUE))
labs(x = "group", y = "percentage (%)")