[英]Reordering a character column values for plotting
Following is a subset of the dataframe I have: 以下是我拥有的数据框的子集:
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")
I am plotting a graph using ggplot2
with x-axis being of the format MONTH_DAYOFMONTH ie 1_13 means it January and 13th of the same month, y axis indicating a count (which is value
column in the dataframe). 我正在使用ggplot2
绘制图形,x轴的格式为MONTH_DAYOFMONTH,即1_13表示同月的1月和13日,y轴表示计数(这是数据帧中的value
列)。 When I plot the data, using the command: 当我绘制数据时,使用以下命令:
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")`
the x-axis is not sorted and displays the starting with the x-axis values being 1_0, 1_1, 1_10, 1_11
... instead of 1_0, 1_1, 1_2, 1_3
. x轴未排序,并显示x轴值为1_0, 1_1, 1_10, 1_11
...而不是1_0, 1_1, 1_2, 1_3
。
How can I sort such values so that the plot shows the data is the order I would like to see? 如何对这些值进行排序,以使绘图显示数据是我希望看到的顺序?
Try mixedsort
, from the gtools package: 尝试mixedsort
,从gtools包:
library(gtools)
sample$MONTH_DAY <-
with(sample, ordered(MONTH_DAY, levels=mixedsort(MONTH_DAY)))
## Try your plotting code here
To illustrate what it does: 为了说明它的作用:
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"
I would just turn it into a date and plot it like this (NB in the data you gave all values bar two were NA
so I made some values using runif(50 , max = 50)
... 我只是将其转换为一个日期并像这样绘制它(在您给所有值栏的数据中,NB注意均为NA
所以我使用runif(50 , max = 50)
来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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.