I have some data that I would like to visualize but I cannot manage to do this..
I have this play sheet:
label <- c("t","x","t","x","t","t","t","x","t","x")
t0 <- c(1:10)
t1 <- c(3:12)
t2 <- c(6:15)
t3 <- c(8:17)
data <- data.frame(t0, t1, t2, t3, label)
The beginning looks like this
t0 t1 t2 t3 label
1 1 3 6 8 t
2 2 4 7 9 x
3 3 5 8 10 t
4 4 6 9 11 x
I would like to plot the values of all samples (rows), as a line against the four time points(t=0,1,2,3). Furthermore I would like to color the lines with label t in one color and the label x in another. So in the play data I would end up with 10 lines of which 6 would be one color and 4 another.
I would appreciate all help! I tried some things using mathplot and ggplot but I am really stuck
Ok, I'll bite.
As mentioned in the comments, your code will not currently work in R. I tried to structure my answer so that it will work with dataframes essentially identical to one you supplied, but I had to make a slight change to the code, which I show below:
label <- c("t","x","t","x","t","t","t","x","t","x")
t0 <- c(1:10)
t1 <- c(3:12)
t2 <- c(6:15)
t3 <- c(8:17)
data <- data.frame(t0, t1, t2, t3, label)
If we're going to use ggplot
, it we need to get your data into what's called long form using the reshape2
package. This occurs during the melt
step. Note that variable
will become our time
column. During the sequence of operations on ndata, we are constantly referring back to the ndata
object. This is so that you can follow the code step-by-step.
We're also going to need some packages, which we'll load now:
# load packages
library(ggplot2)
library(reshape2)
library(dplyr)
data$sample_id <- as.character(rownames(data))
ndata <- melt(data, id = c("sample_id", "label")) # melt "data" into long form
ndata <- arrange(ndata, sample_id) # re-order rows by sample_id. Helpful for viewing dataframe
ndata <- mutate(ndata, variable = gsub("t", "", ndata$variable)) # remove the "t" character
ndata <- rename(ndata, time = variable)
ndata <- mutate(ndata, time = as.numeric(time)) # Make sure that time is numeric
newplot <- ggplot(ndata, aes(x = time, y = value, color = label, group = sample_id)) +
geom_point() +
geom_line()
newplot
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.