Как вы строите одно распределение поверх другого в R? - PullRequest
2 голосов
/ 23 марта 2020

Я очень часто сталкиваюсь с графиками такого типа в научных c статьях: пример

Эти два графика сделаны отдельно и объединены в ggplot с помощью сетки?

Ответы [ 2 ]

1 голос
/ 23 марта 2020

Добро пожаловать в переполнение стека Ираклий:

Вот простой пример использования сюжета с использованием Plotly и подзаговоров () :

library(dplyr)
library(plotly)

# plot1
fig1 = plot_ly(data = mtcars, type = 'scatter', mode='markers', x = ~mpg, y= ~hp, name = "HP")

# plot2
fig2 = plot_ly(data = mtcars, type = 'scatter', mode= 'markers', x = ~mpg,y = ~drat, name="Drat")

subplot(fig1, fig2, shareX = T, nrows = 2)

Вот вывод:

image

0 голосов
/ 23 марта 2020

Еще один вариант получения подобного сюжета можно получить с помощью пакета cowplot. Вот пример использования набора данных mtcars.

library(cowplot)
library(ggplot2)

#Create first plot
p1<-ggplot(mtcars, aes(x = mpg, 
                   y = disp)) +
  geom_point() 

#Create second plot
p2<-ggplot(mtcars, aes(x = mpg, 
                       y = hp)) +
  geom_point()+ 
  #Remove x axis ticks, title and text
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.x = element_blank())

#Create a canvas and draw the two plots with different sizes.
#x and y define the plot's lower left corner where each plot will be set (0-1)
# width and height can be interpreted as the proportion (0-1) of the original
#plot to be used to draw each plot
p3<-ggdraw()+
  draw_plot(p1,x=0,y=0,width=1,height=0.7)+
  draw_plot(p2,x=0,y=0.7,width=1,height=0.3)
p3

cowplot

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...