以下是我拥有的数据框的子集:
sample <- structure(list(MONTH_DAY = c("1_0", "1_1", "1_10", "1_11", "1_12",
"1_13", "1_14", "1_15", "1_16", "1_17", "1_18", "1_19", "1_2",
"1_20", "1_21", "1_22", "1_23", "1_3", "1_4", "1_5", "1_6", "1_7",
"1_8", "1_9", "2_0", "2_1", "2_10", "2_11", "2_12", "2_13", "2_14",
"2_15", "2_16", "2_17", "2_18", "2_19", "2_2", "2_20", "2_21",
"2_22", "2_23", "2_3", "2_4", "2_5", "2_6", "2_7", "2_8", "2_9",
"3_0", "3_1"), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("9",
"10", "11", "12", "13"), class = "factor"), value = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 51, 18
)), .Names = c("MONTH_DAY", "variable", "value"), row.names = c(NA,
50L), class = "data.frame")
我正在使用ggplot2
绘制图形,x轴的格式为MONTH_DAYOFMONTH,即1_13表示同月的1月和13日,y轴表示计数(value
列为数据框)。当我绘制数据时,使用命令:
ggplot(sampleData, aes(x=MONTH_DAY, y=value, colour=variable, group=variable)) + `geom_line() + theme(axis.text.x=element_text(angle=90, size=4, hjust=-0.2, vjust=0.5)) + scale_colour_discrete("Months")`
x轴未排序,显示的起点是x轴值为1_0, 1_1, 1_10, 1_11
...而不是1_0, 1_1, 1_2, 1_3
。
如何对这些值进行排序,以便绘图显示数据是我希望看到的顺序?
答案 0 :(得分:4)
从 gtools 包中尝试mixedsort
:
library(gtools)
sample$MONTH_DAY <-
with(sample, ordered(MONTH_DAY, levels=mixedsort(MONTH_DAY)))
## Try your plotting code here
说明它的作用:
MONTH_DAY = c("1_0", "1_1", "1_10", "1_11", "1_12",
"1_13", "1_14", "1_15", "1_16", "1_17", "1_18", "1_19", "1_2",
"1_20", "1_21", "1_22", "1_23", "1_3", "1_4", "1_5", "1_6", "1_7",
"1_8", "1_9", "2_0", "2_1", "2_10", "2_11", "2_12", "2_13", "2_14",
"2_15", "2_16", "2_17", "2_18", "2_19", "2_2", "2_20", "2_21",
"2_22", "2_23", "2_3", "2_4", "2_5", "2_6", "2_7", "2_8", "2_9",
"3_0", "3_1")
head(sort(MONTH_DAY), 10)
# [1] "1_0" "1_1" "1_10" "1_11" "1_12" "1_13" "1_14" "1_15" "1_16" "1_17"
head(mixedsort(MONTH_DAY), 10)
# [1] "1_0" "1_1" "1_2" "1_3" "1_4" "1_5" "1_6" "1_7" "1_8" "1_9"
答案 1 :(得分:1)
我只是将它变成一个日期并将其绘制成这样(在您给出的数据中注意,所有值栏两个为NA
所以我使用runif(50 , max = 50)
制作了一些值......
sampleData$MONTH_DAY <- as.Date( sampleData$MONTH_DAY , format = "%m_%d" )
ggplot(sampleData, aes(x=MONTH_DAY, y=value, colour=variable, group=variable)) +
geom_line() +
theme(axis.text.x=element_text(angle=90, size=4, hjust=-0.2, vjust=0.5)) +
scale_colour_discrete("Months")