Note that there are some explanatory texts on larger screens.

plurals
  1. POReordering a character column values for plotting
    text
    copied!<p>Following is a subset of the dataframe I have:</p> <pre><code>sample &lt;- 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") </code></pre> <p>I am plotting a graph using <code>ggplot2</code> with x-axis being of the format MONTH_DAYOFMONTH i.e. 1_13 means it January and 13th of the same month, y axis indicating a count (which is <code>value</code> column in the dataframe). When I plot the data, using the command: </p> <pre><code>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")` </code></pre> <p>the x-axis is not sorted and displays the starting with the x-axis values being <code>1_0, 1_1, 1_10, 1_11</code> ... instead of <code>1_0, 1_1, 1_2, 1_3</code>.</p> <p>How can I sort such values so that the plot shows the data is the order I would like to see? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload