ggplot2 - I have this piece of code, and I would like to put the plots in the same plot in R (like the hold on; function in matlab) -
library(ggplot2) qplot(womanbed, womanage) ggplot(data.frame(womanbed, womanage), aes(womanbed,womanage)) + geom_point(color='#56b4e9') qplot(manbed, manage) ggplot(data.frame(manbed, manage), aes(manbed,manage)) + geom_point(color='darkblue')`
as can see womanbed,womanage first plot , manbed , manage second plot. these vectors character type (e.g : "24","23"). want both plots @ same figure (so have dots men , women different colors in same x,y axis)
the data have:
womanbed: "01:00" "23:00" "23:30" "00.30" "23:00" "01:00" "23:00" "02:00" "01.50" "02:00" "01:00" "04:00" "01.30" "23:30" "00:00" "00:00" "01:00" "00:00" "00:30" "01:56" "05:30" "23:45" "00:45" "22.40" "02:00" "00:15" "00:30" "23:00" "22:00" "00:00" "22:00" "00:10" "23:00"
manbed: "01:00" "10:30" "00:30" "01:02" "02:00" "03:00" "23:00" "00:00" "23:30" "00:00" "23:40" "23:45" "02:00" "02:00" "12:00" "01:00" "23:30" "01:15" "23:30" "01:00" "01:00" "01.30" "01:30" "01:57" "23:00" "23:00" "00:06" "08:00" "02:00" "22.30" "02:30" "22:15" "00:30" "02:00" "23:30" "23:59" "23:00" "01:00" "01:00" "23:30" "01:30" "23:00" "03:00" "22.30" "01:30" "23:00" "00:15" "23:05" "23:00" "01:45" "01:00" "02:00" "23:00" "00:00" "12:00" "03:00" "23:00" "11:55" "02:00" "03:00" "05:00" "02:00" "11:00" "15:55" "23:59" "02:00" "03:00" "06:00" "22:45" "04:00" "22:30" "00:30" "00:00" "01:45" "01:45" "22.30" "01:00" "01:00" "22.45" "12:00"
manage: "26" "27" "26" "47" "26" "27" "27" "23" "23" "36" "27" "25" "23" "24" "28" "25" "23" "23" "23" "25" "24" "26" "25" "25" "23" "22" "27" "23" "22" "26" "27" "37" "22" "23" "25" "24" "26" "23" "26" "22" "24" "27" "23" "27" "24" "25" "22" "25" "25" "23" "28" "24" "27" "23" "28" "28" "37" "26" "27" "24" "25" "23" "28" "25" "27" "30" "24" "23" "23" "25" "26" "27" "23" "21" "21" "25" "24" "26" "24" "25"
womanage:
"27" "22" "24" "23" "23" "25" "29" "24" "27" "22" "24" "25" "28" "24" "17" "26" "24" "25" "22" "23" "28" "22" "22" "27" "25" "24" "22" "22" "23" "23" "34" "23" "24"
all 4 variables character vectors.
i want plot this:[run code see], has x axis bed times, , y ages ages both woman , man (with different colours each gender)
### load packages # preferred (imo) way handle table library(data.table) # best plotting package library(ggplot2) # time format related stuff library(lubridate) ### prepare data wb <- c("01:00", "23:00", "23:30") mb <- c("01:00", "10:30", "00:30") # convert time scale wb <- hm(wb) mb <- hm(mb) wa <- c("27", "22", "24") ma <- c("26", "27", "26") # convert character numbers wa <- as.numeric(wa) ma <- as.numeric(ma) # create data table dwomen <- data.table(bed = wb, age = wa, gender = rep("women", length(wa))) dmen <- data.table(bed = mb, age = ma, gender = rep("men", length(ma))) # merge women , men datasets 1 dplot <- rbind(dwomen, dmen) ### final plot ggplot(dplot, aes(bed, age, color = gender)) + geom_point(size = 2) + scale_x_time() + labs(x = "hours", y = "age", title = "hours ~ age, per gender") + theme_bw() + scale_color_brewer(palette = "set1", name = "gender")
edit: if there preference other colour palette 1 can use scale_colour_manual
instead of scale_color_brewer
.
Comments
Post a Comment