[R] Plot data in sequence

Sarah Goslee @@r@h@go@|ee @end|ng |rom gm@||@com
Wed Apr 4 18:07:55 CEST 2018


Hi,

Thanks for the reproducible example.

Looking at
str(datn)
would give you a clue.
STATUS is a factor because it contains character values. Factor levels
by default are alphabetical with numbers first, but you can change
those.

> str(datn)
'data.frame': 36 obs. of  3 variables:
 $ LEVEL : Factor w/ 4 levels "DIPLOMA","MATRIC",..: 1 1 1 1 1 1 1 1 1 4 ...
 $ STATUS: Factor w/ 9 levels "11121","11122",..: 9 1 2 3 4 5 6 7 8 9 ...
 $ CGPA  : num  3.32 2.91 2.9 2.89 2.89 2.93 2.96 2.76 2.73 2.77 ...

> levels(datn$STATUS)
[1] "11121" "11122" "12131" "12132" "13141" "13142" "14151" "14152" "ENTRY"
> datn$STATUS <- factor(datn$STATUS, levels=c("ENTRY", "11121", "11122", "12131", "12132", "13141", "13142", "14151", "14152"))
> levels(datn$STATUS)
[1] "ENTRY" "11121" "11122" "12131" "12132" "13141" "13142" "14151" "14152"

Reorder the levels to your desired outcome, and then plot the data again.

Sarah

On Wed, Apr 4, 2018 at 11:10 AM, roslinazairimah zakaria
<roslinaump using gmail.com> wrote:
> Hi r-users,
>
> I would like to draw line plots. However, the plot starts from 11121 data
> and plot data ENTRY last in the plot.
>
> Here is the code and data.
>
> datn <- read.table(header=TRUE, text='
> LEVEL   STATUS      CGPA
> DIPLOMA ENTRY 3.32
> DIPLOMA 11121 2.91
> DIPLOMA 11122 2.90
> DIPLOMA 12131 2.89
> DIPLOMA 12132 2.89
> DIPLOMA 13141 2.93
> DIPLOMA 13142 2.96
> DIPLOMA 14151 2.76
> DIPLOMA 14152 2.73
> STPM ENTRY 2.77
> STPM 11121 3.26
> STPM 11122 3.30
> STPM 12131 3.28
> STPM 12132 3.27
> STPM 13141 3.28
> STPM 13142 3.28
> STPM 14151 3.30
> STPM 14152 3.31
> MATRIC ENTRY 2.93
> MATRIC 11121 2.69
> MATRIC 11122 2.79
> MATRIC 12131 2.81
> MATRIC 12132 2.80
> MATRIC 13141 2.77
> MATRIC 13142 2.76
> MATRIC 14151 2.78
> MATRIC 14152 2.82
> STAM ENTRY 2.00
> STAM 11121 3.05
> STAM 11122 2.99
> STAM 12131 2.97
> STAM 12132 2.89
> STAM 13141 2.84
> STAM 13142 2.79
> STAM 14151 2.79
> STAM 14152 2.77
> ')
>
> ggplot(data=datn, aes(x=STATUS, y=CGPA, group=LEVEL,
> colour=LEVEL),cex.axis=0.2, ylim=c(0,4)) +
>     geom_line(size=1.3) +
>     geom_point()+
>     #guides(fill=FALSE) +
>     theme(legend.title=element_blank(),legend.position=c(.3,
> .3),axis.text.x = element_text(size=8),axis.text=element_text(size=10))
>
> Thank you very much.
>
> --
> *Roslinazairimah Zakaria*
> *Tel: +609-5492370; Fax. No.+609-5492766*
>


-- 
Sarah Goslee
http://www.functionaldiversity.org




More information about the R-help mailing list