在R中对齐直方图和散点图

时间:2017-06-06 08:33:20

标签: r alignment background-image histogram scatter-plot

我试图显示两个图,一个直方图和一个散点图,我希望它们相对于x轴对齐。我用hist()和我的散点图用ggplot2创建了直方图。但是,我发现创建一个混合这两个的多时隙的问题,所以我决定使用ggplot2创建直方图。请在下面找到代码:

loadedimage <- readTIFF("my_data05.tiff")
rasterizedimage <- rasterGrob(loadedimage, interpolate = TRUE)
realmadrid_colors <-c("#00529f", "#8cc1f3", "blue")
data_test <- subset(full_data, Rival == "Barcelona")
data_kroos <- subset(data_test, Jugador == "Kroos")

myscatter <- ggplot(data_kroos, aes(x = x1, y = y1)) + 
  annotation_custom(rasterizedimage, xmin = 0, xmax = 713, ymin = 0 , ymax = 497) +
  geom_point(aes(fill = factor(data_kroos$Resultado)),
         shape = 21, 
         size = 5) +
  scale_fill_manual(values=realmadrid_colors) + 
  guides(fill=guide_legend(title=NULL)) +
  coord_fixed() + 
  theme_bw() + 
  theme_no_axes(base.theme = theme_bw()) + 
  theme(legend.position = c(0.5, 0.04),
    legend.box = "horizontal",
    legend.direction = "horizontal",
    legend.box.background = element_rect(fill = NA, colour = "transparent"),
    legend.text = element_text(family = "Courier", size = 14),
    axis.title = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    plot.margin=unit(c(-0.05,-0.05,-0.05,-0.1),"in")
    )

myhistogram <- qplot(data_barca[data_barca$Sección == "Passes_All",]$x1, breaks=seq(0, 660, by = 66), alpha = 0.5, geom = "histogram")

如果我尝试使用grid.newpage()和grid.draw(),我收到一条错误消息:ncol(x)== ncol(y)不为TRUE

grid.newpage()
grid.draw(rbind(ggplotGrob(myscatter), ggplotGrob(myhistogram), size = "first"))

我希望得到http://biokit.readthedocs.io/en/latest/auto_examples/plot_scatter.html之类的内容 (事实上​​,我仍处于第一步,我的目标与前一个链接完全相同),或类似combine histogram with scatter plot in R之类的东西,但在这种情况下,它不使用ggplot2,我不确定我是否可以玩就像使用那个包一样。

欢迎任何帮助!如果您需要我上传任何文件,请告诉我。

提前致谢!

1 个答案:

答案 0 :(得分:0)

进一步搜索可能是我的答案!我正在寻找的是边缘直方图!

Scatterplot with marginal histograms in ggplot2