[英]R two plots overlay
I need help with a plot in R. 我需要有关R中情节的帮助。
I get a plot with a source "data_small". 我得到一个带有源“ data_small”的图。 I now have a second source "data_big" which I want to overlay in the same plot.
现在,我有第二个来源“ data_big”,我想将其覆盖在同一图中。
Both sources have the columns "risk_datatheft_likelihood" and "risk_datatheft_damage" 这两个来源都有“ risk_datatheft_likelihood”和“ risk_datatheft_damage”列
Any idea? 任何想法? the second source should be displayed in a different color.
第二个来源应以其他颜色显示。
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
I think you probably just want to bind the rows of the two datasets with a new variable to distinguish them, like so: 我认为您可能只想将两个数据集的行与一个新变量绑定以区别它们,如下所示:
bind_rows(mutate(data_big, size="big"),
mutate(data_small, size="small"))
This produces a tibble that looks like: 这将产生如下所示的小标题:
# 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
Now, you can use size
as the color aesthetic: 现在,您可以将
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))
Here's the full code followed by a transcript, since you were having trouble reproducing my solution. 这是完整的代码,后跟成绩单,因为您在重现我的解决方案时遇到了麻烦。
The full code: 完整代码:
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)
and the complete transcript: 和完整的成绩单:
> 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)
>
thank you very much for your help! 非常感谢您的帮助! I am not shure, but this cobination works also:
我不是很确定,但是这种结合也可以:
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.