在R中的简单图表中添加气泡图

时间:2019-06-11 15:43:26

标签: r plot

我正在R中绘制一些简单的变量,例如:

a<- runif(1000, 1, 100)
b<- runif(1000, 30, 200)
c<- runif(1000, 45, 160)
plot(sort(a),sort(b), xlab = "fraction of population a", ylab = "something happening to population a")
points(sort(a), sort(c), col="red")
legend("topleft", legend = c("time 0", "time 1"), pch=1, lwd=2.5,  col = c("black","red"))

这给出了以下图解: enter image description here

现在,我想补充一点信息,以确认我的population atime 0time 1之间进行了更改。最好将其添加为图表右下角的小气泡图。类似于下图(但仅显示pop_a_time_0pop_a_time_1):

enter image description here

这可能吗?

2 个答案:

答案 0 :(得分:1)

这是使用ggplot代替基本图形的解决方案。

我用ggforce::geom_circle()画了圆圈,并按照here进行了插入。

library(dplyr)
library(ggforce)
library(ggplot2)

ggplot() + 
  geom_circle(aes(x0 = 0, y0 = 0:2, r = 1:3)) + 
  geom_text(aes(label = c("3", "20", "40m", "Population"), x = 0, y = c(1.5, 3.5, 5.5, 6.5))) + 
  coord_equal() + 
  theme_void() -> inset_plot

tibble(x = rep(sort(runif(1000, 1, 100)), 2), 
       time = rep(c("time 0", "time 1"), each = 1000), 
       y = c(sort(runif(1000, 30, 200)), sort(runif(1000, 45, 160)))) %>% 
  ggplot(aes(x = x, y = y, color = time)) + 
  geom_point() + 
  labs(x = "fraction of popuation a", 
       y = "something happend to popuation a") + 
  theme(legend.position = c(.05, .92)) + 
  annotation_custom(ggplotGrob(inset_plot), xmin = 70, xmax = 120, ymin = 25, ymax = 75)

答案 1 :(得分:1)

对于基本图形,软件包draw.circle中有函数plotrix
我将首先重做问题中的图形。

library(plotrix)

set.seed(1234)    # Make the results reproducible

a <- runif(1000, 1, 100)
b <- runif(1000, 30, 200)
c <- runif(1000, 45, 160)

plot(sort(a),sort(b), xlab = "fraction of population a", ylab = "something happening to population a")
points(sort(a), sort(c), col="red")
legend("topleft", legend = c("time 0", "time 1"), pch=1, lwd=2.5,  col = c("black","red"))

现在是圈子。

draw.circle(x = 80, y = 50, radius = 12, border = "black")
draw.circle(x = 80, y = 46, radius = 9, border = "red")

最后是标签。由于存在数字,因此在给出线性模型的情况下,我计算了y的{​​{1}}轴的预测值。

x = 100

enter image description here