user2300940
user2300940

Reputation: 2385

sort values in column and plot ordered values

How do I order the numbers in the second column so that the lowest values come first? Please look at my example data and my code.

> TumorSize
   Tumour.size
1           15
2         12,5
3           10
4            4
5            8
6            9
7           12
8            7
9            5
10           2
11         4,5
12           8
13          12

Tried this, but it does not work for values above 10:

TumorSize$Tumour.size <- with(TumorSize, TumorSize[order(Tumour.size), 1])

ggplot(TumorSize, aes(Tumour.size)) + geom_dotplot() + theme_bw() +
  labs(x="Tumor Size", y="Frequency") +
  theme(axis.text.x = element_text( angle = 90))

Upvotes: 0

Views: 41

Answers (1)

Konrad
Konrad

Reputation: 18585

You have to clean up your values:

dta <- read.table(textConnection("Tumour.size
                            1           15
                            2         12,5
                            3           10
                            4            4
                            5            8
                            6            9
                            7           12
                            8            7
                            9            5
                            10           2
                            11         4,5
                            12           8
                            13          12"),
              header=TRUE)

# to num accounting for ,
dta$Tumour.size <- as.numeric(sub(",",".",dta$Tumour.size))

Then you can conveinetly sort your data, like in the syntax below that uses dplyr:

require(dplyr)
dta_ord <- arrange(dta, Tumour.size)

With respect to the chart, you don't actually have to pass the sorted data frame if you tidy up your values:

require(ggplot2)
ggplot(dta, aes(Tumour.size)) + geom_dotplot() + theme_bw() +
    labs(x="Tumor Size", y="Frequency") +
    theme(axis.text.x = element_text( angle = 90))

would give you the following chart:

sample chart

Upvotes: 2

Related Questions