簡體   English   中英

我有這段代碼,我想將繪圖放在R的同一繪圖中(如hold; matlab中的函數)

[英]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')`

如您所見,女人床是第一位情節,女人床是第二位情節。 所有這些向量都是字符類型(例如:“ 24”,“ 23”)。 我希望兩個圖都在同一圖形上(以便在相同的x,y軸上具有不同顏色的男女點)

我擁有的數據:

女人床:“ 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“

管理:“ 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“

所有4個變量都是字符向量。

我想要一個像這樣的圖:[運行代碼以查看],該圖具有x軸為上床時間,y年齡為男女年齡(每種性別使用不同的顏色)

### 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")

編輯:如果有其他調色板的偏好,可以使用scale_colour_manual而不是scale_color_brewer

ResultPlot

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM