简体   繁体   中英

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). 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 .

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:

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) ...

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")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM