简体   繁体   中英

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 you see womanbed,womanage is the first plot and manbed and manage is the second plot. All these vectors are character type (eg : "24","23"). I want both plots to be at the same figure (so to have dots for men and women with different colors in the same x,y axis)

The data that I 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 the 4 variables are character vectors.

I want a plot like this:[run the code to see], that has x axis for the bed times, and y ages for the ages for both woman and man (with different colours for each gender)

### Load packages
# Preferred (imo) way to 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 to time scale
wb <- hm(wb)
mb <- hm(mb)
wa <- c("27", "22", "24")
ma <- c("26", "27", "26")
# Convert character to 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 and men datasets into one
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 is preference for other colour palette one can use scale_colour_manual instead of scale_color_brewer .

ResultPlot

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM