Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the order of values on the x-axis in R

Tags:

r

I am attempting to create a line graph (time vs anxiety) in R with the x-axis ranging from 10am to 6pm. R, however, reorders the values in numeric order and the graph looks vaguely 's-shaped'. Please see below for vectors:

anxiety <- c(1, 1, 2, 2, 3.5, 4, 4.5, 5, 5)
time <- c(10, 11, 12, 1, 2, 3, 4, 5, 6)

plot(time, anxiety, xlab='Time (hour of day)', ylab='Degree of anxiety (estimated)', xaxt='n', main='The Effect of Technology Deprivation on Anxiety', col='blue', type='l', cex.lab=1.5, cex.main=1.5, las=1)

I would prefer that the x-axis values be presented in chronological order (10am, 11am, etc.) and that the graph reflect a near linear pattern of increasing anxiety.

Thanks all.

~Caitlin

like image 483
CaitlinG Avatar asked Dec 03 '25 17:12

CaitlinG


1 Answers

if you are willing to give ggplot2 a try

# data
anxiety <- c(1, 1, 2, 2, 3.5, 4, 4.5, 5, 5)
time <- c(10, 11, 12, 1, 2, 3, 4, 5, 6)
df <- data.frame(anxiety, time)
# order the level of time
df$time <- factor(df$time, order=TRUE, levels=time)

# plot
ggplot(df, aes(x=time, y=anxiety, group=1)) + geom_line()

enter image description here

like image 71
KFB Avatar answered Dec 06 '25 06:12

KFB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!