我需要一些有关整理图的组和标签的帮助,到目前为止,这是我的代码:
alphabet <- c("A", "A", "A", "A", "A", "A",
"C", "C", "C", "C", "C", "C", "B", "B", "B", "B", "B","B")
ammount <- c("1", "2", "3", "4", "5", "6", "1", "2", "3", "4", "5", "6", "1", "2", "3", "4", "5", "6")
percentage <- c(30.3, 25.8, 21.2, 9.1, 9.1, 4.5, 23.1, 21.5, 9.2, 23.1, 13.9, 9.2, 27.8, 17.5, 14.3, 20.6, 14.3, 5.6)
DataFrame.test <- data.frame(alphabet, ammount, percentage)
DataFrame.test
library(ggplot2)
data <- read.table(
header=TRUE, text='alphabet ammount percentage
1 A 1 30.3
2 A 2 25.8
3 A 3 21.2
4 A 4 9.1
5 A 5 9.1
6 A 6 4.5
7 C 1 23.1
8 C 2 21.5
9 C 3 9.2
10 C 4 23.1
11 C 5 13.9
12 C 6 9.2
13 B 1 27.8
14 B 2 17.5
15 B 3 14.3
16 B 4 20.6
17 B 5 14.3
18 B 6 5.6')
bargraph <- ggplot(data, aes(x=ammount, y=percentage, fill = alphabet)) +
geom_bar(stat="identity", position = position_dodge(0.70), width = 0.65) +
scale_fill_brewer(palette = "Set1") + labs(x = "ammount", y= "percentage (%)")
bargraph + theme_minimal()
给我下图:
“>
现在,这些组按字母顺序排列,但我希望它们按顺序排列(A,C,B)。另外,我的组中只有2、4和6的标签,但是我也想要1、3和5的标签。
非常感谢您的帮助。
我需要一些关于整理图的组和标签的帮助,到目前为止,这是我的代码:字母
您可以这样修改数据集:
您需要将字母和ammount变量转换为因子变量,并为字母变量指定顺序。像这样: