Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

r - How do I order the x axis on ggplot2 numerically for two sets of data?

I'm trying to plot two lines onto one graph using "Time" on the x axis, and BP and n01 on the y-axis. My whole table goes from T1:Tx and then T100 to T109/Tx. When I try to plot the points, it goes from T1, T10, T100, T101, T2,T20,T21, etc. What I would like on my x-axisT1, T2, T3, ... etc until T109.

I can do it as a consecutive graph (e.g. time points T1:T109 for one line, but then it restarts the next one at T1 after T109) The whole point of having them time stamped is to be able to see what happens simultaneously.

If I change time to as.numeric it tells me NAs introduced by coercion.

converting to Integers ruins my order..

Is the only way to do this by creating a vector of the values of T1:T109 int he order I want?

 P Experiment FilePeriod          n01          n02         n03     BP01   BP02   Rates time
P01 Exp1   File01 Baseli…        13.7         91.5        19.3     91.6    144.    140   T1    
P01 Exp2   File01 Baseli…        13.3         93.7        18.8     92.3    144.    140   T2    
P01 Exp3   File01 Baseli…        12.5         88.3        18.0     93.2    143.    140   T3    
P01 Exp4   File01 140             3.08        59.3        17.6     83.7     98.8   140   T4    
P01 Exp5   File01 140             3.23        67.1        18.4     84.6    101.    140   T5    
P01 Exp6   File01 140             3.31        64.0        19.5     85.0    101.    140   T6  
P02 Exp1   File02 140             3.08        59.3        17.6     83.7     98.8   140   T4    
P02 Exp2   File02 140             3.23        67.1        18.4     84.6    101.    140   T5    
P02 Exp3   File02 140             3.31        64.0        19.5     85.0    101.    140   T6   

This is what I've tried so far...

df$time <- str_sort(df$time, numeric=T)
#works in isolation

df$time <- factor(df$time, levels = df$time)


df$time  <- factor(df$time, as.character(df$time)
#no change to graph

df <- df %>%
  mutate(time = as_factor(time))
# organised my time, but took it out of order with files and experiment

df$time <-gtools::mixedsort(df$time)
#Works for df, but when i plot on graph it goes back to being out of order

g01 <- ggplot(df, aes(x=time, color=File))+
  geom_point(aes(y=log(BP01)), shape=16)+
  geom_point(aes(y=n01), shape = 4)

Also tried adding and removing the scale_x_discrete. I've tried inputting variations of the above attempts into the x-axis. e.g.

ggplot(VTFI010_SLOW, aes(x=factor(VTFI010_SLOW$q_VT_q, as.character(VTFI010_SLOW$q_VT_q)), color=File))+
+   geom_point(aes(y=log(MAP_Mean)), shape=16)+
+   geom_point(aes(y=Laser1_Magic), shape = 4

and

df$time <- factor(df$time, levels = df$time[order(df$Experiment)])

This results in Error in `levels<-`(`*tmp*`, value = as.character(levels)) : factor level [13] is duplicated


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe this is what you are looking for. You could convert your time variable to an ordered factor using df$time <- factor(df$time, levels = paste0("T", 1:109)):

df$time <- factor(df$time, levels = paste0("T", 1:109))

library(ggplot2)

ggplot(df, aes(x = time, color = File)) +
  geom_point(aes(y = log(BP01)), shape = 16) +
  geom_point(aes(y = n01), shape = 4)

DATA

df <- structure(list(P = c(
  "P01", "P01", "P01", "P01", "P01", "P01",
  "P02", "P02", "P02"
), Experiment = c(
  "Exp1", "Exp2", "Exp3",
  "Exp4", "Exp5", "Exp6", "Exp1", "Exp2", "Exp3"
), File = c(
  "File01",
  "File01", "File01", "File01", "File01", "File01", "File02", "File02",
  "File02"
), Period = c(
  "Baseli…", "Baseli…", "Baseli…",
  "140", "140", "140", "140", "140", "140"
), n01 = c(
  13.7, 13.3,
  12.5, 3.08, 3.23, 3.31, 3.08, 3.23, 3.31
), n02 = c(
  91.5, 93.7,
  88.3, 59.3, 67.1, 64, 59.3, 67.1, 64
), n03 = c(
  19.3, 18.8, 18,
  17.6, 18.4, 19.5, 17.6, 18.4, 19.5
), BP01 = c(
  91.6, 92.3, 93.2,
  83.7, 84.6, 85, 83.7, 84.6, 85
), BP02 = c(
  144, 144, 143, 98.8,
  101, 101, 98.8, 101, 101
), Rates = c(
  140L, 140L, 140L, 140L,
  140L, 140L, 140L, 140L, 140L
), time = c(
  "T1", "T2", "T3", "T4",
  "T5", "T6", "T4", "T5", "T6"
)), class = "data.frame", row.names = c(
  NA,
  -9L
))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...