Plot data in sequence

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Plot data in sequence

roslinazairimah zakaria
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*

*Email: [hidden email] <[hidden email]>;
[hidden email] <[hidden email]>*
Faculty of Industrial Sciences & Technology
University Malaysia Pahang
Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot data in sequence

Sarah Goslee
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
<[hidden email]> 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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot data in sequence

roslinazairimah zakaria
Hi Sarah,

It works beautifully. Thank you very much.

On Thu, Apr 5, 2018 at 12:07 AM, Sarah Goslee <[hidden email]>
wrote:

> 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
> <[hidden email]> 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
>



--
*Roslinazairimah Zakaria*
*Tel: +609-5492370; Fax. No.+609-5492766*

*Email: [hidden email] <[hidden email]>;
[hidden email] <[hidden email]>*
Faculty of Industrial Sciences & Technology
University Malaysia Pahang
Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.