Добавьте пузырьковую диаграмму к простому графику в R - PullRequest
0 голосов
/ 11 июня 2019

Я строю некоторые простые переменные в 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 a изменился между time 0 и time 1.Лучше всего добавить маленькую пузырьковую диаграмму в правом нижнем углу графика.Что-то вроде следующего графика (но только с отображением pop_a_time_0 и pop_a_time_1):

enter image description here

Возможно ли это?

Ответы [ 2 ]

1 голос
/ 14 июня 2019

С базовой графикой в ​​пакете есть функция 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 для x = 100, учитывая линейную модель.

fit_1 <- lm(sort(b)~sort(a))
fit_0 <- lm(sort(c)~sort(a))
t1 <- predict(fit_1, newdata = data.frame(a = 100))
t0 <- predict(fit_0, newdata = data.frame(a = 100))
t1 <- round(t1)
t0 <- round(t0)

text(80, 65, labels = t1, pos = 3, cex = 0.65)
text(80, 56, labels = t0, pos = 3, cex = 0.65)
text(80, 75, labels = "Population", pos = 3)    # Plot once
text(80, 75, labels = "Population", pos = 3)    # Overplot to make it darker

enter image description here

1 голос
/ 11 июня 2019

Вот решение с использованием ggplot вместо базовой графики.

Я использовал ggforce::geom_circle(), чтобы нарисовать круги, и выполнил вставку в соответствии с здесь .

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...