簡體   English   中英

R兩幅圖疊加

[英]R two plots overlay

我需要有關R中情節的幫助。

我得到一個帶有源“ data_small”的圖。 現在,我有第二個來源“ data_big”,我想將其覆蓋在同一圖中。

這兩個來源都有“ risk_datatheft_likelihood”和“ risk_datatheft_damage”列

任何想法? 第二個來源應以其他顏色顯示。

ggplot(data_small, aes(risk_datatheft_likelihood, risk_datatheft_damage)) + 
  geom_jitter(color="blue") + 
  labs(x = "damage", y = "likelihood",
       title = "Risk Map", subtitle = "Datatheft") + 
        theme_classic() +
        theme(legend.position="bottom") + 
        geom_hline(yintercept = 0.5, color="red") + 
        geom_hline(yintercept = 1.5) + 
        geom_hline(yintercept = 2.5) + 
        geom_hline(yintercept = 3.5) + 
        geom_hline(yintercept = 4.5) + 
        geom_vline(xintercept = 0.5, color="red") + 
        geom_vline(xintercept = 1.5) + 
        geom_vline(xintercept = 2.5) + 
        geom_vline(xintercept = 3.5) + 
        geom_vline(xintercept = 4.5)

data_big.csv
risk_datatheft_likelihood;risk_datatheft_damage
B;3
B;2
C;4
A;1
D;5

data_small.csv
risk_datatheft_likelihood;risk_datatheft_damage
C;4
A;2
B;3
C;4
D;1

我認為您可能只想將兩個數據集的行與一個新變量綁定以區別它們,如下所示:

bind_rows(mutate(data_big, size="big"),
          mutate(data_small, size="small"))

這將產生如下所示的小標題:

# A tibble: 10 x 3
   risk_datatheft_likelihood risk_datatheft_damage size 
   <chr>                                     <int> <chr>
 1 B                                             3 big  
 2 B                                             2 big  
 3 C                                             4 big  
 4 A                                             1 big  
 5 D                                             5 big  
 6 C                                             4 small
 7 A                                             2 small
 8 B                                             3 small
 9 C                                             4 small
10 D                                             1 small

現在,您可以將size用作顏色美觀度:

bind_rows(mutate(data_big, size="big"),
          mutate(data_small, size="small")) %>%
    ggplot(aes(risk_datatheft_likelihood, risk_datatheft_damage)) +
    geom_jitter(aes(col=size))

這是完整的代碼,后跟成績單,因為您在重現我的解決方案時遇到了麻煩。

完整代碼:

library(tidyverse)

data_big <- read_delim("data_big.csv", delim=";")
data_small <- read_delim("data_small.csv", delim=";")

# run the plot using one multiline command:
bind_rows(mutate(data_big, size="big"),
          mutate(data_small, size="small")) %>%
    ggplot(aes(risk_datatheft_likelihood, risk_datatheft_damage)) +
    geom_jitter(aes(col=size))

# alternatively, save the combined data first
data_combined <- bind_rows(mutate(data_big, size="big"),
                           mutate(data_small, size="small"))

# and run the plot separately
ggplot(data_combined,
       aes(risk_datatheft_likelihood, risk_datatheft_damage)) +
    geom_jitter(aes(col=size)) +
    labs(x = "damage", y = "likelihood",
         title = "Risk Map", subtitle = "Datatheft") +
    theme_classic() +
    theme(legend.position="bottom") +
    geom_hline(yintercept = 0.5, color="red") +
    geom_hline(yintercept = 1.5) +
    geom_hline(yintercept = 2.5) +
    geom_hline(yintercept = 3.5) +
    geom_hline(yintercept = 4.5) +
    geom_vline(xintercept = 0.5, color="red") +
    geom_vline(xintercept = 1.5) +
    geom_vline(xintercept = 2.5) +
    geom_vline(xintercept = 3.5) +
    geom_vline(xintercept = 4.5)

和完整的成績單:

> library(tidyverse)
[... stuff about loading tidyverse ...]
> 
> data_big <- read_delim("data_big.csv", delim=";")
Parsed with column specification:
cols(
  risk_datatheft_likelihood = col_character(),
  risk_datatheft_damage = col_integer()
)
> data_small <- read_delim("data_small.csv", delim=";")
Parsed with column specification:
cols(
  risk_datatheft_likelihood = col_character(),
  risk_datatheft_damage = col_integer()
)
> 
> # run the plot using one multiline command:
> bind_rows(mutate(data_big, size="big"),
+           mutate(data_small, size="small")) %>%
+     ggplot(aes(risk_datatheft_likelihood, risk_datatheft_damage)) +
+     geom_jitter(aes(col=size))
> 
> # alternatively, save the combined data first
> data_combined <- bind_rows(mutate(data_big, size="big"),
+                            mutate(data_small, size="small"))
> 
> # and run the plot separately
> ggplot(data_combined,
+        aes(risk_datatheft_likelihood, risk_datatheft_damage)) +
+     geom_jitter(aes(col=size)) +
+     labs(x = "damage", y = "likelihood",
+          title = "Risk Map", subtitle = "Datatheft") +
+     theme_classic() +
+     theme(legend.position="bottom") +
+     geom_hline(yintercept = 0.5, color="red") +
+     geom_hline(yintercept = 1.5) +
+     geom_hline(yintercept = 2.5) +
+     geom_hline(yintercept = 3.5) +
+     geom_hline(yintercept = 4.5) +
+     geom_vline(xintercept = 0.5, color="red") +
+     geom_vline(xintercept = 1.5) +
+     geom_vline(xintercept = 2.5) +
+     geom_vline(xintercept = 3.5) +
+     geom_vline(xintercept = 4.5)
> 

非常感謝您的幫助! 我不是很確定,但是這種結合也可以:

bind_rows(mutate(data_big, size="big"),
          mutate(data_small, size="small")) %>%
  ggplot(aes(risk_datatheft_likelihood, risk_datatheft_damage)) +
  geom_jitter(aes(col=size)) +
  labs(x = "risk_datatheft_likelihood", y = "risk_datatheft_damage",
       title = "Risk Map", subtitle = "Risiko: Datatheft") +
  theme_classic() +
  theme(legend.position="bottom") +
  geom_hline(yintercept = 0.5, color="red") +
  geom_hline(yintercept = 1.5) +
  geom_hline(yintercept = 2.5) +
  geom_hline(yintercept = 3.5) +
  geom_hline(yintercept = 4.5) +
  geom_vline(xintercept = 0.5, color="red") +
  geom_vline(xintercept = 1.5) +
  geom_vline(xintercept = 2.5) +
  geom_vline(xintercept = 3.5) +
  geom_vline(xintercept = 4.5)

暫無
暫無

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

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