[英]How to change order of bars in barplot?
I need to change the order of the bars in my barplot. 我需要更改条形图中条形的顺序。 The default is alphabetical which I do not want.
默认值是我不希望使用的字母顺序。 I need the order to be "Elementary School", "Middle School", "High School".
我需要顺序为“小学”,“中学”,“高中”。
colors <- c("cornflowerblue","mediumpurple1","coral2", "azure4")
colorsleg <-c("cornflowerblue","mediumpurple1","coral2", "azure4")
mytable <- table(century$race, century$type)
mytable2 <- prop.table(mytable, 2) #changes counts to percentages#
M <- c(
"Elementary
School",
"High
School",
"Middle
School")
par(mar=c(5, 6, 4.1, 2.1)) #THIS CHANGES THE GRAPHS MARGINS TO MAKE
#ROOM FOR LONG Y LABELS. default margin sizes are mar=c(5.1, 4.1, 4.1, 2.1)
#
barplot(mytable2,
col=colors,
border = NA,
ylim = range(0,3),
xlim = range(0,1),
# #THIS GETS RID OF Y AXIS LINE#
family="Arial",
horiz = T, names.arg= M,
las=1)`
`
I need the order to Elementary School, Middle School, High School.
Before the table
step, if we change the 'type' column to factor
with levels
specified as values in 'M' 在之前
table
的步骤,如果我们改变“类型”一栏factor
与levels
的“M”指定为值
century$type <- factor(century$type, levels = M)
where, 哪里,
M <- c("Elementary School", "Middle School", "High School")
and then do the table
and prop.table
step 然后做
table
和prop.table
步骤
mytable <- table(century$race, century$type)
mytable2 <- prop.table(mytable, 2)
and plot the barplot 并绘制小图
par(mar=c(5.5, 8.5, 5.1, 2.1))
barplot(mytable2,
col=colors,
border = NA,
ylim = range(0,3),
xlim = range(0,1),
family="Arial",
horiz = TRUE, names.arg= M,
las=1)
century <- structure(list(race = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L), .Label = c("F", "M"), class = "factor"), type = structure(c(3L,
2L, 3L, 3L, 3L, 1L, 2L, 3L, 1L, 2L), .Label = c("Elementary School",
"High School", "Middle School"), class = "factor")),
class = "data.frame", row.names = c(NA, -10L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.